diff --git a/packages/uni-cli-shared/lib/vue-loader.js b/packages/uni-cli-shared/lib/vue-loader.js index ed0c123d01288392dc4999a44a7daf18d50f17c6..a3b31daa31d2ce29f9147ba89174c8506a8b5add 100644 --- a/packages/uni-cli-shared/lib/vue-loader.js +++ b/packages/uni-cli-shared/lib/vue-loader.js @@ -1,8 +1,37 @@ +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: require('@dcloudio/uni-template-compiler'), + compiler, hotReload: false, cacheDirectory: false, - cacheIdentifier: false + cacheIdentifier: false, + transformAssetUrls } const defaultCompilerOptions = { @@ -18,5 +47,6 @@ module.exports = { options, { compilerOptions: Object.assign({}, defaultCompilerOptions, compilerOptions) }) - } + }, + compiler } diff --git a/packages/uni-template-compiler/lib/asset-url.js b/packages/uni-template-compiler/lib/asset-url.js new file mode 100644 index 0000000000000000000000000000000000000000..5a56ddc82e6bcb90f3fdab5b1adea47fd13632fb --- /dev/null +++ b/packages/uni-template-compiler/lib/asset-url.js @@ -0,0 +1,41 @@ +const transformAssetUrls = { + 'audio': 'src', + 'video': ['src', 'poster'], + 'img': 'src', + 'image': 'src', + 'cover-image': 'src', + 'v-uni-audio': 'src', + 'v-uni-video': ['src', 'poster'], + 'v-uni-image': 'src', + 'v-uni-cover-image': 'src' +} + +function rewrite (attr, name) { + if (attr.name === name) { + const value = attr.value + // only transform static URLs + if (value.charAt(0) === '"' && value.charAt(value.length - 1) === '"') { + attr.value = attr.value + .replace('"@/', '"/') + .replace('"~@/', '"/') + return true + } + } + return false +} +module.exports = { + postTransformNode: (node) => { + if (!node.attrs) { + return + } + const attributes = transformAssetUrls[node.tag] + if (!attributes) { + return + } + if (typeof attributes === 'string') { + node.attrs.some(attr => rewrite(attr, attributes)) + } else if (Array.isArray(attributes)) { + attributes.forEach(item => node.attrs.some(attr => rewrite(attr, item))) + } + } +} diff --git a/packages/uni-template-compiler/lib/index.js b/packages/uni-template-compiler/lib/index.js index 925dc967e2a7b88ade009a0c36ef6096f9d02bd7..88e36fa125f66f72c03ef71ae08f68925499e4b8 100644 --- a/packages/uni-template-compiler/lib/index.js +++ b/packages/uni-template-compiler/lib/index.js @@ -42,6 +42,11 @@ module.exports = { (options.modules || (options.modules = [])).push(autoComponentsModule) } + // 非h5平台,transformAssetUrls + if (process.env.UNI_PLATFORM !== 'h5') { + (options.modules || (options.modules = [])).push(require('./asset-url')) + } + options.isUnaryTag = isUnaryTag // 将 autoComponents 挂在 isUnaryTag 上边 options.isUnaryTag.autoComponents = new Set() diff --git a/packages/vue-cli-plugin-uni/lib/app-plus/index.js b/packages/vue-cli-plugin-uni/lib/app-plus/index.js index d7020707f16e7c20b7313335ffb94cf254af6172..81e1d191d22e9ab3e37fe1714849e199aaef050f 100644 --- a/packages/vue-cli-plugin-uni/lib/app-plus/index.js +++ b/packages/vue-cli-plugin-uni/lib/app-plus/index.js @@ -3,8 +3,7 @@ const webpack = require('webpack') const { getMainEntry, - isInHBuilderX, - getPlatformCompiler + isInHBuilderX } = require('@dcloudio/uni-cli-shared') const vueLoader = require('@dcloudio/uni-cli-shared/lib/vue-loader') @@ -163,7 +162,7 @@ const v3 = { loader: isAppService ? 'wrap-loader' : path.resolve(__dirname, '../../packages/webpack-uni-app-loader/view/main.js'), options: { - compiler: getPlatformCompiler(), + compiler: vueLoader.compiler, before: [ beforeCode + statCode + getGlobalUsingComponentsCode() ] diff --git a/packages/vue-cli-plugin-uni/lib/configure-webpack.js b/packages/vue-cli-plugin-uni/lib/configure-webpack.js index a8220ab857a63f12150d92594573cf225a31f949..f7a0cbeaa51123196f283f9da839604238f9a9d5 100644 --- a/packages/vue-cli-plugin-uni/lib/configure-webpack.js +++ b/packages/vue-cli-plugin-uni/lib/configure-webpack.js @@ -264,4 +264,4 @@ module.exports = function configureWebpack (platformOptions, manifestPlatformOpt } }, platformWebpackConfig) } -} +} diff --git a/packages/vue-cli-plugin-uni/lib/h5/index.js b/packages/vue-cli-plugin-uni/lib/h5/index.js index 7bf377cbbde55ba5dd937e645479144e630a9be2..546f0256f01828cc290a5be048ff680ad4573653 100644 --- a/packages/vue-cli-plugin-uni/lib/h5/index.js +++ b/packages/vue-cli-plugin-uni/lib/h5/index.js @@ -182,11 +182,7 @@ global.onAppShow = function(){}; modifyVueLoader(webpackConfig, { isH5: true, - hotReload: true, - transformAssetUrls: { - 'v-uni-image': ['src'], - 'v-uni-cover-image': ['src'] - } + hotReload: true }, require('./compiler-options'), api) if (process.env.NODE_ENV === 'production') {