Skip to content
This repository was archived by the owner on Mar 22, 2022. It is now read-only.

Commit f8d1a39

Browse files
committed
Merge branch 'develop'
2 parents 8a96390 + a374605 commit f8d1a39

File tree

5 files changed

+92
-19
lines changed

5 files changed

+92
-19
lines changed

.eslintrc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
"max-lines": ["error", 400],
5858
"max-nested-callbacks": "error",
5959
"max-params": "error",
60-
"max-statements": ["error", 15],
60+
"max-statements": ["error", 20],
6161
"max-statements-per-line": "error",
6262
"multiline-ternary": ["error", "always-multiline"],
6363
"new-parens": "error",

lib/ImportTree.js

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@ const globby = require("globby")
33
const path = require("path")
44
const gutil = require("gulp-util")
55

6-
const MULTILINE_COMMENTS = /\/\*[\s\S]*?\*\//g
7-
const SINGLELINE_COMMENTS = /[ \t]*\/\/[^\r\n]*/g
8-
const IMPORT_STATEMENTS = /^[ \t]*@import[ \t]+(["'])([^\r\n]+?)\1/gm
6+
const findImportedPaths = require("./findImportedPaths")
7+
98
const STYLESHEET_EXTENSION = /\.s?css$/
109
const FILENAME = /([^/\\]+)$/
1110
const FILENAME_WITHOUT_LEADING_UNDERSCORE = /[/\\][^_/\\][^/\\]*$/
@@ -113,18 +112,13 @@ module.exports = class ImportTree {
113112

114113
readFile(file) {
115114
if (file.endsWith(".scss")) {
116-
const content = fs.readFileSync(file, "utf8")
117-
.replace(MULTILINE_COMMENTS, "")
118-
.replace(SINGLELINE_COMMENTS, "")
119-
IMPORT_STATEMENTS.lastIndex = 0
120-
let match = null
121-
while (null !== (match = IMPORT_STATEMENTS.exec(content))) {
115+
findImportedPaths(file).forEach((importedPath) => {
122116
try {
123-
resolveImport.call(this, file, match[2])
117+
resolveImport.call(this, file, importedPath)
124118
} catch (e) {
125119
this.warn(e.message)
126120
}
127-
}
121+
})
128122
}
129123
}
130124

lib/findImportedPaths.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const fs = require("fs")
2+
3+
const MULTILINE_COMMENTS = /\/\*[^]*?\*\//g
4+
const SINGLELINE_COMMENTS = /\/\/.*/g
5+
const IMPORT_STATEMENTS = (() => {
6+
const stringLiteral = "(?:'[^'\\r\\n]*'|\"[^\"\\r\\n]*\")"
7+
const importStatement = `@import\\s*(${stringLiteral}(?:\\s*,\\s*${stringLiteral})*)`
8+
return new RegExp(importStatement, "g")
9+
})()
10+
const COMMA = /\s*,\s*/
11+
12+
const removeComments = (content) => content
13+
.replace(MULTILINE_COMMENTS, "")
14+
.replace(SINGLELINE_COMMENTS, "")
15+
16+
const findImportValues = (content) => {
17+
const cleanedContent = removeComments(content)
18+
IMPORT_STATEMENTS.lastIndex = 0
19+
let match = null
20+
const importValues = []
21+
while (null !== (match = IMPORT_STATEMENTS.exec(cleanedContent))) {
22+
importValues.push(match[1])
23+
}
24+
return importValues
25+
}
26+
27+
const findImportedPaths = (content) => findImportValues(content)
28+
.reduce((stringLiterals, importValue) => stringLiterals.concat(importValue.split(COMMA)), [])
29+
.map((stringLiteral) => stringLiteral.slice(1, stringLiteral.length - 1))
30+
31+
module.exports = (file) => findImportedPaths(fs.readFileSync(file, "utf8"))

package.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "gulp-watch-sass",
3-
"version": "1.3.2",
3+
"version": "1.4.0",
44
"description": "Watches SASS files, adding @import-ing files to the stream",
55
"keywords": [
66
"gulpplugin",
@@ -23,20 +23,20 @@
2323
"dependencies": {
2424
"globby": "~7.1.1",
2525
"gulp-fn": "~0.0.0",
26-
"gulp-watch": "~4.3.11",
26+
"gulp-watch": "~5.0.0",
2727
"vinyl-file": "~3.0.0"
2828
},
2929
"devDependencies": {
3030
"coveralls": "~3.0.0",
31-
"eslint": "~4.12.1",
31+
"eslint": "~4.16.0",
3232
"husky": "~0.14.3",
3333
"istanbul": "~0.4.5",
3434
"mkdirp": "~0.5.1",
35-
"mocha": "~4.0.1",
35+
"mocha": "~5.0.0",
3636
"rimraf": "~2.6.2",
37-
"should": "~13.1.3",
38-
"sinon": "~4.1.3",
39-
"snyk": "~1.54.1"
37+
"should": "~13.2.1",
38+
"sinon": "~4.2.1",
39+
"snyk": "~1.69.3"
4040
},
4141
"scripts": {
4242
"commitmsg": "node ./.hooks/commit-msg",

test/files.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,30 @@ describe("gulp-watch-sass", () => {
3434
console.warn.called.should.be.false()
3535
}
3636

37+
it("should handle both single and double quotes", () => {
38+
create("a.scss", "@import 'b.scss';\n@import \"c.scss\";")
39+
create("b.scss", "div { margin: 0; }")
40+
create("c.scss", "div { margin: 0; }")
41+
testChanges([
42+
{ change: "b.scss", expect: "a.scss" }
43+
])
44+
testChanges([
45+
{ change: "c.scss", expect: "a.scss" }
46+
])
47+
})
48+
49+
it("should handle multiple imports on a single line", () => {
50+
create("a.scss", "@import 'b.scss'; @import 'c.scss';")
51+
create("b.scss", "div { margin: 0; }")
52+
create("c.scss", "div { margin: 0; }")
53+
testChanges([
54+
{ change: "b.scss", expect: "a.scss" }
55+
])
56+
testChanges([
57+
{ change: "c.scss", expect: "a.scss" }
58+
])
59+
})
60+
3761
it("should handle SASS files with .scss extension", () => {
3862
create("a.scss", "@import 'b.scss';")
3963
create("b.scss", "div { margin: 0; }")
@@ -131,4 +155,28 @@ describe("gulp-watch-sass", () => {
131155
], ["dir2"])
132156
})
133157

158+
it("should handle comma-separated imports", () => {
159+
create("a.scss", "@import 'b.scss',\n'c.scss';")
160+
create("b.scss", "div { margin: 0; }")
161+
create("c.scss", "div { margin: 0; }")
162+
testChanges([
163+
{ change: "b.scss", expect: "a.scss" }
164+
])
165+
testChanges([
166+
{ change: "c.scss", expect: "a.scss" }
167+
])
168+
})
169+
170+
it("should handle comma-separated imports on a single line", () => {
171+
create("a.scss", "@import 'b.scss', 'c.scss';")
172+
create("b.scss", "div { margin: 0; }")
173+
create("c.scss", "div { margin: 0; }")
174+
testChanges([
175+
{ change: "b.scss", expect: "a.scss" }
176+
])
177+
testChanges([
178+
{ change: "c.scss", expect: "a.scss" }
179+
])
180+
})
181+
134182
})

0 commit comments

Comments
 (0)