diff --git a/packages/uni-template-compiler/lib/auto-components.js b/packages/uni-template-compiler/lib/auto-components.js index bb6511847ad65460c58513fd2e341b03dc34acf3..9220bbe3092dd05c815b89c11f52c75bc8773df3 100644 --- a/packages/uni-template-compiler/lib/auto-components.js +++ b/packages/uni-template-compiler/lib/auto-components.js @@ -5,8 +5,8 @@ const { module.exports = { preTransformNode (el, options) { if (isComponent(el.tag)) { - // 挂在 isReservedTag 上边,可以保证外部访问到 - (options.isReservedTag.autoComponents || (options.isReservedTag.autoComponents = new Set())).add(el.tag) + // 挂在 isUnaryTag 上边,可以保证外部访问到 + (options.isUnaryTag.autoComponents || (options.isUnaryTag.autoComponents = new Set())).add(el.tag) } } } diff --git a/packages/uni-template-compiler/lib/index.js b/packages/uni-template-compiler/lib/index.js index 876abd47056ed89c8dd506f71644cdb50d397a1a..9ba87750e56180fbf12d0bc7da54f17e2f03086d 100644 --- a/packages/uni-template-compiler/lib/index.js +++ b/packages/uni-template-compiler/lib/index.js @@ -23,7 +23,8 @@ const compilerAlipayModule = require('./module-alipay') const generateCodeFrame = require('./codeframe') const { - isComponent + isComponent, + isUnaryTag } = require('./util') function compileTemplate (source, options) { @@ -31,7 +32,7 @@ function compileTemplate (source, options) { console.log(options) const { autoComponents - } = options.isReservedTag + } = options.isUnaryTag if (autoComponents) { console.log('检测到的自定义组件:' + [...autoComponents]) } @@ -44,10 +45,12 @@ function compileTemplate (source, options) { module.exports = { compile (source, options = {}) { - (options.modules || (options.modules = [])).push(require('./auto-components')) + (options.modules || (options.modules = [])).push(require('./auto-components')) + options.isUnaryTag = isUnaryTag + options.preserveWhitespace = false if (options.service) { (options.modules || (options.modules = [])).push(require('./app/service')) - options.optimize = false // 启用 staticRenderFns + options.optimize = false // 启用 staticRenderFns // domProps => attrs options.mustUseProp = () => false options.isReservedTag = (tagName) => !isComponent(tagName) // 非组件均为内置 @@ -61,7 +64,8 @@ module.exports = { } } else if (options.view) { (options.modules || (options.modules = [])).push(require('./app/view')) - options.optimize = false // 暂不启用 staticRenderFns + options.optimize = false // 暂不启用 staticRenderFns + options.isUnaryTag = isUnaryTag options.isReservedTag = (tagName) => false // 均为组件 try { return compileTemplate(source, options) diff --git a/packages/uni-template-compiler/lib/util.js b/packages/uni-template-compiler/lib/util.js index ef479dd717d93c9118c033f48200d1b66577f585..f4c6d125f03afcbdb4843d5d3a9d2694ff3c1bbe 100644 --- a/packages/uni-template-compiler/lib/util.js +++ b/packages/uni-template-compiler/lib/util.js @@ -6,9 +6,9 @@ const { METHOD_RENDER_LIST } = require('./constants') -function cached(fn) { +function cached (fn) { const cache = Object.create(null) - return function cachedFn(str) { + return function cachedFn (str) { const hit = cache[str] return hit || (cache[str] = fn(str)) } @@ -21,7 +21,7 @@ const camelize = cached((str) => { return str.replace(camelizeRE, (_, c) => c ? c.toUpperCase() : '') }) -function getCode(node) { +function getCode (node) { return babelGenerate(t.cloneDeep(node), { compact: true, jsescOption: { @@ -31,11 +31,11 @@ function getCode(node) { }).code } -function traverseKey(ast, state) { +function traverseKey (ast, state) { let forKey = false babelTraverse(ast, { noScope: true, - ObjectProperty(path) { + ObjectProperty (path) { if (forKey) { return } @@ -44,7 +44,7 @@ function traverseKey(ast, state) { path.stop() } }, - CallExpression(path) { + CallExpression (path) { if (path.node.callee.name === METHOD_RENDER_LIST) { path.stop() } @@ -53,7 +53,7 @@ function traverseKey(ast, state) { return forKey } -function traverseFilter(ast, state) { +function traverseFilter (ast, state) { const filterModules = state.options.filterModules if (!filterModules.length) { return false @@ -61,7 +61,7 @@ function traverseFilter(ast, state) { let isFilter = false babelTraverse(ast, { noScope: true, - Identifier(path) { + Identifier (path) { if (filterModules.includes(path.node.name)) { const parentNode = path.parent if ( // t.msg || t['msg'] @@ -81,11 +81,11 @@ function traverseFilter(ast, state) { return isFilter } -function wrapper(code, reverse = false) { +function wrapper (code, reverse = false) { return reverse ? `{{!(${code})}}` : `{{${code}}}` } -function genCode(node, noWrapper = false, reverse = false, quotes = true) { +function genCode (node, noWrapper = false, reverse = false, quotes = true) { if (t.isStringLiteral(node)) { return reverse ? `!(${node.value})` : node.value } else if (t.isIdentifier(node)) { @@ -98,11 +98,11 @@ function genCode(node, noWrapper = false, reverse = false, quotes = true) { return noWrapper ? code : wrapper(code, reverse) } -function getForIndexIdentifier(id) { +function getForIndexIdentifier (id) { return `__i${id}__` } -function getForKey(forKey, forIndex, state) { +function getForKey (forKey, forIndex, state) { if (forKey) { if (t.isIdentifier(forKey)) { if (forIndex !== forKey.name) { // 非 forIndex @@ -121,7 +121,7 @@ function getForKey(forKey, forIndex, state) { return '' } -function processMemberProperty(node, state) { +function processMemberProperty (node, state) { if (node.computed) { const property = node.property if (t.isNumericLiteral(property)) { @@ -139,7 +139,7 @@ function processMemberProperty(node, state) { } } -function processMemberExpression(element, state) { +function processMemberExpression (element, state) { // item['order']=>item.order if (t.isMemberExpression(element)) { element = t.cloneDeep(element) @@ -152,19 +152,19 @@ function processMemberExpression(element, state) { babelTraverse(element, { noScope: true, - MemberExpression(path) { + MemberExpression (path) { processMemberProperty(path.node, state) } }) babelTraverse(element, { noScope: true, - MemberExpression(path) { + MemberExpression (path) { if (t.isStringLiteral(path.node.property)) { path.node.computed = false } }, - StringLiteral(path) { + StringLiteral (path) { path.replaceWith(t.identifier(path.node.value)) } }) @@ -172,7 +172,7 @@ function processMemberExpression(element, state) { return element } -function hasOwn(obj, key) { +function hasOwn (obj, key) { return hasOwnProperty.call(obj, key) } @@ -182,7 +182,7 @@ const { getTagName } = require('./h5') -function isComponent(tagName) { +function isComponent (tagName) { if ( tagName === 'block' || tagName === 'component' || @@ -194,15 +194,15 @@ function isComponent(tagName) { return !hasOwn(tags, getTagName(tagName.replace('v-uni-', ''))) } -function makeMap(str, expectsLowerCase) { +function makeMap (str, expectsLowerCase) { const map = Object.create(null) const list = str.split(',') for (let i = 0; i < list.length; i++) { map[list[i]] = true } - return expectsLowerCase ? - val => map[val.toLowerCase()] : - val => map[val] + return expectsLowerCase + ? val => map[val.toLowerCase()] + : val => map[val] } module.exports = { isUnaryTag: makeMap( 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 2ccf287395d6f827ad2b8595cdd2b53930baa20f..ef13252ff048dfea825b40144947eb31426df5f8 100644 --- a/packages/vue-cli-plugin-uni/lib/h5/compiler-options.js +++ b/packages/vue-cli-plugin-uni/lib/h5/compiler-options.js @@ -2,10 +2,6 @@ const { tags } = require('@dcloudio/uni-cli-shared') -const { - isUnaryTag -} = require('@dcloudio/uni-template-compiler/lib/util') - const simplePathRE = /^[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*|\['[^']*?']|\["[^"]*?"]|\[\d+]|\[[A-Za-z_$][\w$]*])*$/ function processEvent (expr, filterModules) { @@ -66,8 +62,6 @@ function fixBooleanAttribute (el) { } module.exports = { - isUnaryTag, - preserveWhitespace: false, modules: [require('../format-text'), { preTransformNode (el, options) { fixBooleanAttribute(el) diff --git a/packages/vue-cli-plugin-uni/lib/h5/index.js b/packages/vue-cli-plugin-uni/lib/h5/index.js index b25da6d1598b483fca092c9726366422ac65d4d7..752c11450fb1407021b904632462a26fc434daa8 100644 --- a/packages/vue-cli-plugin-uni/lib/h5/index.js +++ b/packages/vue-cli-plugin-uni/lib/h5/index.js @@ -144,6 +144,11 @@ module.exports = { loader: resolve('packages/webpack-uni-filter-loader') }] }] + }, + resolveLoader: { + alias: { + 'vue-style-loader': resolve('packages/h5-vue-style-loader') + } }, plugins } diff --git a/packages/vue-cli-plugin-uni/lib/mp-compiler-options.js b/packages/vue-cli-plugin-uni/lib/mp-compiler-options.js index d9593e6a31ebfa048f52e30524d956597f4a6134..544713d308566b91d7db74dd4b56de492e2ef90c 100644 --- a/packages/vue-cli-plugin-uni/lib/mp-compiler-options.js +++ b/packages/vue-cli-plugin-uni/lib/mp-compiler-options.js @@ -3,15 +3,9 @@ const { convertStaticStyle } = require('@dcloudio/uni-cli-shared') -const { - isUnaryTag -} = require('@dcloudio/uni-template-compiler/lib/util') - module.exports = { - isUnaryTag, - preserveWhitespace: false, modules: [require('./format-text'), { - preTransformNode(el, { + preTransformNode (el, { warn }) { if (el.attrsMap) { @@ -40,7 +34,7 @@ module.exports = { }) } }, - postTransformNode(el) { + postTransformNode (el) { if (process.env.UNI_PLATFORM === 'mp-alipay') { if (el.tag === 'slot') { if (!el.children.length) {