From dfc7fdf29a5a7737b756dc5aef7175721685b21f Mon Sep 17 00:00:00 2001 From: Light Leung Date: Tue, 29 Oct 2019 11:42:49 +0800 Subject: [PATCH 1/2] feat: globalMpxAttrsFilter --- packages/webpack-plugin/lib/index.js | 1 + .../lib/template-compiler/compiler.js | 63 +++++++++++-------- .../lib/template-compiler/index.js | 1 + 3 files changed, 40 insertions(+), 25 deletions(-) diff --git a/packages/webpack-plugin/lib/index.js b/packages/webpack-plugin/lib/index.js index 5e6edb494..69a89fbfc 100644 --- a/packages/webpack-plugin/lib/index.js +++ b/packages/webpack-plugin/lib/index.js @@ -233,6 +233,7 @@ class MpxWebpackPlugin { resolveMode: this.options.resolveMode, mode: this.options.mode, srcMode: this.options.srcMode, + options: this.options, externalClasses: this.options.externalClasses, projectRoot: this.options.projectRoot, autoScopeRules: this.options.autoScopeRules, diff --git a/packages/webpack-plugin/lib/template-compiler/compiler.js b/packages/webpack-plugin/lib/template-compiler/compiler.js index 9f946c632..50857c772 100644 --- a/packages/webpack-plugin/lib/template-compiler/compiler.js +++ b/packages/webpack-plugin/lib/template-compiler/compiler.js @@ -186,18 +186,24 @@ function isMpxCommentAttrs (content) { return /@mpx-attrs/.test(content) } +function normalizePlatformMpxAttrsOpts (opts) { + const ret = {} + // Array to map for removing attributes + ret.remove = (opts.remove || []).reduce((acc, val) => { + acc[val] = true + return acc + }, {}) + // Default adding map + ret.add = opts.add || {} + return ret +} + function produceMpxCommentAttrs (content) { const exp = /@mpx-attrs[^(]*?\(([\s\S]*)\)/.exec(content)[1].trim() const tmpOpts = evalMpxCommentExp(exp) // normalize Object.keys(tmpOpts).forEach(k => { - // Array to map for removing attributes - tmpOpts[k].remove = (tmpOpts[k].remove || []).reduce((acc, val) => { - acc[val] = true - return acc - }, {}) - // Default adding map - tmpOpts[k].add = tmpOpts[k].add || {} + Object.assign(tmpOpts[k], normalizePlatformMpxAttrsOpts(tmpOpts[k])) if (k.indexOf(',') > -1) { const modes = k.split(',') @@ -210,29 +216,33 @@ function produceMpxCommentAttrs (content) { curMpxComment = tmpOpts } +function modifyAttrsFromCurMpxAttrOptions (attrs, curModeMpxComment) { + const removeMap = curModeMpxComment.remove + const addMap = curModeMpxComment.add + + const newAttrs = [] + attrs.forEach(attr => { + if (!removeMap[attr.name]) { + newAttrs.push(attr) + } + }) + + Object.keys(addMap).forEach(name => { + newAttrs.push({ + name, + value: addMap[name] + }) + }) + + return newAttrs +} + function consumeMpxCommentAttrs (attrs, mode) { let ret = attrs if (curMpxComment) { const curModeMpxComment = curMpxComment[mode] if (curModeMpxComment) { - const removeMap = curModeMpxComment.remove - const addMap = curModeMpxComment.add - - const newAttrs = [] - attrs.forEach(attr => { - if (!removeMap[attr.name]) { - newAttrs.push(attr) - } - }) - - Object.keys(addMap).forEach(name => { - newAttrs.push({ - name, - value: addMap[name] - }) - }) - - ret = newAttrs + ret = modifyAttrsFromCurMpxAttrOptions(attrs, curModeMpxComment) } // reset @@ -764,6 +774,9 @@ function parse (template, options) { attrs = guardIESVGBug(attrs) } + if (options.globalMpxAttrsFilter) { + attrs = modifyAttrsFromCurMpxAttrOptions(attrs, normalizePlatformMpxAttrsOpts(options.globalMpxAttrsFilter({ tagName: tag, attrs, __mpx_mode__: mode }) || {})) + } attrs = consumeMpxCommentAttrs(attrs, mode) let element = createASTElement(tag, attrs, currentParent) diff --git a/packages/webpack-plugin/lib/template-compiler/index.js b/packages/webpack-plugin/lib/template-compiler/index.js index 2088db6e5..ec2627186 100644 --- a/packages/webpack-plugin/lib/template-compiler/index.js +++ b/packages/webpack-plugin/lib/template-compiler/index.js @@ -41,6 +41,7 @@ module.exports = function (raw) { basename: path.basename(this.resource), isComponent: !!componentsMap[resourcePath], mode, + globalMpxAttrsFilter: mpx.options.globalMpxAttrsFilter, externalClasses, srcMode: localSrcMode || globalSrcMode, isNative, From 5b95d607f9f18c9e858465be9860b09e82db3ba2 Mon Sep 17 00:00:00 2001 From: Light Leung Date: Tue, 29 Oct 2019 15:20:56 +0800 Subject: [PATCH 2/2] feat: globalMpxAttrsFilter, remove setting plugin options to mpx object --- packages/webpack-plugin/lib/index.js | 2 +- packages/webpack-plugin/lib/template-compiler/index.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/webpack-plugin/lib/index.js b/packages/webpack-plugin/lib/index.js index 69a89fbfc..996d8929d 100644 --- a/packages/webpack-plugin/lib/index.js +++ b/packages/webpack-plugin/lib/index.js @@ -233,7 +233,7 @@ class MpxWebpackPlugin { resolveMode: this.options.resolveMode, mode: this.options.mode, srcMode: this.options.srcMode, - options: this.options, + globalMpxAttrsFilter: this.options.globalMpxAttrsFilter, externalClasses: this.options.externalClasses, projectRoot: this.options.projectRoot, autoScopeRules: this.options.autoScopeRules, diff --git a/packages/webpack-plugin/lib/template-compiler/index.js b/packages/webpack-plugin/lib/template-compiler/index.js index ec2627186..fbfffd0f1 100644 --- a/packages/webpack-plugin/lib/template-compiler/index.js +++ b/packages/webpack-plugin/lib/template-compiler/index.js @@ -41,7 +41,7 @@ module.exports = function (raw) { basename: path.basename(this.resource), isComponent: !!componentsMap[resourcePath], mode, - globalMpxAttrsFilter: mpx.options.globalMpxAttrsFilter, + globalMpxAttrsFilter: mpx.globalMpxAttrsFilter, externalClasses, srcMode: localSrcMode || globalSrcMode, isNative,