Skip to content
This repository has been archived by the owner on Dec 6, 2021. It is now read-only.

Commit

Permalink
don't quit when failed in watch mode
Browse files Browse the repository at this point in the history
  • Loading branch information
egoist committed Feb 17, 2017
1 parent 3c27fde commit b87e8d9
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,9 @@ const res = vbuild(cliOptions)
{
webpackConfig, // final webpack config
options, // final options
server // in dev mode, an instance of `http.Server`
compiler, // webpack compiler instance
server, // in dev mode, an instance of `http.Server`
watcher // in watch mode, webpack watcher
}

// error catch
Expand Down
11 changes: 7 additions & 4 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ function start(cliOptions = {}) { // eslint-disable-line complexity
webpackConfig = webpackMerge.smart(webpackConfig, options.webpack)
}

const server = run(webpackConfig, options)
const result = run(webpackConfig, options)

if (cliOptions.dev || cliOptions.watch) {
const configFile = getConfigFile(options.config)
Expand All @@ -331,8 +331,11 @@ function start(cliOptions = {}) { // eslint-disable-line complexity
if (e === 'rename') {
return
}
if (server) {
server.close()
if (result.server) {
result.server.close()
}
if (result.watcher) {
result.watcher.close()
}
if (watcher) {
watcher.close()
Expand All @@ -344,7 +347,7 @@ function start(cliOptions = {}) { // eslint-disable-line complexity
}
}

return {webpackConfig, options, server}
return result
}

function getFilenames(options) {
Expand Down
22 changes: 18 additions & 4 deletions lib/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,14 @@ module.exports = function (webpackConfig, options) {
console.log(webpackConfig)
}

const result = {
webpackConfig,
options
}

if (typeof options.run === 'function') {
return options.run(webpackConfig)
options.run(webpackConfig)
return result
}

let compiler
Expand All @@ -26,33 +32,41 @@ module.exports = function (webpackConfig, options) {
throw err
}
}
result.compiler = compiler

if (options.watch) {
// watch mode
console.log('> Running in watch mode')
rm(path.join(options.dist, '*'))
compiler.watch({}, (err, stats) => handleBuild(err, stats, true))

result.watcher = compiler.watch({}, (err, stats) => handleBuild(err, stats, true))
} else if (options.dev) {
// dev mode
const server = createServer(compiler, options)

server.listen(options.port, options.host)
if (options.open) {
require('opn')(`http://${options.host}:${options.port}`)
}
return server
result.server = server
} else {
// default production mode
console.log('> Creating an optimized production build:\n')
// remove dist files but keep that folder in production mode
rm(path.join(options.dist, '*'))
compiler.run(handleBuild)
}

return result

function handleBuild(err, stats, watch) {
if (err) {
throw new AppError(err.stack)
}
if (stats.hasErrors() || stats.hasWarnings()) {
const failureMessage = `\n\n${chalk.bgRed.black(' ERROR ')} Compiling failed!\n`
throw new AppError(stats.toString('errors-only') + failureMessage)
const msg = stats.toString('errors-only') + failureMessage
return console.error(msg)
}
console.log(stats.toString(options.stats))
console.log(`\n${chalk.bgGreen.black(' DONE ')} Compiled successfully!\n`)
Expand Down

0 comments on commit b87e8d9

Please sign in to comment.