Skip to content

Commit 4edc9bd

Browse files
authored
Add watch mode (#322)
1 parent 29f3178 commit 4edc9bd

File tree

2 files changed

+50
-34
lines changed

2 files changed

+50
-34
lines changed

bin/vue-build

Lines changed: 41 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,10 @@ program
2929
.option('--port [port]', 'Server port')
3030
.option('--host [host]', 'Server host', 'localhost')
3131
.option('--prod, --production', 'Build for production')
32+
.option('-w, --watch', 'Run in watch mode')
3233
.option('-m, --mount', 'Auto-create app instance from given component, true for `.vue` file')
3334
.option('-c, --config [file]', 'Use custom config file')
34-
.option('-w, --webpack [file]', 'Use custom webpack config file')
35+
.option('--wp, --webpack [file]', 'Use custom webpack config file')
3536
.option('--disable-config', 'You do not want to use config file')
3637
.option('--disable-webpack-config', 'You do not want to use webpack config file')
3738
.option('-o, --open', 'Open browser')
@@ -73,7 +74,8 @@ var options = merge({
7374
mount: program.mount,
7475
proxy: program.proxy,
7576
production: program.production,
76-
lib: program.lib
77+
lib: program.lib,
78+
watch: program.watch
7779
})
7880

7981
function help () {
@@ -254,12 +256,12 @@ if (production) {
254256
new ExtractTextPlugin(options.lib ? `${replaceExtension(options.entry, '.css')}` : '[name].[contenthash:8].css')
255257
)
256258
} else {
259+
if (!options.watch) {
260+
webpackConfig.plugins.push(new webpack.HotModuleReplacementPlugin())
261+
webpackConfig.entry.client.unshift(require.resolve('webpack-hot-middleware/client') + '?reload=true')
262+
}
257263
webpackConfig.devtool = 'eval-source-map'
258-
webpackConfig.entry.client.unshift(
259-
require.resolve('webpack-hot-middleware/client') + '?reload=true'
260-
)
261264
webpackConfig.plugins.push(
262-
new webpack.HotModuleReplacementPlugin(),
263265
new FriendlyErrorsPlugin(),
264266
new PostCompilePlugin(() => {
265267
console.log(`> Open http://${options.host}:${options.port}`)
@@ -300,36 +302,15 @@ try {
300302

301303
checkEntryExists(options.entry)
302304

303-
if (production) {
305+
if (options.watch) {
306+
console.log('> Running in watch mode')
307+
rm(path.join(options.dist, '*'))
308+
compiler.watch({}, (err, stats) => handleBuild(err, stats, true))
309+
} else if (production) {
304310
console.log('> Creating an optimized production build:\n')
305311
// remove dist files but keep that folder in production mode
306312
rm(path.join(options.dist, '*'))
307-
compiler.run((err, stats) => {
308-
if (err) {
309-
process.exitCode = 1
310-
return console.error(err.stack)
311-
}
312-
// output warning
313-
if (stats.hasWarnings() && !stats.hasErrors()) {
314-
console.error(stats.toString('errors-only'))
315-
}
316-
// output wanring and errors
317-
if (stats.hasErrors()) {
318-
process.exitCode = 1
319-
return console.error(stats.toString('errors-only'))
320-
}
321-
console.log(stats.toString({
322-
chunks: false,
323-
children: false,
324-
modules: false,
325-
colors: true
326-
}))
327-
console.log(`\n${chalk.bgGreen.black(' SUCCESS ')} Compiled successfully.\n`)
328-
console.log(`The ${chalk.cyan(options.dist)} folder is ready to be deployed.`)
329-
console.log(`You may also serve it locally with a static server:\n`)
330-
console.log(` ${chalk.yellow('npm')} i -g serve`)
331-
console.log(` ${chalk.yellow('serve')} ${options.dist}\n`)
332-
})
313+
compiler.run(handleBuild)
333314
} else {
334315
var server = createServer(compiler, options)
335316

@@ -369,3 +350,30 @@ function replaceExtension (entry, ext) {
369350
function getLibraryName (fileName) {
370351
return fileName.replace(/[-_.]([\w])/, (_, p1) => p1.toUpperCase())
371352
}
353+
354+
function handleBuild (err, stats, watch) {
355+
if (watch) {
356+
process.stdout.write('\x1Bc')
357+
}
358+
if (err) {
359+
process.exitCode = 1
360+
return console.error(err.stack)
361+
}
362+
if (stats.hasErrors() || stats.hasWarnings()) {
363+
process.exitCode = 1
364+
return console.error(stats.toString('errors-only'))
365+
}
366+
console.log(stats.toString({
367+
chunks: false,
368+
children: false,
369+
modules: false,
370+
colors: true
371+
}))
372+
console.log(`\n${chalk.bgGreen.black(' SUCCESS ')} Compiled successfully.\n`)
373+
if (!watch) {
374+
console.log(`The ${chalk.cyan(options.dist)} folder is ready to be deployed.`)
375+
console.log(`You may also serve it locally with a static server:\n`)
376+
console.log(` ${chalk.yellow('npm')} i -g serve`)
377+
console.log(` ${chalk.yellow('serve')} ${options.dist}\n`)
378+
}
379+
}

docs/build.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,15 @@ This will create an optimized bundle in UMD format, and the name of exported lib
5252

5353
Note that in some cases you may use [`externals`](https://webpack.js.org/configuration/externals/) to exclude some modules from your bundle.
5454

55-
For more CLI usages:
55+
**Watch mode:**
56+
57+
```bash
58+
$ vue build index.js --watch
59+
```
60+
61+
It's similar to `development mode` but does not add hot-reloading support and uses a real file system.
62+
63+
**For more CLI usages:**
5664

5765
```bash
5866
$ vue build -h

0 commit comments

Comments
 (0)