提交 23898a1f 编写于 作者: fxy060608's avatar fxy060608

fix(h5): compatible with relative public path

上级 ea172958
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 = {
......
......@@ -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
}
......
......@@ -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 上边
......
......@@ -62,6 +62,7 @@ function fixBooleanAttribute (el) {
}
module.exports = {
h5: true,
modules: [require('../format-text'), {
preTransformNode (el, options) {
fixBooleanAttribute(el)
......
......@@ -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)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册