Skip to content

Commit 94f9fba

Browse files
author
Jeff Escalante
committed
Merge pull request #82 from carrot/watchfix
Watch mode fixes
2 parents 4b28a6e + abde576 commit 94f9fba

File tree

3 files changed

+32
-31
lines changed

3 files changed

+32
-31
lines changed

lib/index.js

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -23,35 +23,17 @@ export default class Roots extends EventEmitter {
2323
const id = this._id()
2424
const compiler = webpack(this.config)
2525

26-
compiler.run((err, stats) => {
27-
if (err) {
28-
return this.emit('error', new RootsError({ id: id, message: err }))
29-
}
30-
31-
// Webpack "soft errors" are classified as warnings in roots. An error is
32-
// an error. If it doesn't break the build, it's a warning.
33-
const jsonStats = stats.toJson()
34-
if (jsonStats.errors.length > 0) {
35-
this.emit('warning', new RootsWarning({ id: id, message: jsonStats.errors }))
36-
}
37-
if (jsonStats.warnings.length > 0) {
38-
this.emit('warning', new RootsWarning({ id: id, message: jsonStats.warnings }))
39-
}
40-
41-
this.emit('compile', { id: id, stats: stats })
42-
})
26+
compiler.run(compileCallback.bind(this, id))
4327

4428
// Returns the compilation's ID synchronously, this can be checked against
4529
// events emitted from the project instance.
4630
return [id, compiler]
4731
}
4832

49-
watch (opts) {
50-
const [, compiler] = this.compile()
51-
return compiler.watch(opts, (err, stats) => {
52-
if (err) { return this.emit('error', err) }
53-
this.emit('compile', { stats: stats })
54-
})
33+
watch (opts = {}) {
34+
const id = this._id()
35+
const compiler = webpack(this.config)
36+
return compiler.watch(opts, compileCallback.bind(this, id))
5537
}
5638

5739
clean () {
@@ -117,3 +99,21 @@ export default class Roots extends EventEmitter {
11799
function npmInstall (opts) {
118100
return node.call(exec, 'npm install', { cwd: opts.root })
119101
}
102+
103+
function compileCallback (id, err, stats) {
104+
if (err) {
105+
return this.emit('error', new RootsError({ id: id, message: err }))
106+
}
107+
108+
// Webpack "soft errors" are classified as warnings in roots. An error is
109+
// an error. If it doesn't break the build, it's a warning.
110+
const jsonStats = stats.toJson()
111+
if (jsonStats.errors.length) {
112+
this.emit('warning', new RootsWarning({ id: id, message: jsonStats.errors }))
113+
}
114+
if (jsonStats.warnings.length) {
115+
this.emit('warning', new RootsWarning({ id: id, message: jsonStats.warnings }))
116+
}
117+
118+
this.emit('compile', { id: id, stats: stats })
119+
}

lib/plugins/fs_plugin.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ export default class FsWebpackPlugin {
1010
}
1111

1212
apply (compiler) {
13-
// read file tree and get all files
14-
compiler.plugin('run', (compilation, done) => {
15-
compiler.options.files = util.getFilesFromGlob(compiler, this.opts)
16-
done()
17-
})
13+
compiler.plugin('run', run.bind(this, compiler))
14+
compiler.plugin('watch-run', run.bind(this, compiler))
1815
}
1916
}
17+
18+
function run (compiler, compilation, done) {
19+
compiler.options.files = util.getFilesFromGlob(compiler, this.opts)
20+
done()
21+
}

test/watch.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,14 @@ test.cb('watches the project, reloads on modification', (t) => {
1515
})
1616
let i = 0
1717

18-
// right now it compiles twice on the first shot for some reason
1918
project.on('compile', (res) => {
2019
i++
21-
if (i > 1) {
20+
if (i === 1) {
2221
const file = path.join(fixturesPath, 'watch/index.jade')
2322
fs.appendFileSync(file, ' ')
2423
fs.writeFileSync(file, fs.readFileSync(file, 'utf8').trim())
2524
}
26-
if (i > 2) {
25+
if (i === 2) {
2726
watcher.close()
2827
t.end()
2928
}

0 commit comments

Comments
 (0)