Skip to content

Commit 87e89ce

Browse files
committed
Merge branch 'master' of https://github.com/didi/mpx
2 parents bc337bc + a40dc61 commit 87e89ce

4 files changed

Lines changed: 475 additions & 8 deletions

File tree

packages/webpack-plugin/lib/template-compiler/compiler.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,11 +176,11 @@ const i18nWxsPath = normalize.lib('runtime/i18n.wxs')
176176
const i18nWxsLoaderPath = normalize.lib('wxs/i18n-loader.js')
177177
// 添加~前缀避免wxs绝对路径在存在projectRoot时被拼接为错误路径
178178
const i18nWxsRequest = '~' + i18nWxsLoaderPath + '!' + i18nWxsPath
179-
const i18nModuleName = '_i'
179+
const i18nModuleName = '_i_'
180180
const stringifyWxsPath = '~' + normalize.lib('runtime/stringify.wxs')
181-
const stringifyModuleName = '_s'
181+
const stringifyModuleName = '_s_'
182182
const optionalChainWxsPath = '~' + normalize.lib('runtime/oc.wxs')
183-
const optionalChainWxsName = '_oc' // 改成_oc解决web下_o重名问题
183+
const optionalChainWxsName = '_oc_' // 改成_oc解决web下_o重名问题
184184

185185
const tagRES = /(\{\{(?:.|\n|\r)+?\}\})(?!})/
186186
const tagRE = /\{\{((?:.|\n|\r)+?)\}\}(?!})/
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* 链式合并方法的工具函数
3+
*
4+
* 在多条件分支下使用 Object.assign 会导致同名方法被覆盖,
5+
* 这个函数通过创建组合函数来确保所有方法都能按顺序执行。
6+
*
7+
* @param {Object} target - 目标 visitor 对象
8+
* @param {Object} source - 要链式分配的 visitor 方法对象
9+
*
10+
* @example
11+
* const visitor = {}
12+
*
13+
* // 第一次合并
14+
* chainAssign(visitor, {
15+
* CallExpression(path) {
16+
* console.log('第一个处理器')
17+
* }
18+
* })
19+
*
20+
* // 第二次合并 - 不会覆盖,而是组合执行
21+
* chainAssign(visitor, {
22+
* CallExpression(path) {
23+
* console.log('第二个处理器')
24+
* }
25+
* })
26+
*
27+
* // 执行时会依次输出:
28+
* // 第一个处理器
29+
* // 第二个处理器
30+
*/
31+
module.exports = function chainAssign (target, source) {
32+
for (const [key, value] of Object.entries(source)) {
33+
if (target[key]) {
34+
// 如果已存在同名方法,创建组合函数依次执行
35+
const originalMethod = target[key]
36+
target[key] = function (path) {
37+
originalMethod.call(this, path)
38+
// 只有当节点没有停止遍历或被移除时才继续执行
39+
if (!path.removed && !path.shouldStop) {
40+
value.call(this, path)
41+
}
42+
}
43+
} else {
44+
target[key] = value
45+
}
46+
}
47+
}

packages/webpack-plugin/lib/wxs/pre-loader.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const t = require('@babel/types')
44
const generate = require('@babel/generator').default
55
const parseRequest = require('../utils/parse-request')
66
const isEmptyObject = require('../utils/is-empty-object')
7+
const chainAssign = require('../utils/chain-assign')
78
const parseQuery = require('loader-utils').parseQuery
89

910
module.exports = function (content) {
@@ -30,8 +31,7 @@ module.exports = function (content) {
3031
' __mpx_args__[i] = arguments[i];\n' +
3132
'}'
3233
).program.body
33-
// todo Object.assign可能会覆盖,未来存在非预期的覆盖case时需要改进处理
34-
Object.assign(visitor, {
34+
chainAssign(visitor, {
3535
Identifier (path) {
3636
if (path.node.name === 'arguments') {
3737
path.node.name = '__mpx_args__'
@@ -66,7 +66,7 @@ module.exports = function (content) {
6666
}
6767

6868
if (mode !== 'wx') {
69-
Object.assign(visitor, {
69+
chainAssign(visitor, {
7070
CallExpression (path) {
7171
const callee = path.node.callee
7272
if (t.isIdentifier(callee) && callee.name === 'getRegExp') {
@@ -81,7 +81,7 @@ module.exports = function (content) {
8181
}
8282

8383
if (mode === 'dd') {
84-
Object.assign(visitor, {
84+
chainAssign(visitor, {
8585
MemberExpression (path) {
8686
const property = path.node.property
8787
if (
@@ -96,7 +96,7 @@ module.exports = function (content) {
9696
}
9797

9898
if (!module.wxs) {
99-
Object.assign(visitor, {
99+
chainAssign(visitor, {
100100
MemberExpression (path) {
101101
const property = path.node.property
102102
if (

0 commit comments

Comments
 (0)