Skip to content

Commit a8921cc

Browse files
zenbrentmichael-ciniawsky
authored andcommittedFeb 2, 2018
fix(index): continue watching after dependency {Error} (#332)
1 parent 08c063a commit a8921cc

File tree

10 files changed

+127
-5
lines changed

10 files changed

+127
-5
lines changed
 

‎lib/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ module.exports = function loader (css, map) {
186186
return null
187187
})
188188
}).catch((err) => {
189+
if (err.file) this.addDependency(err.file)
189190
return err.name === 'CssSyntaxError' ? cb(new SyntaxError(err)) : cb(err)
190191
})
191192
}

‎package.json

+2
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@
2020
"jest": "^21.0.0",
2121
"jsdoc-to-markdown": "^3.0.0",
2222
"memory-fs": "^0.4.0",
23+
"postcss-import": "^11.0.0",
2324
"postcss-js": "^1.0.0",
2425
"standard": "^10.0.0",
2526
"standard-version": "^4.0.0",
2627
"sugarss": "^1.0.0",
28+
"util.promisify": "^1.0.0",
2729
"webpack": "^3.0.0"
2830
},
2931
"scripts": {
+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

33
exports[`Loader Default 1`] = `"module.exports = \\"a { color: black }\\\\n\\""`;
4+
5+
exports[`Loader Watching Dependencies Error 1`] = `"module.exports = \\"a { color: black }\\\\n\\""`;
6+
7+
exports[`Loader Watching Dependencies Error 2`] = `"throw new Error(\\"Module build failed: Syntax Error \\\\n\\\\n(1:5) Unknown word\\\\n\\\\n\\\\u001b[31m\\\\u001b[1m>\\\\u001b[22m\\\\u001b[39m\\\\u001b[90m 1 | \\\\u001b[39ma \\\\u001b[33m{\\\\u001b[39m color black \\\\u001b[33m}\\\\u001b[39m\\\\n \\\\u001b[90m | \\\\u001b[39m \\\\u001b[31m\\\\u001b[1m^\\\\u001b[22m\\\\u001b[39m\\\\n \\\\u001b[90m 2 | \\\\u001b[39m\\\\n\\");"`;
8+
9+
exports[`Loader Watching Dependencies Error 3`] = `"module.exports = \\"a { color: black }\\\\n\\""`;

‎test/fixtures/watch/watching/index.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import style from './style.css'
2+
3+
export default style
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
a { color: black }
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@import "./styleDep";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
a { color black }

‎test/helpers/compiler.js

+15-5
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,21 @@ module.exports = function compiler (fixture, config, options) {
4343

4444
if (!options.emit) compiler.outputFileSystem = new MemoryFS()
4545

46-
return new Promise((resolve, reject) => {
47-
return compiler.run((err, stats) => {
48-
if (err) reject(err)
46+
if (options.watch) {
47+
return new Promise((resolve, reject) => {
48+
const watcher = compiler.watch({}, (err, stats) => {
49+
options.watch(err, stats, (s) => {
50+
watcher.close(resolve)
51+
})
52+
})
53+
})
54+
} else {
55+
return new Promise((resolve, reject) => {
56+
return compiler.run((err, stats) => {
57+
if (err) reject(err)
4958

50-
resolve(stats)
59+
resolve(stats)
60+
})
5161
})
52-
})
62+
}
5363
}

‎test/helpers/fs.js

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
const path = require('path')
2+
const { readFile: _readFile, writeFile: _writeFile, unlink: _unlink } = require('fs')
3+
const promisify = require('util.promisify')
4+
5+
const fs = {
6+
readFile: promisify(_readFile),
7+
writeFile: promisify(_writeFile),
8+
unlink: promisify(_unlink)
9+
}
10+
11+
function readFile (name) {
12+
const file = path.join(__dirname, '../fixtures', name)
13+
14+
return fs.readFile(file)
15+
.then(data => data.toString())
16+
}
17+
18+
function writeFile (name, contents) {
19+
const file = path.join(__dirname, '../fixtures', name)
20+
21+
return fs.writeFile(file, contents)
22+
}
23+
24+
module.exports.copyFile = function copyFile (src, dest) {
25+
return readFile(src)
26+
.then(contents => writeFile(dest, contents))
27+
}
28+
29+
module.exports.deleteFile = function deleteFile (name) {
30+
const file = path.join(__dirname, '../fixtures', name)
31+
32+
return fs.unlink(file)
33+
}

‎test/loader.test.js

+64
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const webpack = require('./helpers/compiler')
44
const { loader } = require('./helpers/compilation')
5+
const { copyFile, deleteFile } = require('./helpers/fs');
56

67
describe('Loader', () => {
78
test('Default', () => {
@@ -20,4 +21,67 @@ describe('Loader', () => {
2021
expect(src).toMatchSnapshot()
2122
})
2223
})
24+
25+
describe('Watching', () => {
26+
describe('Dependencies', () => {
27+
const files = {
28+
syntaxError: "watch/watching/syntaxError.css",
29+
noSyntaxError: "watch/watching/noSyntaxError.css",
30+
changingFile: "watch/watching/styleDep.css"
31+
}
32+
33+
beforeEach(() => copyFile(files.noSyntaxError, files.changingFile))
34+
35+
afterEach(() => deleteFile(files.changingFile))
36+
37+
test('Error', () => {
38+
const config = {
39+
loader: {
40+
options: {
41+
plugins: [require("postcss-import")],
42+
}
43+
}
44+
}
45+
46+
const steps = [
47+
(stats) => {
48+
const { err, src } = loader(stats)
49+
50+
expect(src).toMatchSnapshot()
51+
expect(err.length).toEqual(0)
52+
53+
return copyFile(files.syntaxError, files.changingFile)
54+
},
55+
(stats) => {
56+
const { err, src } = loader(stats)
57+
58+
expect(src).toMatchSnapshot()
59+
expect(err.length).toEqual(1)
60+
61+
return copyFile(files.noSyntaxError, files.changingFile)
62+
},
63+
(stats, close) => {
64+
const { err, src } = loader(stats)
65+
66+
expect(src).toMatchSnapshot()
67+
expect(src).toEqual("module.exports = \"a { color: black }\\n\"")
68+
expect(err.length).toEqual(0)
69+
70+
return close()
71+
}
72+
];
73+
74+
var currentStep = 0
75+
76+
const options = {
77+
watch (err, stats, close) {
78+
steps[currentStep](stats, close)
79+
currentStep++
80+
}
81+
}
82+
83+
return webpack('watch/watching/index.js', config, options)
84+
})
85+
})
86+
})
2387
})

0 commit comments

Comments
 (0)
Please sign in to comment.