Skip to content

Commit 64a4aaa

Browse files
committed
feat(jsx2mp-loader): hackRegeneratorRuntimeFunction in ali miniapp of copyNpm
1 parent dc74534 commit 64a4aaa

File tree

2 files changed

+55
-3
lines changed

2 files changed

+55
-3
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* @Author 阿劭 [email protected]
3+
* @Date 2023-04-10 18:21:49
4+
* @LastEditors 阿劭 [email protected]
5+
* @LastEditTime 2023-04-10 21:33:21
6+
* @Description 小程序严格模式下报错 `Function(...) is not function` 兼容
7+
* https://aliyuque.antfin.com/tianjie.stj/nivfgf/wi0gk43l6otk3lam?singleDoc#tnMFv
8+
*/
9+
10+
module.exports = function visitor({ types: t }) {
11+
12+
/**
13+
* 判断节点是否是 Function("r", "regeneratorRuntime = r")(runtime);
14+
* @param {*} node
15+
*/
16+
function isFunctionRegeneratorRuntime (node) {
17+
return (
18+
t.isCallExpression(node.callee) &&
19+
node.callee.callee.name === 'Function' &&
20+
node.callee.arguments.length === 2 &&
21+
t.isStringLiteral(node.callee.arguments[0]) &&
22+
node.callee.arguments[0].value === 'r' &&
23+
t.isStringLiteral(node.callee.arguments[1]) &&
24+
node.callee.arguments[1].value === 'regeneratorRuntime = r' &&
25+
node.arguments.length === 1 &&
26+
t.isIdentifier(node.arguments[0]) &&
27+
node.arguments[0].name === 'runtime'
28+
);
29+
}
30+
31+
return {
32+
visitor: {
33+
CallExpression(path, state) {
34+
const { node } = path;
35+
if (
36+
t.isCallExpression(node.callee) &&
37+
node.callee.callee.name === 'Function'
38+
) {
39+
if (isFunctionRegeneratorRuntime(node)) {
40+
if (path.parentPath.node && t.isExpressionStatement(path.parentPath.node)) {
41+
path.parentPath.remove();
42+
}
43+
}
44+
}
45+
},
46+
}
47+
};
48+
};

packages/jsx2mp-loader/src/script-loader.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const { join, dirname, relative, resolve, sep, extname } = require('path');
33
const { copySync, existsSync, mkdirpSync, ensureFileSync, writeJSONSync, readFileSync, readJSONSync } = require('fs-extra');
44
const { getOptions } = require('loader-utils');
55
const resolveModule = require('resolve');
6-
const { constants: { QUICKAPP }} = require('miniapp-builder-shared');
6+
const { constants: { QUICKAPP, MINIAPP }, platformMap } = require('miniapp-builder-shared');
77
const cached = require('./cached');
88
const { removeExt, doubleBackslash, normalizeOutputFilePath, addRelativePathPrefix, isFromTargetDirs } = require('./utils/pathHelper');
99
const { isNpmModule, isJSONFile, isTypescriptFile } = require('./utils/judgeModule');
@@ -70,6 +70,9 @@ module.exports = function scriptLoader(content) {
7070
let outputContent = {};
7171
let outputOption = {};
7272

73+
// 支付宝小程序 copyNpm 模式下对 @babel/runtime/regenerator/index.js 中的 `Function("r", "regeneratorRuntime=r")(runtime);` 进行处理
74+
const needHackRegeneratorRuntimeFunction = platform.type === platformMap[MINIAPP].type && !disableCopyNpm && this.resourcePath.indexOf('@babel/runtime/regenerator/index.js') > -1;
75+
7376
outputContent = { code: rawContent };
7477
outputOption = {
7578
outputPath: {
@@ -87,8 +90,9 @@ module.exports = function scriptLoader(content) {
8790
platform,
8891
aliasEntries
8992
}
90-
]
91-
],
93+
],
94+
needHackRegeneratorRuntimeFunction ? require('./babel-plugin-handle-regeneratorRuntime') : null
95+
].filter(t => t),
9296
platform,
9397
isTypescriptFile: isTypescriptFile(this.resourcePath),
9498
rootDir,

0 commit comments

Comments
 (0)