From dc382111d95dc5cf60186b0792fb0893ff193d88 Mon Sep 17 00:00:00 2001 From: Evan You Date: Tue, 5 Jun 2018 00:22:20 -0400 Subject: [PATCH] feat: allow using relative baseUrl --- packages/@vue/cli-service/lib/Service.js | 1 + packages/@vue/cli-service/lib/config/css.js | 9 +++++++-- packages/@vue/cli-service/lib/config/prod.js | 9 +++++++-- packages/@vue/cli-service/lib/options.js | 2 +- packages/@vue/cli-service/lib/util/getAssetPath.js | 7 ++++++- 5 files changed, 22 insertions(+), 6 deletions(-) diff --git a/packages/@vue/cli-service/lib/Service.js b/packages/@vue/cli-service/lib/Service.js index f8097daa45..a32dbd2c15 100644 --- a/packages/@vue/cli-service/lib/Service.js +++ b/packages/@vue/cli-service/lib/Service.js @@ -248,6 +248,7 @@ module.exports = class Service { } // normlaize some options + resolved.baseUrl = resolved.baseUrl.replace(/^\.\//, '') ensureSlash(resolved, 'baseUrl') removeSlash(resolved, 'outputDir') diff --git a/packages/@vue/cli-service/lib/config/css.js b/packages/@vue/cli-service/lib/config/css.js index 8c54912849..46139c6337 100644 --- a/packages/@vue/cli-service/lib/config/css.js +++ b/packages/@vue/cli-service/lib/config/css.js @@ -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 diff --git a/packages/@vue/cli-service/lib/config/prod.js b/packages/@vue/cli-service/lib/config/prod.js index 91230c7b99..da3cde1d84 100644 --- a/packages/@vue/cli-service/lib/config/prod.js +++ b/packages/@vue/cli-service/lib/config/prod.js @@ -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 diff --git a/packages/@vue/cli-service/lib/options.js b/packages/@vue/cli-service/lib/options.js index dfdf154a21..25dd011ae4 100644 --- a/packages/@vue/cli-service/lib/options.js +++ b/packages/@vue/cli-service/lib/options.js @@ -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(), diff --git a/packages/@vue/cli-service/lib/util/getAssetPath.js b/packages/@vue/cli-service/lib/util/getAssetPath.js index 8a28f94fa5..5966677650 100644 --- a/packages/@vue/cli-service/lib/util/getAssetPath.js +++ b/packages/@vue/cli-service/lib/util/getAssetPath.js @@ -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