Skip to content

Commit

Permalink
feat(jsx2mp-loader): hackRegeneratorRuntimeFunction in ali miniapp of…
Browse files Browse the repository at this point in the history
… copyNpm
  • Loading branch information
shiftj18 committed Apr 11, 2023
1 parent dc74534 commit 64a4aaa
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* @Author 阿劭 [email protected]
* @Date 2023-04-10 18:21:49
* @LastEditors 阿劭 [email protected]
* @LastEditTime 2023-04-10 21:33:21
* @Description 小程序严格模式下报错 `Function(...) is not function` 兼容
* https://aliyuque.antfin.com/tianjie.stj/nivfgf/wi0gk43l6otk3lam?singleDoc#tnMFv
*/

module.exports = function visitor({ types: t }) {

/**
* 判断节点是否是 Function("r", "regeneratorRuntime = r")(runtime);
* @param {*} node
*/
function isFunctionRegeneratorRuntime (node) {
return (
t.isCallExpression(node.callee) &&
node.callee.callee.name === 'Function' &&
node.callee.arguments.length === 2 &&
t.isStringLiteral(node.callee.arguments[0]) &&
node.callee.arguments[0].value === 'r' &&
t.isStringLiteral(node.callee.arguments[1]) &&
node.callee.arguments[1].value === 'regeneratorRuntime = r' &&
node.arguments.length === 1 &&
t.isIdentifier(node.arguments[0]) &&
node.arguments[0].name === 'runtime'
);
}

return {
visitor: {
CallExpression(path, state) {
const { node } = path;
if (
t.isCallExpression(node.callee) &&
node.callee.callee.name === 'Function'
) {
if (isFunctionRegeneratorRuntime(node)) {
if (path.parentPath.node && t.isExpressionStatement(path.parentPath.node)) {
path.parentPath.remove();
}
}
}
},
}
};
};
10 changes: 7 additions & 3 deletions packages/jsx2mp-loader/src/script-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const { join, dirname, relative, resolve, sep, extname } = require('path');
const { copySync, existsSync, mkdirpSync, ensureFileSync, writeJSONSync, readFileSync, readJSONSync } = require('fs-extra');
const { getOptions } = require('loader-utils');
const resolveModule = require('resolve');
const { constants: { QUICKAPP }} = require('miniapp-builder-shared');
const { constants: { QUICKAPP, MINIAPP }, platformMap } = require('miniapp-builder-shared');
const cached = require('./cached');
const { removeExt, doubleBackslash, normalizeOutputFilePath, addRelativePathPrefix, isFromTargetDirs } = require('./utils/pathHelper');
const { isNpmModule, isJSONFile, isTypescriptFile } = require('./utils/judgeModule');
Expand Down Expand Up @@ -70,6 +70,9 @@ module.exports = function scriptLoader(content) {
let outputContent = {};
let outputOption = {};

// 支付宝小程序 copyNpm 模式下对 @babel/runtime/regenerator/index.js 中的 `Function("r", "regeneratorRuntime=r")(runtime);` 进行处理
const needHackRegeneratorRuntimeFunction = platform.type === platformMap[MINIAPP].type && !disableCopyNpm && this.resourcePath.indexOf('@babel/runtime/regenerator/index.js') > -1;

outputContent = { code: rawContent };
outputOption = {
outputPath: {
Expand All @@ -87,8 +90,9 @@ module.exports = function scriptLoader(content) {
platform,
aliasEntries
}
]
],
],
needHackRegeneratorRuntimeFunction ? require('./babel-plugin-handle-regeneratorRuntime') : null
].filter(t => t),
platform,
isTypescriptFile: isTypescriptFile(this.resourcePath),
rootDir,
Expand Down

0 comments on commit 64a4aaa

Please sign in to comment.