diff --git a/packages/uni-cli-shared/lib/vue-loader.js b/packages/uni-cli-shared/lib/vue-loader.js index a3b31daa31d2ce29f9147ba89174c8506a8b5add..212958875e5238a29ab5e5f54fb9cee7354cf3eb 100644 --- a/packages/uni-cli-shared/lib/vue-loader.js +++ b/packages/uni-cli-shared/lib/vue-loader.js @@ -1,37 +1,11 @@ const compiler = require('@dcloudio/uni-template-compiler') -// 非 h5 平台禁用,由uni-template-compiler自行实现 -const transformAssetUrls = { - audio: false, - video: false, - source: false, - img: false, - image: false, - use: false -} - -if (process.env.UNI_PLATFORM === 'h5') { - Object.assign(transformAssetUrls, { - 'audio': 'src', - 'video': ['src', 'poster'], - 'source': 'src', - 'img': 'src', - 'use': ['xlink:href', 'href'], - 'image': 'src', - 'cover-image': 'src', - 'v-uni-audio': 'src', - 'v-uni-video': ['src', 'poster'], - 'v-uni-image': 'src', - 'v-uni-cover-image': 'src' - }) -} - const defaultOptions = { compiler, hotReload: false, cacheDirectory: false, cacheIdentifier: false, - transformAssetUrls + transformAssetUrls: false // 禁用,由 uni-template-compiler 自行实现 transformAssetUrls } const defaultCompilerOptions = { diff --git a/packages/uni-template-compiler/lib/asset-url.js b/packages/uni-template-compiler/lib/asset-url.js index d6b28096732b9322f09bb983905368719ac4413f..cb9621a3252aedeb8cb02fc038eab67c2084aa00 100644 --- a/packages/uni-template-compiler/lib/asset-url.js +++ b/packages/uni-template-compiler/lib/asset-url.js @@ -61,11 +61,20 @@ function rewrite (attr, name, options) { const value = attr.value // only transform static URLs if (value.charAt(0) === '"' && value.charAt(value.length - 1) === '"') { - attr.value = attr.value - .replace('"@/', '"/') - .replace('"~@/', '"/') - if (options.service || options.view) { // v3 + if (!options.h5) { // 非 H5 平台 + attr.value = attr.value + .replace('"@/', '"/') + .replace('"~@/', '"/') + } + if (options.service || options.view || options.h5) { // v3,h5 attr.value = urlToRequire(attr.value.slice(1, -1)) + // h5 且 publicPath 为 ./ (仅生产模式可能为./) + if ( + options.h5 && + options.publicPath === './' + ) { + attr.value = `(${attr.value}).substr(1)` + } } return true } diff --git a/packages/uni-template-compiler/lib/index.js b/packages/uni-template-compiler/lib/index.js index f6205148addd7607fed20d6280601c786bd94131..3ee46100a72285453bb1385904c33a483ae4e44b 100644 --- a/packages/uni-template-compiler/lib/index.js +++ b/packages/uni-template-compiler/lib/index.js @@ -45,10 +45,8 @@ module.exports = { (options.modules || (options.modules = [])).push(autoComponentsModule) } - // 非h5平台,transformAssetUrls - if (process.env.UNI_PLATFORM !== 'h5') { - (options.modules || (options.modules = [])).push(require('./asset-url')) - } + // transformAssetUrls + (options.modules || (options.modules = [])).push(require('./asset-url')) options.isUnaryTag = isUnaryTag // 将 autoComponents 挂在 isUnaryTag 上边 diff --git a/packages/vue-cli-plugin-uni/lib/h5/compiler-options.js b/packages/vue-cli-plugin-uni/lib/h5/compiler-options.js index ef13252ff048dfea825b40144947eb31426df5f8..9da1b0e59a50f79647fcffc6a0441fda942ef00e 100644 --- a/packages/vue-cli-plugin-uni/lib/h5/compiler-options.js +++ b/packages/vue-cli-plugin-uni/lib/h5/compiler-options.js @@ -62,6 +62,7 @@ function fixBooleanAttribute (el) { } module.exports = { + h5: true, modules: [require('../format-text'), { preTransformNode (el, options) { fixBooleanAttribute(el) diff --git a/packages/vue-cli-plugin-uni/lib/h5/index.js b/packages/vue-cli-plugin-uni/lib/h5/index.js index 2f37600daec93bcac1e09fd582686012ba833734..3175d468e7e74b29c9d075ace02f62579a6e1069 100644 --- a/packages/vue-cli-plugin-uni/lib/h5/index.js +++ b/packages/vue-cli-plugin-uni/lib/h5/index.js @@ -166,10 +166,14 @@ module.exports = { webpackConfig.plugins.delete('preload-index') } + const compilerOptions = require('./compiler-opitons') + if (publicPath === './') { + compilerOptions.publicPath = publicPath + } modifyVueLoader(webpackConfig, { isH5: true, hotReload: true - }, require('./compiler-options'), api) + }, compilerOptions, api) if (process.env.NODE_ENV === 'production') { require('./cssnano-options')(webpackConfig) diff --git a/packages/vue-cli-plugin-uni/lib/split-chunks.js b/packages/vue-cli-plugin-uni/lib/split-chunks.js index 03226ded506de83010ae6b0dce450a0a05744b54..6649316393ed63fe2fb58cc4d4da684b9876446e 100644 --- a/packages/vue-cli-plugin-uni/lib/split-chunks.js +++ b/packages/vue-cli-plugin-uni/lib/split-chunks.js @@ -133,7 +133,7 @@ module.exports = function getSplitChunks () { return chunks.find(item => !subPackageRoots.find(root => item.name.indexOf(root) === 0)) } - const subPackageRoots = Object.keys(process.UNI_SUBPACKAGES) + const subPackageRoots = Object.keys(process.UNI_SUBPACKAGES).map(root => root + '/') Object.keys(process.UNI_SUBPACKAGES).forEach(root => { (function (root) { @@ -145,7 +145,7 @@ module.exports = function getSplitChunks () { const matchSubPackages = findSubPackages(chunks) if ( matchSubPackages.size === 1 && - matchSubPackages.has(root) && + matchSubPackages.has(root + '/') && !hasMainPackage(chunks) ) { if (process.env.UNI_OPT_TRACE) {