diff --git a/packages/uni-template-compiler/__tests__/compiler-app-plus-extra.service.spec.js b/packages/uni-template-compiler/__tests__/compiler-app-plus-extra.service.spec.js
index aea890b0532234b57418ca692e9dc7ce34da33ab..f41b94cce02d2108f49cee981fb5cbf1a74b6b1f 100644
--- a/packages/uni-template-compiler/__tests__/compiler-app-plus-extra.service.spec.js
+++ b/packages/uni-template-compiler/__tests__/compiler-app-plus-extra.service.spec.js
@@ -250,6 +250,16 @@ describe('codegen', () => {
' {{text}} text text {{text}} ',
`with(this){return _c('view',[_v((_$s(0,'t0-0',_s(text)))+(_$s(0,'t0-1',_s(text))))])}`
)
+ })
+ it('generate bool attr', () => {
+ assertCodegen(
+ '',
+ `with(this){return _c('video',{attrs:{"_i":0}})}`
+ )
+ assertCodegen(
+ '',
+ `with(this){return _c('video',{})}`
+ )
})
})
/* eslint-enable quotes */
diff --git a/packages/uni-template-compiler/__tests__/compiler-app-plus-extra.view.spec.js b/packages/uni-template-compiler/__tests__/compiler-app-plus-extra.view.spec.js
index a34429112bbfd923e71d468ed9072d95ffbe1a92..9adfc872dcbebbf6bcb0cbb841938fe36d4a0c5b 100644
--- a/packages/uni-template-compiler/__tests__/compiler-app-plus-extra.view.spec.js
+++ b/packages/uni-template-compiler/__tests__/compiler-app-plus-extra.view.spec.js
@@ -189,6 +189,16 @@ describe('codegen', () => {
' {{text}} text text {{text}} ',
`with(this){return _c('v-uni-view',{attrs:{"_i":0}},[_v((_$g(0,'t0-0'))+" text text "+(_$g(0,'t0-1')))])}`
)
+ })
+ it('generate bool attr', () => {
+ assertCodegen(
+ '',
+ `with(this){return _c('v-uni-video',{attrs:{"controls":true,"_i":0}})}`
+ )
+ assertCodegen(
+ '',
+ `with(this){return _c('v-uni-video',{attrs:{"controls":"","_i":0}})}`
+ )
})
})
/* eslint-enable quotes */
diff --git a/packages/uni-template-compiler/__tests__/compiler-extra.spec.js b/packages/uni-template-compiler/__tests__/compiler-extra.spec.js
index ce108281acf5ec2853d041219cf7cbd8c35b516e..410e0d923e4671e55866fc9c5ecee3c325528391 100644
--- a/packages/uni-template-compiler/__tests__/compiler-extra.spec.js
+++ b/packages/uni-template-compiler/__tests__/compiler-extra.spec.js
@@ -646,5 +646,15 @@ describe('mp:compiler-extra', () => {
'{{ item }}',
'{{item}}'
)
+ })
+ it('generate bool attr', () => {
+ assertCodegen(
+ '',
+ ''
+ )
+ assertCodegen(
+ '',
+ ''
+ )
})
})
diff --git a/packages/uni-template-compiler/__tests__/demo.js b/packages/uni-template-compiler/__tests__/demo.js
index 75b8c58c5723cc9d9b6917757f46f3ba1d9e045c..81e914ea07f157a3f36a81d6c44462dbcc64e296 100644
--- a/packages/uni-template-compiler/__tests__/demo.js
+++ b/packages/uni-template-compiler/__tests__/demo.js
@@ -19,7 +19,7 @@ const scopedPath = path.resolve(__dirname, '../../')
const compiler = require('../lib')
const res = compiler.compile(
`
- {{text}} text text {{text}}
+
`, {
miniprogram: true,
resourcePath: '/User/fxy/Documents/test.wxml',
@@ -32,9 +32,9 @@ const res = compiler.compile(
mp: {
platform: 'mp-weixin'
},
- filterModules: ['swipe'],
+ filterModules: ['swipe']
// service: true,
- view: true
+ // view: true
})
console.log(require('util').inspect(res, {
diff --git a/packages/uni-template-compiler/lib/app/pre-transform-node.js b/packages/uni-template-compiler/lib/app/pre-transform-node.js
index 9c3b298ab95947b0f9df04f96d00da1e90fa1c95..b90234f27e4c8db1fdab133064d1386fd5f78646 100644
--- a/packages/uni-template-compiler/lib/app/pre-transform-node.js
+++ b/packages/uni-template-compiler/lib/app/pre-transform-node.js
@@ -4,32 +4,10 @@ const {
addRawAttr
} = require('./util')
-const dirRE = /^v-|^@|^:|^#/
-/**
- * 兼容小程序Boolean属性的怪异行为(为true,为false)
- * @param {Object} el
- */
-function fixBooleanAttribute (el) {
- if (!el.attrsList) {
- return
- }
- el.attrsList.forEach(attr => {
- if (attr.bool) { // =>
- if (!dirRE.test(attr.name) && attr.name !== 'inline-template') {
- delete el.attrsMap[attr.name]
- attr.name = ':' + attr.name
- attr.value = 'true'
- el.attrsMap[attr.name] = attr.value
- }
- }
- })
-}
-
module.exports = function preTransformNode (el, options) {
if (!hasOwn(options, 'nid')) {
options.nid = 0
}
- fixBooleanAttribute(el)
addRawAttr(el, ID, options.nid++)
if (el.attrsMap['v-for']) {
el.forId = el.attrsMap[ID]
diff --git a/packages/uni-template-compiler/lib/bool-attr.js b/packages/uni-template-compiler/lib/bool-attr.js
new file mode 100644
index 0000000000000000000000000000000000000000..8d46f86819c6a3655db85e754b103712e6ace58d
--- /dev/null
+++ b/packages/uni-template-compiler/lib/bool-attr.js
@@ -0,0 +1,19 @@
+const dirRE = /^v-|^@|^:|^#/
+
+module.exports = {
+ preTransformNode (el) {
+ if (!el.attrsList) {
+ return
+ }
+ el.attrsList.forEach(attr => {
+ if (attr.bool) {
+ if (!dirRE.test(attr.name) && attr.name !== 'inline-template') {
+ delete el.attrsMap[attr.name]
+ attr.name = ':' + attr.name
+ attr.value = 'true'
+ el.attrsMap[attr.name] = attr.value
+ }
+ }
+ })
+ }
+}
diff --git a/packages/uni-template-compiler/lib/index.js b/packages/uni-template-compiler/lib/index.js
index 93a9579f6776d312aaf33180bb0e49f690875fbd..4c8e44e61c55aa3a8707f423ee605d1b260aaf86 100644
--- a/packages/uni-template-compiler/lib/index.js
+++ b/packages/uni-template-compiler/lib/index.js
@@ -44,9 +44,12 @@ module.exports = {
) {
(options.modules || (options.modules = [])).push(autoComponentsModule)
}
-
+ if (!options.modules) {
+ options.modules = []
+ }
// transformAssetUrls
- (options.modules || (options.modules = [])).push(require('./asset-url'))
+ options.modules.push(require('./asset-url'))
+ options.modules.push(require('./bool-attr'))
options.isUnaryTag = isUnaryTag
// 将 autoComponents 挂在 isUnaryTag 上边
@@ -54,7 +57,7 @@ module.exports = {
options.preserveWhitespace = false
if (options.service) {
- (options.modules || (options.modules = [])).push(require('./app/service'))
+ options.modules.push(require('./app/service'))
options.optimize = false // 启用 staticRenderFns
// domProps => attrs
options.mustUseProp = () => false
@@ -68,7 +71,7 @@ module.exports = {
throw e
}
} else if (options.view) {
- (options.modules || (options.modules = [])).push(require('./app/view'))
+ options.modules.push(require('./app/view'))
options.optimize = false // 暂不启用 staticRenderFns
options.isUnaryTag = isUnaryTag
options.isReservedTag = (tagName) => false // 均为组件
@@ -80,14 +83,14 @@ module.exports = {
}
} else if (options['quickapp-native']) {
// 后续改版,应统一由具体包实现
- (options.modules || (options.modules = [])).push(require('@dcloudio/uni-quickapp-native/lib/compiler-module'))
+ options.modules.push(require('@dcloudio/uni-quickapp-native/lib/compiler-module'))
}
if (!options.mp) { // h5,quickapp-native
return compileTemplate(source, options, compile)
}
- (options.modules || (options.modules = [])).push(compilerModule)
+ options.modules.push(compilerModule)
if (options.mp.platform === 'mp-alipay') {
options.modules.push(compilerAlipayModule)
@@ -147,7 +150,7 @@ module.exports = {
delete state.files
// resolve scoped slots
- res.generic = state.generic || []
+ res.generic = state.generic || []
delete state.generic
// define scoped slots
diff --git a/packages/vue-cli-plugin-hbuilderx/build/vue-loader.conf.js b/packages/vue-cli-plugin-hbuilderx/build/vue-loader.conf.js
index 4b885abb0668d631cc377ea54942bb8fec4eaaaf..641c91c803e6b2f1368facf36854d39fa6498539 100644
--- a/packages/vue-cli-plugin-hbuilderx/build/vue-loader.conf.js
+++ b/packages/vue-cli-plugin-hbuilderx/build/vue-loader.conf.js
@@ -105,7 +105,8 @@ const oldCompile = compiler.compile
compiler.compile = function (source, options = {}) {
(options.modules || (options.modules = [])).push(autoComponentsModule)
- options.modules.push(require('@dcloudio/uni-template-compiler/lib/asset-url'))
+ options.modules.push(require('@dcloudio/uni-template-compiler/lib/asset-url'))
+ options.modules.push(require('@dcloudio/uni-template-compiler/lib/bool-attr'))
options.isUnaryTag = isUnaryTag
// 将 autoComponents 挂在 isUnaryTag 上边
diff --git a/packages/vue-cli-plugin-hbuilderx/packages/weex-template-compiler/build.js b/packages/vue-cli-plugin-hbuilderx/packages/weex-template-compiler/build.js
index 194440a955d1119aca4a0d1350ff0867e8fda8ce..bcd3961ecc29cba61f71dd51b3c27480c2db9d8b 100644
--- a/packages/vue-cli-plugin-hbuilderx/packages/weex-template-compiler/build.js
+++ b/packages/vue-cli-plugin-hbuilderx/packages/weex-template-compiler/build.js
@@ -479,7 +479,9 @@ function parseHTML (html, options) {
: options.shouldDecodeNewlines;
attrs[i] = {
name: args[1],
- value: decodeAttr(value, shouldDecodeNewlines)
+ value: decodeAttr(value, shouldDecodeNewlines),
+ // fixed by xxxxxx 标记 Boolean Attribute
+ bool: args[3] === undefined && args[4] === undefined && args[5] === undefined
};
if (process.env.NODE_ENV !== 'production' && options.outputSourceRange) {
attrs[i].start = args.start + args[0].match(/^\s*/).length;
diff --git a/packages/vue-cli-plugin-uni/lib/h5/compiler-options.js b/packages/vue-cli-plugin-uni/lib/h5/compiler-options.js
index 222138068f0dbcf3f059ef22283f3c66920e1789..ae9f69efd19ea4e839ef5c2ee17eb1b1277e7e03 100644
--- a/packages/vue-cli-plugin-uni/lib/h5/compiler-options.js
+++ b/packages/vue-cli-plugin-uni/lib/h5/compiler-options.js
@@ -40,32 +40,10 @@ function addTag (tag) {
process.UNI_TAGS.add(tag)
}
-const dirRE = /^v-|^@|^:/
-/**
- * 兼容小程序Boolean属性的怪异行为(为true,为false)
- * @param {Object} el
- */
-function fixBooleanAttribute (el) {
- if (!el.attrsList) {
- return
- }
- el.attrsList.forEach(attr => {
- if (attr.bool) { // =>
- if (!dirRE.test(attr.name) && attr.name !== 'inline-template') {
- delete el.attrsMap[attr.name]
- attr.name = ':' + attr.name
- attr.value = 'true'
- el.attrsMap[attr.name] = attr.value
- }
- }
- })
-}
-
module.exports = {
h5: true,
modules: [require('../format-text'), {
preTransformNode (el, options) {
- fixBooleanAttribute(el)
if (el.tag.indexOf('v-uni-') === 0) {
addTag(el.tag.replace('v-uni-', ''))
} else if (hasOwn(tags, el.tag)) {