Skip to content

Commit 89b3c82

Browse files
authored
Merge pull request #2260 from didi/fix-mode-dd-build-error
fix: 修复输出dd时memberChain多个处理改变path后编译报错问题
2 parents fb89c96 + 9b583a0 commit 89b3c82

4 files changed

Lines changed: 288 additions & 342 deletions

File tree

packages/webpack-plugin/lib/utils/chain-assign.js

Lines changed: 0 additions & 47 deletions
This file was deleted.
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* 链式合并方法的工具函数
3+
*
4+
* 在多条件分支下使用 Object.assign 会导致同名方法被覆盖,
5+
* 这个函数通过创建组合函数来确保所有方法都能按顺序执行。
6+
*
7+
* @param {Object} target - 目标 visitor 对象
8+
* @param {Object} source - 要链式分配的 visitor 方法对象
9+
**/
10+
11+
// 辅助函数:将 visitor 的所有钩子添加到结果中
12+
function mergeVisitorHooks (result, visitor) {
13+
result.enter = result.enter.concat(visitor.enter)
14+
result.exit = result.exit.concat(visitor.exit)
15+
return result
16+
}
17+
18+
function normalizeVisitor(visitor) {
19+
if (Array.isArray(visitor.enter) && Array.isArray(visitor.exit)) {
20+
return visitor
21+
}
22+
if (typeof visitor === 'function') {
23+
return { enter: [visitor], exit: [] }
24+
}
25+
26+
if (visitor.enter) {
27+
if (!Array.isArray(visitor.enter)) {
28+
visitor.enter = [visitor.enter]
29+
}
30+
} else {
31+
visitor.enter = []
32+
}
33+
34+
if (visitor.exit) {
35+
if (!Array.isArray(visitor.exit)) {
36+
visitor.exit = [visitor.exit]
37+
}
38+
} else {
39+
visitor.exit = []
40+
}
41+
return visitor
42+
}
43+
44+
module.exports = function mergeVisitors (target, source) {
45+
for (const [key, value] of Object.entries(source)) {
46+
if (!target[key]) {
47+
target[key] = normalizeVisitor(value)
48+
} else {
49+
// 合并现有值和新值
50+
target[key] = mergeVisitorHooks(normalizeVisitor(target[key]), normalizeVisitor(value))
51+
}
52+
}
53+
54+
return target
55+
}

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +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')
7+
const mergeVisitors = require('../utils/merge-visitors')
88
const parseQuery = require('loader-utils').parseQuery
99

1010
module.exports = function (content) {
@@ -31,7 +31,7 @@ module.exports = function (content) {
3131
' __mpx_args__[i] = arguments[i];\n' +
3232
'}'
3333
).program.body
34-
chainAssign(visitor, {
34+
mergeVisitors(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-
chainAssign(visitor, {
69+
mergeVisitors(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-
chainAssign(visitor, {
84+
mergeVisitors(visitor, {
8585
MemberExpression (path) {
8686
const property = path.node.property
8787
if (
@@ -96,11 +96,14 @@ module.exports = function (content) {
9696
}
9797

9898
if (!module.wxs) {
99-
chainAssign(visitor, {
99+
mergeVisitors(visitor, {
100100
MemberExpression (path) {
101+
if (!t.isMemberExpression(path.node)) {
102+
return
103+
}
101104
const property = path.node.property
102105
if (
103-
(property.name === 'constructor' || property.value === 'constructor') &&
106+
property && (property.name === 'constructor' || property.value === 'constructor') &&
104107
!(t.isMemberExpression(path.parent) && path.parentKey === 'object')
105108
) {
106109
path.replaceWith(t.memberExpression(path.node, t.identifier('name')))

0 commit comments

Comments
 (0)