Skip to content

Commit

Permalink
fix: issue #135 relating watch mode (#136)
Browse files Browse the repository at this point in the history
* fix: issue #135 relating watch mode

* fix: use removeListener instead of off
  • Loading branch information
AngusFu authored and egoist committed Nov 16, 2018
1 parent 0eeed30 commit 0373281
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 16 deletions.
48 changes: 32 additions & 16 deletions src/index.js
Expand Up @@ -575,23 +575,39 @@ export default class Bili extends EventEmitter {
}:\n`) + util.inspect(outputOptions, { colors: true }))

if (this.options.watch) {
const watcher = watch({
...inputOptions,
output: outputOptions,
watch: {
clearScreen: true
}
})
watcher.on('event', async e => {
if (e.code === 'ERROR' || e.code === 'FATAL') {
handleError(e.error)
}
if (e.code === 'BUNDLE_END') {
process.exitCode = 0
logger.write(await this.stats())
}
return new Promise(resolve => {
const watcher = watch({
...inputOptions,
output: outputOptions,
watch: {
clearScreen: true
}
})

watcher.on('event', async e => {
if (e.code === 'ERROR' || e.code === 'FATAL') {
handleError(e.error)
}
if (e.code === 'BUNDLE_END') {
process.exitCode = 0
logger.write(await this.stats())
}
})

// better ensure that `ongenerate` methods run at least once
// since we will check `bundleCount` later
// SEE https://github.com/egoist/bili/issues/135
watcher.on('event', function handler(e) {
if (e.code === 'BUNDLE_END') {
watcher.removeListener('event', handler)
resolve()
}
})

// save watchers to gain more control over them
this.watchers = this.watchers || []
this.watchers.push(watcher)
})
return
}

const bundle = await rollup(inputOptions)
Expand Down
22 changes: 22 additions & 0 deletions test/watch.test.js
@@ -0,0 +1,22 @@
import path from 'path'
import fs from 'fs'
import Bili from '../src'

const filename = 'watch.test.output.js'
const input = path.resolve(__dirname, 'demo/index.js')
const option = {
input,
filename,
outDir: path.resolve(__dirname),
watch: true
}

test('watch', async () => {
const result = await Bili.generate(option)
expect(result).toHaveProperty('bundles')
expect(result.watchers.length).toBe(1)

// close and clean
result.watchers[0].close()
fs.unlinkSync(path.resolve(__dirname, filename))
})

0 comments on commit 0373281

Please sign in to comment.