From 1cbf4950bae6dec383efefa49550298605843d12 Mon Sep 17 00:00:00 2001 From: fxy060608 Date: Wed, 11 Mar 2020 18:33:47 +0800 Subject: [PATCH] feat(cli): template asset url --- packages/uni-cli-shared/lib/vue-loader.js | 36 ++++++++++++++-- .../uni-template-compiler/lib/asset-url.js | 41 +++++++++++++++++++ packages/uni-template-compiler/lib/index.js | 5 +++ .../vue-cli-plugin-uni/lib/app-plus/index.js | 5 +-- .../lib/configure-webpack.js | 2 +- packages/vue-cli-plugin-uni/lib/h5/index.js | 6 +-- 6 files changed, 83 insertions(+), 12 deletions(-) create mode 100644 packages/uni-template-compiler/lib/asset-url.js diff --git a/packages/uni-cli-shared/lib/vue-loader.js b/packages/uni-cli-shared/lib/vue-loader.js index ed0c123d0..a3b31daa3 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 000000000..5a56ddc82 --- /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 925dc967e..88e36fa12 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 d7020707f..81e1d191d 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 a8220ab85..f7a0cbeaa 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 7bf377cbb..546f0256f 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') { -- GitLab