Skip to content

Commit 1719622

Browse files
committed
feat: make public dir optional
close #1265
1 parent 9a4159d commit 1719622

File tree

3 files changed

+45
-28
lines changed

3 files changed

+45
-28
lines changed

packages/@vue/cli-service/lib/commands/build/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ module.exports = (api, options) => {
5959
// respect inline build destination in copy plugin
6060
if (args.dest) {
6161
api.chainWebpack(config => {
62-
if (args.target === 'app') {
62+
if (config.plugins.has('copy')) {
6363
config.plugin('copy').tap(args => {
6464
args[0][0].to = targetDir
6565
return args

packages/@vue/cli-service/lib/config/app.js

+32-27
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
// config that are specific to --target app
2+
const fs = require('fs')
3+
const path = require('path')
24

35
module.exports = (api, options) => {
46
api.chainWebpack(webpackConfig => {
@@ -7,9 +9,16 @@ module.exports = (api, options) => {
79
return
810
}
911

12+
const isProd = process.env.NODE_ENV === 'production'
13+
1014
// HTML plugin
1115
const resolveClientEnv = require('../util/resolveClientEnv')
16+
const htmlPath = api.resolve('public/index.html')
1217
const htmlOptions = {
18+
// use default index.html
19+
template: fs.existsSync(htmlPath)
20+
? htmlPath
21+
: path.resolve(__dirname, 'index-default.html'),
1322
templateParameters: (compilation, assets, pluginOptions) => {
1423
// enhance html-webpack-plugin's built in template params
1524
let stats
@@ -27,10 +36,19 @@ module.exports = (api, options) => {
2736
}, resolveClientEnv(options.baseUrl, true /* raw */))
2837
}
2938
}
30-
// only set template path if index.html exists
31-
const htmlPath = api.resolve('public/index.html')
32-
if (require('fs').existsSync(htmlPath)) {
33-
htmlOptions.template = htmlPath
39+
40+
if (isProd) {
41+
Object.assign(htmlOptions, {
42+
minify: {
43+
removeComments: true,
44+
collapseWhitespace: true,
45+
removeAttributeQuotes: true
46+
// more options:
47+
// https://github.com/kangax/html-minifier#options-quick-reference
48+
},
49+
// necessary to consistently work with multiple chunks via CommonsChunkPlugin
50+
chunksSortMode: 'dependency'
51+
})
3452
}
3553

3654
webpackConfig
@@ -55,31 +73,18 @@ module.exports = (api, options) => {
5573
}])
5674

5775
// copy static assets in public/
58-
webpackConfig
59-
.plugin('copy')
60-
.use(require('copy-webpack-plugin'), [[{
61-
from: api.resolve('public'),
62-
to: api.resolve(options.outputDir),
63-
ignore: ['index.html', '.DS_Store']
64-
}]])
65-
66-
if (process.env.NODE_ENV === 'production') {
67-
// minify HTML
76+
if (fs.existsSync(api.resolve('public'))) {
6877
webpackConfig
69-
.plugin('html')
70-
.tap(([options]) => [Object.assign(options, {
71-
minify: {
72-
removeComments: true,
73-
collapseWhitespace: true,
74-
removeAttributeQuotes: true
75-
// more options:
76-
// https://github.com/kangax/html-minifier#options-quick-reference
77-
},
78-
// necessary to consistently work with multiple chunks via CommonsChunkPlugin
79-
chunksSortMode: 'dependency'
80-
})])
78+
.plugin('copy')
79+
.use(require('copy-webpack-plugin'), [[{
80+
from: api.resolve('public'),
81+
to: api.resolve(options.outputDir),
82+
ignore: ['index.html', '.DS_Store']
83+
}]])
84+
}
8185

82-
// code splitting
86+
// code splitting
87+
if (isProd) {
8388
webpackConfig
8489
.optimization.splitChunks({
8590
chunks: 'all'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
6+
<meta name="viewport" content="width=device-width,initial-scale=1.0">
7+
<title>Vue App</title>
8+
</head>
9+
<body>
10+
<div id="app"></div>
11+
</body>
12+
</html>

0 commit comments

Comments
 (0)