From 1719622494a1b1270032f533f625874f0ac5a3bf Mon Sep 17 00:00:00 2001 From: Evan You Date: Mon, 28 May 2018 14:33:45 -0400 Subject: [PATCH] feat: make public dir optional close #1265 --- .../cli-service/lib/commands/build/index.js | 2 +- packages/@vue/cli-service/lib/config/app.js | 59 ++++++++++--------- .../cli-service/lib/config/index-default.html | 12 ++++ 3 files changed, 45 insertions(+), 28 deletions(-) create mode 100644 packages/@vue/cli-service/lib/config/index-default.html diff --git a/packages/@vue/cli-service/lib/commands/build/index.js b/packages/@vue/cli-service/lib/commands/build/index.js index c4fab14408..b23326bd54 100644 --- a/packages/@vue/cli-service/lib/commands/build/index.js +++ b/packages/@vue/cli-service/lib/commands/build/index.js @@ -59,7 +59,7 @@ module.exports = (api, options) => { // respect inline build destination in copy plugin if (args.dest) { api.chainWebpack(config => { - if (args.target === 'app') { + if (config.plugins.has('copy')) { config.plugin('copy').tap(args => { args[0][0].to = targetDir return args diff --git a/packages/@vue/cli-service/lib/config/app.js b/packages/@vue/cli-service/lib/config/app.js index 8fd4477777..3f1264f200 100644 --- a/packages/@vue/cli-service/lib/config/app.js +++ b/packages/@vue/cli-service/lib/config/app.js @@ -1,4 +1,6 @@ // config that are specific to --target app +const fs = require('fs') +const path = require('path') module.exports = (api, options) => { api.chainWebpack(webpackConfig => { @@ -7,9 +9,16 @@ module.exports = (api, options) => { return } + const isProd = process.env.NODE_ENV === 'production' + // HTML plugin const resolveClientEnv = require('../util/resolveClientEnv') + const htmlPath = api.resolve('public/index.html') const htmlOptions = { + // use default index.html + template: fs.existsSync(htmlPath) + ? htmlPath + : path.resolve(__dirname, 'index-default.html'), templateParameters: (compilation, assets, pluginOptions) => { // enhance html-webpack-plugin's built in template params let stats @@ -27,10 +36,19 @@ module.exports = (api, options) => { }, resolveClientEnv(options.baseUrl, true /* raw */)) } } - // only set template path if index.html exists - const htmlPath = api.resolve('public/index.html') - if (require('fs').existsSync(htmlPath)) { - htmlOptions.template = htmlPath + + if (isProd) { + Object.assign(htmlOptions, { + minify: { + removeComments: true, + collapseWhitespace: true, + removeAttributeQuotes: true + // more options: + // https://github.com/kangax/html-minifier#options-quick-reference + }, + // necessary to consistently work with multiple chunks via CommonsChunkPlugin + chunksSortMode: 'dependency' + }) } webpackConfig @@ -55,31 +73,18 @@ module.exports = (api, options) => { }]) // copy static assets in public/ - webpackConfig - .plugin('copy') - .use(require('copy-webpack-plugin'), [[{ - from: api.resolve('public'), - to: api.resolve(options.outputDir), - ignore: ['index.html', '.DS_Store'] - }]]) - - if (process.env.NODE_ENV === 'production') { - // minify HTML + if (fs.existsSync(api.resolve('public'))) { webpackConfig - .plugin('html') - .tap(([options]) => [Object.assign(options, { - minify: { - removeComments: true, - collapseWhitespace: true, - removeAttributeQuotes: true - // more options: - // https://github.com/kangax/html-minifier#options-quick-reference - }, - // necessary to consistently work with multiple chunks via CommonsChunkPlugin - chunksSortMode: 'dependency' - })]) + .plugin('copy') + .use(require('copy-webpack-plugin'), [[{ + from: api.resolve('public'), + to: api.resolve(options.outputDir), + ignore: ['index.html', '.DS_Store'] + }]]) + } - // code splitting + // code splitting + if (isProd) { webpackConfig .optimization.splitChunks({ chunks: 'all' diff --git a/packages/@vue/cli-service/lib/config/index-default.html b/packages/@vue/cli-service/lib/config/index-default.html new file mode 100644 index 0000000000..73453d6011 --- /dev/null +++ b/packages/@vue/cli-service/lib/config/index-default.html @@ -0,0 +1,12 @@ + + + + + + + Vue App + + +
+ +