Skip to content

Commit 2953e22

Browse files
committed
refactor: validate webpack config in serve as well + warn publicPath config error
1 parent ba75e29 commit 2953e22

File tree

3 files changed

+46
-25
lines changed

3 files changed

+46
-25
lines changed

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

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ async function build (args, api, options) {
8181
const chalk = require('chalk')
8282
const webpack = require('webpack')
8383
const formatStats = require('./formatStats')
84+
const validateWebpackConfig = require('../../util/validateWebpackConfig')
8485
const {
8586
log,
8687
done,
@@ -123,6 +124,9 @@ async function build (args, api, options) {
123124
webpackConfig = require('./resolveAppConfig')(api, args, options)
124125
}
125126

127+
// check for common config errors
128+
validateWebpackConfig(webpackConfig, api, options, args.target)
129+
126130
// apply inline dest path after user configureWebpack hooks
127131
// so it takes higher priority
128132
if (args.dest) {
@@ -131,31 +135,6 @@ async function build (args, api, options) {
131135
})
132136
}
133137

134-
// grab the actual output path and check for common mis-configuration
135-
const actualTargetDir = (
136-
Array.isArray(webpackConfig)
137-
? webpackConfig[0]
138-
: webpackConfig
139-
).output.path
140-
141-
if (!args.dest && actualTargetDir !== api.resolve(options.outputDir)) {
142-
// user directly modifies output.path in configureWebpack or chainWebpack.
143-
// this is not supported because there's no way for us to give copy
144-
// plugin the correct value this way.
145-
throw new Error(
146-
`\n\nConfiguration Error: ` +
147-
`Avoid modifying webpack output.path directly. ` +
148-
`Use the "outputDir" option instead.\n`
149-
)
150-
}
151-
152-
if (actualTargetDir === api.service.context) {
153-
throw new Error(
154-
`\n\nConfiguration Error: ` +
155-
`Do not set output directory to project root.\n`
156-
)
157-
}
158-
159138
if (args.watch) {
160139
modifyConfig(webpackConfig, config => {
161140
config.watch = true

packages/@vue/cli-service/lib/commands/serve.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,14 @@ module.exports = (api, options) => {
4141
const prepareURLs = require('../util/prepareURLs')
4242
const prepareProxy = require('../util/prepareProxy')
4343
const launchEditorMiddleware = require('launch-editor-middleware')
44+
const validateWebpackConfig = require('../util/validateWebpackConfig')
4445

4546
// resolve webpack config
4647
const webpackConfig = api.resolveWebpackConfig()
4748

49+
// check for common config errors
50+
validateWebpackConfig(webpackConfig, api, options)
51+
4852
// load user devServer options with higher priority than devServer
4953
// in webpck config
5054
const projectDevServerOptions = Object.assign(
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
module.exports = function validateWebpackConfig (
2+
webpackConfig,
3+
api,
4+
options,
5+
target = 'app'
6+
) {
7+
const singleConfig = Array.isArray(webpackConfig)
8+
? webpackConfig[0]
9+
: webpackConfig
10+
11+
const actualTargetDir = singleConfig.output.path
12+
13+
if (actualTargetDir !== api.resolve(options.outputDir)) {
14+
// user directly modifies output.path in configureWebpack or chainWebpack.
15+
// this is not supported because there's no way for us to give copy
16+
// plugin the correct value this way.
17+
throw new Error(
18+
`\n\nConfiguration Error: ` +
19+
`Avoid modifying webpack output.path directly. ` +
20+
`Use the "outputDir" option instead.\n`
21+
)
22+
}
23+
24+
if (actualTargetDir === api.service.context) {
25+
throw new Error(
26+
`\n\nConfiguration Error: ` +
27+
`Do not set output directory to project root.\n`
28+
)
29+
}
30+
31+
if (target === 'app' && singleConfig.output.publicPath !== options.baseUrl) {
32+
throw new Error(
33+
`\n\nConfiguration Error: ` +
34+
`Avoid modifying webpack output.publicPath directly. ` +
35+
`Use the "baseUrl" option instead.\n`
36+
)
37+
}
38+
}

0 commit comments

Comments
 (0)