Skip to content

Commit

Permalink
feat: make public dir optional
Browse files Browse the repository at this point in the history
close #1265
  • Loading branch information
yyx990803 committed May 28, 2018
1 parent 9a4159d commit 1719622
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 28 deletions.
2 changes: 1 addition & 1 deletion packages/@vue/cli-service/lib/commands/build/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
59 changes: 32 additions & 27 deletions packages/@vue/cli-service/lib/config/app.js
Original file line number Diff line number Diff line change
@@ -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 => {
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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'
Expand Down
12 changes: 12 additions & 0 deletions packages/@vue/cli-service/lib/config/index-default.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<title>Vue App</title>
</head>
<body>
<div id="app"></div>
</body>
</html>

0 comments on commit 1719622

Please sign in to comment.