Skip to content

Commit d5c6760

Browse files
committed
Option recursive
1 parent f4599f0 commit d5c6760

File tree

8 files changed

+50
-4
lines changed

8 files changed

+50
-4
lines changed

gulp-cssimport.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ var Chunk = require("./chunk");
1414
var defaults = {
1515
extensions: null,
1616
filter: null,
17-
matchPattern: null
17+
matchPattern: null,
18+
limit: 5000
1819
};
1920
Object.defineProperty(defaults, "directory", {
2021
enumerable: true,
@@ -32,8 +33,14 @@ module.exports = function cssImport(options) {
3233
return x.trim();
3334
});
3435
}
35-
36+
37+
var stream;
38+
var cssCount = 0;
39+
3640
function fileContents(data, encoding, callback) {
41+
if (!stream) {
42+
stream = this;
43+
}
3744
var chunk = Chunk.create(data, { directory: options.directory });
3845
//console.log("chunk.isVinyl", chunk.isVinyl);
3946
// https://github.com/kevva/import-regex/
@@ -61,6 +68,11 @@ module.exports = function cssImport(options) {
6168
fileArray[index] = format("importing file %j", pathObject);
6269
lastPos = importRe.lastIndex;
6370
// Start resolving.
71+
// console.log("Start resolving", cssCount++, pathObject);
72+
if (++cssCount > options.limit) {
73+
stream.emit("error", new Error("Exceed limit. Recursive include?"));
74+
return;
75+
}
6476
count++;
6577
resolvePath(pathObject, onResolvePath);
6678
}
@@ -98,13 +110,12 @@ module.exports = function cssImport(options) {
98110
if (fileArray.length > 0) {
99111
contents = fileArray.join("");
100112
}
101-
// todo: 1. options for max recursive
102113
if (!state.done) {
103114
//console.log("chunk.isVinyl", chunk.isVinyl);
104115
var nextChunk;
105116
if (chunk.isVinyl) {
106117
chunk.vinyl.contents = new Buffer(contents);
107-
chunk.vinyl.base = state.directory;
118+
chunk.vinyl.base = state.directory;
108119
nextChunk = chunk.vinyl;
109120
} else {
110121
nextChunk = Chunk.create({

readme.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ Urls are matched by default. If you do not want include them, use `filter` optio
4545
#### matchOptions
4646
Object, [minimatch](https://www.npmjs.com/package/minimatch) options for `matchPattern`.
4747

48+
#### limit
49+
Number, default 5000.
50+
Defence from infinite recursive import.
51+
4852
#### extensions
4953
Deprecated, use `matchPattern` instead.
5054
String or Array, default: null (process all).

test/gulp-recursive/design/a.css

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/* a.css */
2+
@import "b.css";

test/gulp-recursive/design/b.css

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/* b.css */
2+
@import "c.css";

test/gulp-recursive/design/c.css

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/* c.css */
2+
@import "a.css";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@import url("./a.css");

test/gulp-recursive/index.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/// <reference path="../../typings/node/node.d.ts" />
2+
/// <reference path="../../typings/tape/tape.d.ts" />
3+
var test = require("tape");
4+
var fs = require("fs");
5+
var collect = require("collect-stream");
6+
var plugin = require("../..");
7+
var gulp = require("gulp");
8+
9+
var options = {
10+
limit: 100
11+
};
12+
13+
test("Recursive", function (t) {
14+
var stream;
15+
t.plan(1);
16+
var p = plugin(options);
17+
stream = gulp.src("design/style.css")
18+
.pipe(p)
19+
.pipe(gulp.dest("/dev/null"));
20+
p.on("error", function (err) {
21+
t.ok(err.message.indexOf("Recursive include") != -1, "Emit error");
22+
});
23+
});

test/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ var tests = [
1010
"options-extensions-only",
1111
"options-filter",
1212
"complete",
13+
"gulp-recursive",
1314
"gulp-complete"
1415
];
1516
emitter.on("run", function(index) {

0 commit comments

Comments
 (0)