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 0a99ef6ed3509133539ed70e396228d02d5b3475..759cc97ab0a6c0129cfabf5850a8b85ba700c82b 100644 --- a/packages/uni-template-compiler/lib/index.js +++ b/packages/uni-template-compiler/lib/index.js @@ -46,10 +46,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 eb32bab9a4d1176a343585e26a8d1c5061c60725..e973014dd29d0d9c819399d2ba410ee9ef7296a3 100644 --- a/packages/vue-cli-plugin-uni/lib/h5/index.js +++ b/packages/vue-cli-plugin-uni/lib/h5/index.js @@ -167,10 +167,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)