From f744040b50bf236a70c85bdc90ae2064ec82f35c Mon Sep 17 00:00:00 2001 From: Evan You Date: Tue, 6 Mar 2018 15:06:03 -0500 Subject: [PATCH] fix: use dynamic publicPath for web component bundles (fix #949) --- .../@vue/cli-service/lib/commands/build/resolveWcConfig.js | 6 ++++-- .../@vue/cli-service/lib/commands/build/resolveWcEntry.js | 7 +++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/packages/@vue/cli-service/lib/commands/build/resolveWcConfig.js b/packages/@vue/cli-service/lib/commands/build/resolveWcConfig.js index 0c5a65bbac..bb05a50545 100644 --- a/packages/@vue/cli-service/lib/commands/build/resolveWcConfig.js +++ b/packages/@vue/cli-service/lib/commands/build/resolveWcConfig.js @@ -68,8 +68,10 @@ module.exports = (api, { target, entry, name }) => { config.output .filename(`${entryName}.js`) .chunkFilename(`${libName}.[id]${minify ? `.min` : ``}.js`) - // use relative publicPath so this can be deployed anywhere - .publicPath('./') + // use dynamic publicPath so this can be deployed anywhere + // the actual path will be determined at runtime by checking + // document.currentScript.src. + .publicPath('') // externalize Vue in case user imports it config diff --git a/packages/@vue/cli-service/lib/commands/build/resolveWcEntry.js b/packages/@vue/cli-service/lib/commands/build/resolveWcEntry.js index 82d9988535..7b70cfd44a 100644 --- a/packages/@vue/cli-service/lib/commands/build/resolveWcEntry.js +++ b/packages/@vue/cli-service/lib/commands/build/resolveWcEntry.js @@ -33,6 +33,13 @@ import 'css-loader/lib/css-base' import 'vue-style-loader/lib/addStylesShadow' import 'vue-loader/lib/runtime/component-normalizer' +;(() => { + let i + if ((i = document.currentScript) && (i = i.src.match(/(.+\\/)[^/]+\\.js$/))) { + __webpack_public_path__ = i[1] + } +})() + ${files.map(file => { const { camelName, kebabName } = exports.fileToComponentName(prefix, file) return async