Skip to content

Commit fe5a8b5

Browse files
committed
Code style fixes
1 parent b1b97d4 commit fe5a8b5

File tree

5 files changed

+46
-41
lines changed

5 files changed

+46
-41
lines changed

lib/index.js

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

test/__snapshots__/loader.test.js.snap

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
exports[`Loader Default 1`] = `"module.exports = \\"a { color: black }\\\\n\\""`;
44

5-
exports[`Loader Watching Deps After An Error Default 1`] = `"module.exports = \\"a { color: black }\\\\n\\""`;
5+
exports[`Loader Watching Default 1`] = `"module.exports = \\"a { color: black }\\\\n\\""`;
66

7-
exports[`Loader Watching Deps After An Error Default 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\\");"`;
7+
exports[`Loader Watching Default 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\\");"`;
88
9-
exports[`Loader Watching Deps After An Error Default 3`] = `"module.exports = \\"a { color: black }\\\\n\\""`;
9+
exports[`Loader Watching Default 3`] = `"module.exports = \\"a { color: black }\\\\n\\""`;

test/helpers/compiler.js

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

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

46-
if (options.watching) {
46+
if (options.watch) {
4747
return new Promise((resolve, reject) => {
48-
const c = compiler.watch({}, (err, stats) => {
49-
options.handler(err, stats, (s) => {
50-
c.close(resolve)
48+
const watcher = compiler.watch({}, (err, stats) => {
49+
options.watch(err, stats, (s) => {
50+
watcher.close(resolve)
5151
})
5252
})
5353
})

test/helpers/fs.js

+19-17
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,33 @@
11
const path = require('path')
2-
const { readFile, writeFile, unlink } = require('fs')
2+
const { readFile: _readFile, writeFile: _writeFile, unlink: _unlink } = require('fs')
33
const promisify = require('util.promisify')
44

5-
const rf = promisify(readFile)
6-
const wf = promisify(writeFile)
7-
const rm = promisify(unlink)
5+
const fs = {
6+
readFile: promisify(_readFile),
7+
writeFile: promisify(_writeFile),
8+
unlink: promisify(_unlink)
9+
}
810

9-
function readCssFile (name) {
10-
const fileName = path.join(__dirname, '../fixtures', name)
11+
function readFile (name) {
12+
const file = path.join(__dirname, '../fixtures', name)
1113

12-
return rf(fileName)
13-
.then(c => c.toString())
14+
return fs.readFile(file)
15+
.then(data => data.toString())
1416
}
1517

16-
function writeCssFile (name, contents) {
17-
const fileName = path.join(__dirname, '../fixtures', name)
18+
function writeFile (name, contents) {
19+
const file = path.join(__dirname, '../fixtures', name)
1820

19-
return wf(fileName, contents)
21+
return fs.writeFile(file, contents)
2022
}
2123

22-
module.exports.copyCssFile = function copyCssFile (src, dest) {
23-
return readCssFile(src)
24-
.then(contents => writeCssFile(dest, contents))
24+
module.exports.copyFile = function copyFile (src, dest) {
25+
return readFile(src)
26+
.then(contents => writeFile(dest, contents))
2527
}
2628

27-
module.exports.deleteCssFile = function deleteCssFile (name) {
28-
const fileName = path.join(__dirname, '../fixtures', name)
29+
module.exports.deleteFile = function deleteFile (name) {
30+
const file = path.join(__dirname, '../fixtures', name)
2931

30-
return rm(fileName)
32+
return fs.unlink(file)
3133
}

test/loader.test.js

+19-16
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

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

77
describe('Loader', () => {
88
test('Default', () => {
@@ -22,61 +22,64 @@ describe('Loader', () => {
2222
})
2323
})
2424

25-
describe('Watching Deps After An Error', () => {
25+
describe('Watching', () => {
2626
const files = {
27-
syntaxError: "css-watching/syntaxError.css",
28-
noSyntaxError: "css-watching/noSyntaxError.css",
29-
changingFile: "css-watching/styleDep.css"
27+
syntaxError: "watch/watching/syntaxError.css",
28+
noSyntaxError: "watch/watching/noSyntaxError.css",
29+
changingFile: "watch/watching/styleDep.css"
3030
}
3131

32-
beforeAll(() => copyCssFile(files.noSyntaxError, files.changingFile))
32+
beforeAll(() => copyFile(files.noSyntaxError, files.changingFile))
3333

34-
afterAll(() => deleteCssFile(files.changingFile))
34+
afterAll(() => deleteFile(files.changingFile))
3535

3636
test('Default', () => {
3737
const config = {
3838
loader: {
3939
options: {
4040
plugins: [require("postcss-import")],
41-
watching: true
4241
}
4342
}
4443
}
4544

46-
const testSteps = [
45+
const steps = [
4746
(stats) => {
4847
const { err, src } = loader(stats)
48+
4949
expect(src).toMatchSnapshot()
5050
expect(err.length).toEqual(0)
51-
return copyCssFile(files.syntaxError, files.changingFile)
51+
52+
return copyFile(files.syntaxError, files.changingFile)
5253
},
5354
(stats) => {
5455
const { err, src } = loader(stats)
56+
5557
expect(src).toMatchSnapshot()
5658
expect(err.length).toEqual(1)
57-
return copyCssFile(files.noSyntaxError, files.changingFile)
59+
60+
return copyFile(files.noSyntaxError, files.changingFile)
5861
},
5962
(stats, close) => {
6063
const { err, src } = loader(stats)
64+
6165
expect(src).toMatchSnapshot()
6266
expect(src).toEqual("module.exports = \"a { color: black }\\n\"")
6367
expect(err.length).toEqual(0)
68+
6469
return close()
6570
}
6671
];
6772

6873
var currentStep = 0
6974

7075
const options = {
71-
watching: true,
72-
handler: (err, stats, close) => {
73-
testSteps[currentStep](stats, close)
76+
watch (err, stats, close) {
77+
steps[currentStep](stats, close)
7478
currentStep++
7579
}
7680
}
7781

78-
return webpack('css-watching/index.js', config, options)
82+
return webpack('watch/watching/index.js', config, options)
7983
})
8084
})
81-
8285
})

0 commit comments

Comments
 (0)