Skip to content

Commit

Permalink
Merge pull request #325 from lsycxyj/feature/global_mpx_attrs_filter
Browse files Browse the repository at this point in the history
feat: globalMpxAttrsFilter
  • Loading branch information
hiyuki committed Oct 29, 2019
2 parents 4ef9e7b + ac94700 commit 25d1cb6
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 25 deletions.
1 change: 1 addition & 0 deletions packages/webpack-plugin/lib/index.js
Expand Up @@ -233,6 +233,7 @@ class MpxWebpackPlugin {
resolveMode: this.options.resolveMode,
mode: this.options.mode,
srcMode: this.options.srcMode,
globalMpxAttrsFilter: this.options.globalMpxAttrsFilter,
externalClasses: this.options.externalClasses,
projectRoot: this.options.projectRoot,
autoScopeRules: this.options.autoScopeRules,
Expand Down
63 changes: 38 additions & 25 deletions packages/webpack-plugin/lib/template-compiler/compiler.js
Expand Up @@ -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(',')
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions packages/webpack-plugin/lib/template-compiler/index.js
Expand Up @@ -41,6 +41,7 @@ module.exports = function (raw) {
basename: path.basename(this.resource),
isComponent: !!componentsMap[resourcePath],
mode,
globalMpxAttrsFilter: mpx.globalMpxAttrsFilter,
externalClasses,
srcMode: localSrcMode || globalSrcMode,
isNative,
Expand Down

0 comments on commit 25d1cb6

Please sign in to comment.