Skip to content

Commit dc38211

Browse files
committed
feat: allow using relative baseUrl
1 parent bfebc6d commit dc38211

File tree

5 files changed

+22
-6
lines changed

5 files changed

+22
-6
lines changed

packages/@vue/cli-service/lib/Service.js

+1
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ module.exports = class Service {
248248
}
249249

250250
// normlaize some options
251+
resolved.baseUrl = resolved.baseUrl.replace(/^\.\//, '')
251252
ensureSlash(resolved, 'baseUrl')
252253
removeSlash(resolved, 'outputDir')
253254

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

+7-2
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,14 @@ module.exports = (api, options) => {
2424
const shadowMode = !!process.env.VUE_CLI_CSS_SHADOW_MODE
2525
const isProd = process.env.NODE_ENV === 'production'
2626
const shouldExtract = isProd && extract !== false && !shadowMode
27+
const filename = getAssetPath(
28+
options,
29+
`css/[name].[contenthash:8].css`,
30+
true /* placeAtRootIfRelative */
31+
)
2732
const extractOptions = Object.assign({
28-
filename: getAssetPath(options, `css/[name].[contenthash:8].css`),
29-
chunkFilename: getAssetPath(options, 'css/[name].[contenthash:8].css')
33+
filename,
34+
chunkFilename: filename
3035
}, extract && typeof extract === 'object' ? extract : {})
3136

3237
// check if the project has a valid postcss config

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

+7-2
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,18 @@ module.exports = (api, options) => {
22
api.chainWebpack(webpackConfig => {
33
if (process.env.NODE_ENV === 'production') {
44
const getAssetPath = require('../util/getAssetPath')
5+
const filename = getAssetPath(
6+
options,
7+
`js/[name].[chunkhash:8].js`,
8+
true /* placeAtRootIfRelative */
9+
)
510

611
webpackConfig
712
.mode('production')
813
.devtool('source-map')
914
.output
10-
.filename(getAssetPath(options, `js/[name].[chunkhash:8].js`))
11-
.chunkFilename(getAssetPath(options, `js/[name].[chunkhash:8].js`))
15+
.filename(filename)
16+
.chunkFilename(filename)
1217

1318
// keep module.id stable when vendor modules does not change
1419
webpackConfig

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const { createSchema, validate } = require('@vue/cli-shared-utils')
22

33
const schema = createSchema(joi => joi.object({
4-
baseUrl: joi.string(),
4+
baseUrl: joi.string().allow(''),
55
outputDir: joi.string(),
66
assetsDir: joi.string(),
77
runtimeCompiler: joi.boolean(),

packages/@vue/cli-service/lib/util/getAssetPath.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
const path = require('path')
22

3-
module.exports = function getAssetPath (options, filePath) {
3+
module.exports = function getAssetPath (options, filePath, placeAtRootIfRelative) {
4+
// if the user is using a relative URL, place js & css at dist root to ensure
5+
// relative paths work properly
6+
if (placeAtRootIfRelative && options.baseUrl.charAt(0) !== '/') {
7+
return filePath.replace(/^\w+\//, '')
8+
}
49
return options.assetsDir
510
? path.posix.join(options.assetsDir, filePath)
611
: filePath

0 commit comments

Comments
 (0)