Skip to content

Commit e8225d8

Browse files
committed
Added skipComments option, closes #19
1 parent da04809 commit e8225d8

File tree

6 files changed

+28
-22
lines changed

6 files changed

+28
-22
lines changed

.eslintrc.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@ module.exports = {
1515
2,
1616
"unix"
1717
],
18-
"quotes": [
19-
2,
20-
"double"
21-
],
2218
"semi": [
2319
2,
2420
"always"

gulp-cssimport.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ var trim = require("lodash.trim");
1818
var format = require("util").format;
1919

2020
var defaults = {
21+
skipComments: true,
2122
extensions: null,
2223
includePaths: [],
2324
filter: null,
@@ -55,6 +56,21 @@ module.exports = function cssImport(options) {
5556
var promises = [];
5657
var contents = vinyl.contents.toString();
5758
while ((match = importRe.exec(contents)) !== null) {
59+
if (options.skipComments) {
60+
var matchIndex = match.index;
61+
// Check comment symbols 1.
62+
var startCommentPosition = contents.lastIndexOf('/*', matchIndex);
63+
var endCommentPosition = contents.lastIndexOf('*/', matchIndex);
64+
if (!(endCommentPosition > startCommentPosition) && startCommentPosition !== -1) {
65+
continue;
66+
}
67+
// Check comment symbols 2.
68+
var startCommentPosition2 = contents.lastIndexOf('//', matchIndex);
69+
var endCommentPosition2 = contents.lastIndexOf('\n', matchIndex);
70+
if (startCommentPosition2 > endCommentPosition2 && startCommentPosition2 !== -1) {
71+
continue;
72+
}
73+
}
5874
var match2 = /@import\s+(?:url\()?(.+(?=['"\)]))(?:\))?.*/ig.exec(match[0]);
5975
var importPath = trim(match2[1], "'\"");
6076
if (!isMatch(importPath, options)) {
@@ -76,7 +92,7 @@ module.exports = function cssImport(options) {
7692
var pathDirectory = path.dirname(vinyl.path);
7793
var importFile = resolveImportFile(pathDirectory, importPath, options.includePaths);
7894
if (!importFile) {
79-
var err = new Error(`Cannot find file '${importPath}' from '${pathDirectory}' (includePaths: ${options.includePaths})`);
95+
var err = new Error("Cannot find file '" + importPath + "' from '" + pathDirectory + "' (includePaths: " + options.includePaths + ")");
8096
callback(new gutil.PluginError(PLUGIN_NAME, err));
8197
}
8298
promises.push(readFile(importFile, "utf8").then(function(contents) {

gulpfile.js

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,8 @@ gulp.task("watch-eslint", ["eslint"], function() {
88

99
gulp.task("eslint", function() {
1010
var eslint = g.eslint;
11-
var conf = {
12-
rules: {
13-
"no-use-before-define": [0, "nofunc"],
14-
"curly": 1,
15-
"no-comma-dangle": 1,
16-
"no-debugger": 1,
17-
"eol-last": 0,
18-
"new-cap": 1,
19-
"no-underscore-dangle": 0
20-
},
21-
env: {
22-
node: true
23-
}
24-
};
2511
gulp.src(["./gulp-cssimport.js"])
26-
.pipe(eslint(conf))
12+
.pipe(eslint())
2713
// .pipe(eslint.failOnError())
2814
.pipe(eslint.formatEach("stylish", process.stdout));
2915
});

readme.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ OPTIONS
2727
Array, default: `[]`
2828
Additional paths to resolve imports.
2929

30+
#### skipComments
31+
Boolean, default: `true`
32+
gulp-cssimport plugin uses regular expressions which is fast but not solid as AST.
33+
If you have any unexpected result, missing imported content, etc. Try to disable this option.
34+
3035
#### filter
3136
RegExp, default: `null` (no filter).
3237
Process only files which match to regexp.
@@ -119,6 +124,9 @@ TODO
119124

120125
CHANGELOG
121126
---------
127+
5.0 [6 Oct 2016]
128+
- added option 'skipComments'
129+
122130
4.0 [6 Oct 2016]
123131
- added option 'includePaths'
124132

test/sass-commented-import/design/style.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
// @import url("./x1.css"); /**/
33
@import url("./b.css");
44
/*
5-
@import url("./x2.css"); /*
5+
// // @import url("./x2.css"); /*
66
/**/
77
@import url("./c.css");

test/sass-commented-import/result.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
// @import url("./x1.css"); /**/
33
/* b.css */
44
/*
5-
@import url("./x2.css"); /*
5+
// // @import url("./x2.css"); /*
66
/**/
77
/* c.css */

0 commit comments

Comments
 (0)