Skip to content

Commit

Permalink
Make it work with vue-play (vuejs#335)
Browse files Browse the repository at this point in the history
* add hmrEntries

* read cli config

* add hmr client after updating webpack config

* allow to override webpack entry with options.entry

* update docs
  • Loading branch information
egoist authored Feb 5, 2017
1 parent 7f579ca commit bed3913
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 15 deletions.
44 changes: 31 additions & 13 deletions bin/vue-build
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ var options = merge({
host: 'localhost'
}, localConfig, {
entry: args[0],
config: program.config,
port: program.port,
host: program.host,
open: program.open,
Expand All @@ -81,7 +82,7 @@ var options = merge({
})

function help () {
if (!options.run && !options.entry) {
if (!options.config && !options.entry) {
return program.help()
}
}
Expand Down Expand Up @@ -218,6 +219,10 @@ if (options.mount === undefined && !options.lib && /\.vue$/.test(options.entry))
if (options.mount) {
webpackConfig.entry.client.push(ownDir('lib/default-entry.es6'))
webpackConfig.resolve.alias['your-tasteful-component'] = cwd(options.entry)
} else if (Array.isArray(options.entry)) {
webpackConfig.entry.client = options.client
} else if (typeof options.entry === 'object') {
webpackConfig.entry = options.entry
} else {
webpackConfig.entry.client.push(options.entry)
}
Expand All @@ -232,12 +237,16 @@ if (options.lib) {
webpackConfig.output.libraryTarget = 'umd'
} else {
// only output index.html in non-lib mode
webpackConfig.plugins.unshift(
new HtmlWebpackPlugin(Object.assign({
title: 'Vue App',
template: ownDir('lib/template.html')
}, options.html))
)
var html = Array.isArray(options.html) ? options.html : [options.html || {}]

html.forEach(item => {
webpackConfig.plugins.unshift(
new HtmlWebpackPlugin(Object.assign({
title: 'Vue App',
template: ownDir('lib/template.html')
}, item))
)
})
}

// installed by `yarn global add`
Expand Down Expand Up @@ -282,10 +291,6 @@ if (production) {
}))
}
} else {
if (!options.watch) {
webpackConfig.plugins.push(new webpack.HotModuleReplacementPlugin())
webpackConfig.entry.client.unshift(require.resolve('webpack-hot-middleware/client') + `?reload=true&path=http://${options.host}:${options.port}/__webpack_hmr`)
}
webpackConfig.devtool = 'eval-source-map'
webpackConfig.plugins.push(
new FriendlyErrorsPlugin(),
Expand Down Expand Up @@ -316,8 +321,21 @@ if (!options.disableWebpackConfig) {
}
}

// only check entry when there's no custom `run` process
if (!options.run && !fs.existsSync(options.entry)) {
if (!options.watch && !options.production) {
webpackConfig.plugins.push(new webpack.HotModuleReplacementPlugin())
var hmrEntries = options.hmrEntries || ['client']
var hmrClient = require.resolve('webpack-hot-middleware/client') + `?reload=true&path=http://${options.host}:${options.port}/__webpack_hmr`
hmrEntries.forEach(name => {
if (Array.isArray(webpackConfig.entry[name])) {
webpackConfig.entry[name].unshift(hmrClient)
} else {
webpackConfig.entry[name] = [hmrClient, webpackConfig.entry[name]]
}
})
}

// only check entry when there's no custom config
if (!options.config && !fs.existsSync(options.entry)) {
logger.fatal(`${chalk.yellow(options.entry)} does not exist, did you forget to create one?`)
}

Expand Down
17 changes: 15 additions & 2 deletions docs/build.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,16 @@ You can define CLI options in this file.

#### entry

Type: `string`
Type: `string` `Array` `Object`

It's the first argument of `vue build` command, eg: `vue build entry.js`. You can set it here to omit it in CLI arguments.

The single-component mode (`--mount`) will not work if you set `entry` to an `Array` or `Object`.

- `Array`: Override `webpackConfig.entry.client`
- `Object`: Override `webpackConfig.entry`
- `string`: Added to `webpackConfig.entry.client` or used as `webpackConfig.resolve.alias['your-tasteful-component']` in single-component mode.

#### port

Type: `number`<br>
Expand Down Expand Up @@ -143,7 +149,7 @@ PostCSS options, if it's an `Array` or `Function`, the default value will be ove

#### html

Type: `Object`
Type: `Object` `Array`

[html-webpack-plugin](https://github.com/ampedandwired/html-webpack-plugin) options, use this option to customize `index.html` output, default value:

Expand Down Expand Up @@ -176,6 +182,13 @@ Type: `boolean`

In production mode, all generated files will be compressed and produce sourcemaps file. You can use `--disableCompress` to disable this behavior.

#### hmrEntries

Type: `Array`<br>
Default: `['client']`

Add `webpack-hot-middleware` HMR client to specific webpack entries. By default your app is loaded in `client` entry, so we insert it here.

#### proxy

Type: `string`, `Object`
Expand Down

0 comments on commit bed3913

Please sign in to comment.