提交 1cbf4950 编写于 作者: fxy060608's avatar fxy060608

feat(cli): template asset url

上级 af39305e
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 = { const defaultOptions = {
compiler: require('@dcloudio/uni-template-compiler'), compiler,
hotReload: false, hotReload: false,
cacheDirectory: false, cacheDirectory: false,
cacheIdentifier: false cacheIdentifier: false,
transformAssetUrls
} }
const defaultCompilerOptions = { const defaultCompilerOptions = {
...@@ -18,5 +47,6 @@ module.exports = { ...@@ -18,5 +47,6 @@ module.exports = {
options, { options, {
compilerOptions: Object.assign({}, defaultCompilerOptions, compilerOptions) compilerOptions: Object.assign({}, defaultCompilerOptions, compilerOptions)
}) })
} },
compiler
} }
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)))
}
}
}
...@@ -42,6 +42,11 @@ module.exports = { ...@@ -42,6 +42,11 @@ module.exports = {
(options.modules || (options.modules = [])).push(autoComponentsModule) (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 options.isUnaryTag = isUnaryTag
// 将 autoComponents 挂在 isUnaryTag 上边 // 将 autoComponents 挂在 isUnaryTag 上边
options.isUnaryTag.autoComponents = new Set() options.isUnaryTag.autoComponents = new Set()
......
...@@ -3,8 +3,7 @@ const webpack = require('webpack') ...@@ -3,8 +3,7 @@ const webpack = require('webpack')
const { const {
getMainEntry, getMainEntry,
isInHBuilderX, isInHBuilderX
getPlatformCompiler
} = require('@dcloudio/uni-cli-shared') } = require('@dcloudio/uni-cli-shared')
const vueLoader = require('@dcloudio/uni-cli-shared/lib/vue-loader') const vueLoader = require('@dcloudio/uni-cli-shared/lib/vue-loader')
...@@ -163,7 +162,7 @@ const v3 = { ...@@ -163,7 +162,7 @@ const v3 = {
loader: isAppService ? 'wrap-loader' : path.resolve(__dirname, loader: isAppService ? 'wrap-loader' : path.resolve(__dirname,
'../../packages/webpack-uni-app-loader/view/main.js'), '../../packages/webpack-uni-app-loader/view/main.js'),
options: { options: {
compiler: getPlatformCompiler(), compiler: vueLoader.compiler,
before: [ before: [
beforeCode + statCode + getGlobalUsingComponentsCode() beforeCode + statCode + getGlobalUsingComponentsCode()
] ]
......
...@@ -264,4 +264,4 @@ module.exports = function configureWebpack (platformOptions, manifestPlatformOpt ...@@ -264,4 +264,4 @@ module.exports = function configureWebpack (platformOptions, manifestPlatformOpt
} }
}, platformWebpackConfig) }, platformWebpackConfig)
} }
} }
...@@ -182,11 +182,7 @@ global.onAppShow = function(){}; ...@@ -182,11 +182,7 @@ global.onAppShow = function(){};
modifyVueLoader(webpackConfig, { modifyVueLoader(webpackConfig, {
isH5: true, isH5: true,
hotReload: true, hotReload: true
transformAssetUrls: {
'v-uni-image': ['src'],
'v-uni-cover-image': ['src']
}
}, require('./compiler-options'), api) }, require('./compiler-options'), api)
if (process.env.NODE_ENV === 'production') { if (process.env.NODE_ENV === 'production') {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册