Skip to content

Commit 04600e6

Browse files
jkzingyyx990803
authored andcommittedMay 30, 2018
fix(cli-service): make devBaseUrl work properly in serve command (#1405)
1 parent 7ce91c8 commit 04600e6

File tree

6 files changed

+19
-8
lines changed

6 files changed

+19
-8
lines changed
 

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

+10-3
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ module.exports = (api, options) => {
2929
// are running it in a mode with a production env, e.g. in E2E tests.
3030
const isProduction = process.env.NODE_ENV === 'production'
3131

32+
const path = require('path')
3233
const chalk = require('chalk')
3334
const webpack = require('webpack')
3435
const WebpackDevServer = require('webpack-dev-server')
@@ -63,7 +64,10 @@ module.exports = (api, options) => {
6364
if (!isProduction) {
6465
const devClients = [
6566
// dev server client
66-
require.resolve(`webpack-dev-server/client`),
67+
require.resolve(`webpack-dev-server/client`) +
68+
// fix webpack-dev-server socket url to /sockjs-node
69+
// in case it uses options.devBaseUrl
70+
'?/sockjs-node',
Has a conversation. Original line has a conversation.
6771
// hmr client
6872
require.resolve(projectDevServerOptions.hotOnly
6973
? 'webpack/hot/only-dev-server'
@@ -102,14 +106,17 @@ module.exports = (api, options) => {
102106
const server = new WebpackDevServer(compiler, Object.assign({
103107
clientLogLevel: 'none',
104108
historyApiFallback: {
105-
disableDotRule: true
109+
disableDotRule: true,
110+
rewrites: [
111+
{ from: /./, to: path.posix.join(options.devBaseUrl, 'index.html') }
112+
]
106113
},
107114
contentBase: api.resolve('public'),
108115
watchContentBase: !isProduction,
109116
hot: !isProduction,
110117
quiet: true,
111118
compress: isProduction,
112-
publicPath: '/',
119+
publicPath: options.devBaseUrl,
113120
overlay: isProduction // TODO disable this
114121
? false
115122
: { warnings: false, errors: true }

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ module.exports = (api, options) => {
2929
files: assets,
3030
options: pluginOptions
3131
}
32-
}, resolveClientEnv(options.baseUrl, true /* raw */))
32+
}, resolveClientEnv(options, true /* raw */))
3333
}
3434
}
3535

‎packages/@vue/cli-service/lib/config/base.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ module.exports = (api, options) => {
147147
webpackConfig
148148
.plugin('define')
149149
.use(require('webpack/lib/DefinePlugin'), [
150-
resolveClientEnv(options.baseUrl)
150+
resolveClientEnv(options)
151151
])
152152

153153
webpackConfig

‎packages/@vue/cli-service/lib/config/dev.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ module.exports = (api, options) => {
77
webpackConfig
88
.devtool('cheap-module-eval-source-map')
99
.output
10-
.publicPath(options.devBaseUrl || '/')
10+
.publicPath(options.devBaseUrl)
1111

1212
webpackConfig
1313
.plugin('hmr')

‎packages/@vue/cli-service/lib/options.js

+3
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ exports.defaults = () => ({
4848
// project deployment base
4949
baseUrl: '/',
5050

51+
// baseUrl, but for the dev server.
52+
devBaseUrl: '/',
53+
5154
// where to output built files
5255
outputDir: 'dist',
5356

‎packages/@vue/cli-service/lib/util/resolveClientEnv.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
const prefixRE = /^VUE_APP_/
22

3-
module.exports = function resolveClientEnv (publicPath, raw) {
3+
module.exports = function resolveClientEnv (options, raw) {
4+
const isProd = process.env.NODE_ENV === 'production'
45
const env = {}
56
Object.keys(process.env).forEach(key => {
67
if (prefixRE.test(key) || key === 'NODE_ENV') {
78
env[key] = process.env[key]
89
}
910
})
10-
env.BASE_URL = publicPath
11+
env.BASE_URL = isProd ? options.baseUrl : options.devBaseUrl
1112

1213
if (raw) {
1314
return env

0 commit comments

Comments
 (0)
Please sign in to comment.