Skip to content

Commit

Permalink
feat: allow using relative baseUrl
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Jun 5, 2018
1 parent bfebc6d commit dc38211
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 6 deletions.
1 change: 1 addition & 0 deletions packages/@vue/cli-service/lib/Service.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ module.exports = class Service {
}

// normlaize some options
resolved.baseUrl = resolved.baseUrl.replace(/^\.\//, '')
ensureSlash(resolved, 'baseUrl')
removeSlash(resolved, 'outputDir')

Expand Down
9 changes: 7 additions & 2 deletions packages/@vue/cli-service/lib/config/css.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,14 @@ module.exports = (api, options) => {
const shadowMode = !!process.env.VUE_CLI_CSS_SHADOW_MODE
const isProd = process.env.NODE_ENV === 'production'
const shouldExtract = isProd && extract !== false && !shadowMode
const filename = getAssetPath(
options,
`css/[name].[contenthash:8].css`,
true /* placeAtRootIfRelative */
)
const extractOptions = Object.assign({
filename: getAssetPath(options, `css/[name].[contenthash:8].css`),
chunkFilename: getAssetPath(options, 'css/[name].[contenthash:8].css')
filename,
chunkFilename: filename
}, extract && typeof extract === 'object' ? extract : {})

// check if the project has a valid postcss config
Expand Down
9 changes: 7 additions & 2 deletions packages/@vue/cli-service/lib/config/prod.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@ module.exports = (api, options) => {
api.chainWebpack(webpackConfig => {
if (process.env.NODE_ENV === 'production') {
const getAssetPath = require('../util/getAssetPath')
const filename = getAssetPath(
options,
`js/[name].[chunkhash:8].js`,
true /* placeAtRootIfRelative */
)

webpackConfig
.mode('production')
.devtool('source-map')
.output
.filename(getAssetPath(options, `js/[name].[chunkhash:8].js`))
.chunkFilename(getAssetPath(options, `js/[name].[chunkhash:8].js`))
.filename(filename)
.chunkFilename(filename)

// keep module.id stable when vendor modules does not change
webpackConfig
Expand Down
2 changes: 1 addition & 1 deletion packages/@vue/cli-service/lib/options.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { createSchema, validate } = require('@vue/cli-shared-utils')

const schema = createSchema(joi => joi.object({
baseUrl: joi.string(),
baseUrl: joi.string().allow(''),
outputDir: joi.string(),
assetsDir: joi.string(),
runtimeCompiler: joi.boolean(),
Expand Down
7 changes: 6 additions & 1 deletion packages/@vue/cli-service/lib/util/getAssetPath.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
const path = require('path')

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

0 comments on commit dc38211

Please sign in to comment.