提交 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 = {
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
}
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 = {
(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()
......
......@@ -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()
]
......
......@@ -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') {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册