Skip to content

Commit bed3913

Browse files
authored
Make it work with vue-play (#335)
* add hmrEntries * read cli config * add hmr client after updating webpack config * allow to override webpack entry with options.entry * update docs
1 parent 7f579ca commit bed3913

File tree

2 files changed

+46
-15
lines changed

2 files changed

+46
-15
lines changed

bin/vue-build

+31-13
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ var options = merge({
6666
host: 'localhost'
6767
}, localConfig, {
6868
entry: args[0],
69+
config: program.config,
6970
port: program.port,
7071
host: program.host,
7172
open: program.open,
@@ -81,7 +82,7 @@ var options = merge({
8182
})
8283

8384
function help () {
84-
if (!options.run && !options.entry) {
85+
if (!options.config && !options.entry) {
8586
return program.help()
8687
}
8788
}
@@ -218,6 +219,10 @@ if (options.mount === undefined && !options.lib && /\.vue$/.test(options.entry))
218219
if (options.mount) {
219220
webpackConfig.entry.client.push(ownDir('lib/default-entry.es6'))
220221
webpackConfig.resolve.alias['your-tasteful-component'] = cwd(options.entry)
222+
} else if (Array.isArray(options.entry)) {
223+
webpackConfig.entry.client = options.client
224+
} else if (typeof options.entry === 'object') {
225+
webpackConfig.entry = options.entry
221226
} else {
222227
webpackConfig.entry.client.push(options.entry)
223228
}
@@ -232,12 +237,16 @@ if (options.lib) {
232237
webpackConfig.output.libraryTarget = 'umd'
233238
} else {
234239
// only output index.html in non-lib mode
235-
webpackConfig.plugins.unshift(
236-
new HtmlWebpackPlugin(Object.assign({
237-
title: 'Vue App',
238-
template: ownDir('lib/template.html')
239-
}, options.html))
240-
)
240+
var html = Array.isArray(options.html) ? options.html : [options.html || {}]
241+
242+
html.forEach(item => {
243+
webpackConfig.plugins.unshift(
244+
new HtmlWebpackPlugin(Object.assign({
245+
title: 'Vue App',
246+
template: ownDir('lib/template.html')
247+
}, item))
248+
)
249+
})
241250
}
242251

243252
// installed by `yarn global add`
@@ -282,10 +291,6 @@ if (production) {
282291
}))
283292
}
284293
} else {
285-
if (!options.watch) {
286-
webpackConfig.plugins.push(new webpack.HotModuleReplacementPlugin())
287-
webpackConfig.entry.client.unshift(require.resolve('webpack-hot-middleware/client') + `?reload=true&path=http://${options.host}:${options.port}/__webpack_hmr`)
288-
}
289294
webpackConfig.devtool = 'eval-source-map'
290295
webpackConfig.plugins.push(
291296
new FriendlyErrorsPlugin(),
@@ -316,8 +321,21 @@ if (!options.disableWebpackConfig) {
316321
}
317322
}
318323

319-
// only check entry when there's no custom `run` process
320-
if (!options.run && !fs.existsSync(options.entry)) {
324+
if (!options.watch && !options.production) {
325+
webpackConfig.plugins.push(new webpack.HotModuleReplacementPlugin())
326+
var hmrEntries = options.hmrEntries || ['client']
327+
var hmrClient = require.resolve('webpack-hot-middleware/client') + `?reload=true&path=http://${options.host}:${options.port}/__webpack_hmr`
328+
hmrEntries.forEach(name => {
329+
if (Array.isArray(webpackConfig.entry[name])) {
330+
webpackConfig.entry[name].unshift(hmrClient)
331+
} else {
332+
webpackConfig.entry[name] = [hmrClient, webpackConfig.entry[name]]
333+
}
334+
})
335+
}
336+
337+
// only check entry when there's no custom config
338+
if (!options.config && !fs.existsSync(options.entry)) {
321339
logger.fatal(`${chalk.yellow(options.entry)} does not exist, did you forget to create one?`)
322340
}
323341

docs/build.md

+15-2
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,16 @@ You can define CLI options in this file.
8080

8181
#### entry
8282

83-
Type: `string`
83+
Type: `string` `Array` `Object`
8484

8585
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.
8686

87+
The single-component mode (`--mount`) will not work if you set `entry` to an `Array` or `Object`.
88+
89+
- `Array`: Override `webpackConfig.entry.client`
90+
- `Object`: Override `webpackConfig.entry`
91+
- `string`: Added to `webpackConfig.entry.client` or used as `webpackConfig.resolve.alias['your-tasteful-component']` in single-component mode.
92+
8793
#### port
8894

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

144150
#### html
145151

146-
Type: `Object`
152+
Type: `Object` `Array`
147153

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

@@ -176,6 +182,13 @@ Type: `boolean`
176182

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

185+
#### hmrEntries
186+
187+
Type: `Array`<br>
188+
Default: `['client']`
189+
190+
Add `webpack-hot-middleware` HMR client to specific webpack entries. By default your app is loaded in `client` entry, so we insert it here.
191+
179192
#### proxy
180193

181194
Type: `string`, `Object`

0 commit comments

Comments
 (0)