Skip to content

Commit

Permalink
fix(cli-service): make devBaseUrl work properly in serve command (#1405)
Browse files Browse the repository at this point in the history
  • Loading branch information
jkzing authored and yyx990803 committed May 30, 2018
1 parent 7ce91c8 commit 04600e6
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 8 deletions.
13 changes: 10 additions & 3 deletions packages/@vue/cli-service/lib/commands/serve.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ module.exports = (api, options) => {
// are running it in a mode with a production env, e.g. in E2E tests.
const isProduction = process.env.NODE_ENV === 'production'

const path = require('path')
const chalk = require('chalk')
const webpack = require('webpack')
const WebpackDevServer = require('webpack-dev-server')
Expand Down Expand Up @@ -63,7 +64,10 @@ module.exports = (api, options) => {
if (!isProduction) {
const devClients = [
// dev server client
require.resolve(`webpack-dev-server/client`),
require.resolve(`webpack-dev-server/client`) +
// fix webpack-dev-server socket url to /sockjs-node
// in case it uses options.devBaseUrl
'?/sockjs-node',

This comment has been minimized.

Copy link
@ryanrocket

ryanrocket Aug 2, 2019

removing '?/sockjs-node' prevent 404 error in vue application when requesting sockjs from webpack.

// hmr client
require.resolve(projectDevServerOptions.hotOnly
? 'webpack/hot/only-dev-server'
Expand Down Expand Up @@ -102,14 +106,17 @@ module.exports = (api, options) => {
const server = new WebpackDevServer(compiler, Object.assign({
clientLogLevel: 'none',
historyApiFallback: {
disableDotRule: true
disableDotRule: true,
rewrites: [
{ from: /./, to: path.posix.join(options.devBaseUrl, 'index.html') }
]
},
contentBase: api.resolve('public'),
watchContentBase: !isProduction,
hot: !isProduction,
quiet: true,
compress: isProduction,
publicPath: '/',
publicPath: options.devBaseUrl,
overlay: isProduction // TODO disable this
? false
: { warnings: false, errors: true }
Expand Down
2 changes: 1 addition & 1 deletion packages/@vue/cli-service/lib/config/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ module.exports = (api, options) => {
files: assets,
options: pluginOptions
}
}, resolveClientEnv(options.baseUrl, true /* raw */))
}, resolveClientEnv(options, true /* raw */))
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/@vue/cli-service/lib/config/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ module.exports = (api, options) => {
webpackConfig
.plugin('define')
.use(require('webpack/lib/DefinePlugin'), [
resolveClientEnv(options.baseUrl)
resolveClientEnv(options)
])

webpackConfig
Expand Down
2 changes: 1 addition & 1 deletion packages/@vue/cli-service/lib/config/dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module.exports = (api, options) => {
webpackConfig
.devtool('cheap-module-eval-source-map')
.output
.publicPath(options.devBaseUrl || '/')
.publicPath(options.devBaseUrl)

webpackConfig
.plugin('hmr')
Expand Down
3 changes: 3 additions & 0 deletions packages/@vue/cli-service/lib/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ exports.defaults = () => ({
// project deployment base
baseUrl: '/',

// baseUrl, but for the dev server.
devBaseUrl: '/',

// where to output built files
outputDir: 'dist',

Expand Down
5 changes: 3 additions & 2 deletions packages/@vue/cli-service/lib/util/resolveClientEnv.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
const prefixRE = /^VUE_APP_/

module.exports = function resolveClientEnv (publicPath, raw) {
module.exports = function resolveClientEnv (options, raw) {
const isProd = process.env.NODE_ENV === 'production'
const env = {}
Object.keys(process.env).forEach(key => {
if (prefixRE.test(key) || key === 'NODE_ENV') {
env[key] = process.env[key]
}
})
env.BASE_URL = publicPath
env.BASE_URL = isProd ? options.baseUrl : options.devBaseUrl

if (raw) {
return env
Expand Down

0 comments on commit 04600e6

Please sign in to comment.