@shriyanss/js-recon 1.4.1-alpha.4 → 1.4.1-alpha.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/workflows/build-and-prettify.yaml +187 -1
- package/.github/workflows/publish-js-recon.yml +0 -1
- package/.github/workflows/rules-smoke-test.yaml +79 -0
- package/CHANGELOG.md +40 -0
- package/CLAUDE.md +184 -8
- package/README.md +18 -17
- package/build/analyze/engine/csMastSEngine.js +129 -0
- package/build/analyze/engine/csMastSEngine.js.map +1 -0
- package/build/analyze/engine/index.js +15 -0
- package/build/analyze/engine/index.js.map +1 -1
- package/build/analyze/helpers/engineHelpers/findDirectAssignment.js +2 -1
- package/build/analyze/helpers/engineHelpers/findDirectAssignment.js.map +1 -1
- package/build/analyze/helpers/engineHelpers/findMemberExpressionAssignment.js +2 -1
- package/build/analyze/helpers/engineHelpers/findMemberExpressionAssignment.js.map +1 -1
- package/build/analyze/helpers/engineHelpers/resolveFunctionIdentifier.js +2 -1
- package/build/analyze/helpers/engineHelpers/resolveFunctionIdentifier.js.map +1 -1
- package/build/analyze/helpers/engineHelpers/taintFlow.js +11 -2
- package/build/analyze/helpers/engineHelpers/taintFlow.js.map +1 -1
- package/build/analyze/helpers/schemas.js +5 -1
- package/build/analyze/helpers/schemas.js.map +1 -1
- package/build/analyze/helpers/validate.js +3 -3
- package/build/analyze/helpers/validate.js.map +1 -1
- package/build/completion/index.js +432 -0
- package/build/completion/index.js.map +1 -0
- package/build/cs_mast/index.js +94 -3
- package/build/cs_mast/index.js.map +1 -1
- package/build/endpoints/next_js/client_jsFilesHref.js +2 -1
- package/build/endpoints/next_js/client_jsFilesHref.js.map +1 -1
- package/build/endpoints/next_js/client_jsonParse.js +2 -1
- package/build/endpoints/next_js/client_jsonParse.js.map +1 -1
- package/build/endpoints/next_js/client_mappedJsonFile.js +2 -1
- package/build/endpoints/next_js/client_mappedJsonFile.js.map +1 -1
- package/build/endpoints/next_js/client_subsequentRequests.js +2 -1
- package/build/endpoints/next_js/client_subsequentRequests.js.map +1 -1
- package/build/fingerprint/index.js +56 -33
- package/build/fingerprint/index.js.map +1 -1
- package/build/globalConfig.js +1 -1
- package/build/index.js +67 -6
- package/build/index.js.map +1 -1
- package/build/lazyLoad/angular/angular_getFromMainJs.js +2 -1
- package/build/lazyLoad/angular/angular_getFromMainJs.js.map +1 -1
- package/build/lazyLoad/downloadFilesUtil.js +6 -5
- package/build/lazyLoad/downloadFilesUtil.js.map +1 -1
- package/build/lazyLoad/downloadLoadedJsUtil.js +56 -7
- package/build/lazyLoad/downloadLoadedJsUtil.js.map +1 -1
- package/build/lazyLoad/downloadQueue.js +9 -8
- package/build/lazyLoad/downloadQueue.js.map +1 -1
- package/build/lazyLoad/index.js +28 -1
- package/build/lazyLoad/index.js.map +1 -1
- package/build/lazyLoad/next_js/next_GetLazyResourcesBuildManifestJs.js +36 -34
- package/build/lazyLoad/next_js/next_GetLazyResourcesBuildManifestJs.js.map +1 -1
- package/build/lazyLoad/next_js/next_GetLazyResourcesWebpackJs.js +63 -15
- package/build/lazyLoad/next_js/next_GetLazyResourcesWebpackJs.js.map +1 -1
- package/build/lazyLoad/next_js/next_parseLayoutJs.js +80 -68
- package/build/lazyLoad/next_js/next_parseLayoutJs.js.map +1 -1
- package/build/lazyLoad/next_js/next_promiseResolve.js +22 -16
- package/build/lazyLoad/next_js/next_promiseResolve.js.map +1 -1
- package/build/lazyLoad/nuxt_js/nuxt_astParse.js +49 -47
- package/build/lazyLoad/nuxt_js/nuxt_astParse.js.map +1 -1
- package/build/lazyLoad/nuxt_js/nuxt_getFromPageSource.js +3 -1
- package/build/lazyLoad/nuxt_js/nuxt_getFromPageSource.js.map +1 -1
- package/build/lazyLoad/nuxt_js/nuxt_stringAnalysisJSFiles.js +3 -2
- package/build/lazyLoad/nuxt_js/nuxt_stringAnalysisJSFiles.js.map +1 -1
- package/build/lazyLoad/react/react_followImports.js +12 -9
- package/build/lazyLoad/react/react_followImports.js.map +1 -1
- package/build/lazyLoad/react/react_webpackChunkPaths.js +175 -123
- package/build/lazyLoad/react/react_webpackChunkPaths.js.map +1 -1
- package/build/lazyLoad/svelte/svelte_stringAnalysisJSFiles.js +3 -2
- package/build/lazyLoad/svelte/svelte_stringAnalysisJSFiles.js.map +1 -1
- package/build/lazyLoad/techDetect/checkAngularJS.js +27 -6
- package/build/lazyLoad/techDetect/checkAngularJS.js.map +1 -1
- package/build/lazyLoad/techDetect/checkReact.js +8 -0
- package/build/lazyLoad/techDetect/checkReact.js.map +1 -1
- package/build/lazyLoad/techDetect/index.js +23 -2
- package/build/lazyLoad/techDetect/index.js.map +1 -1
- package/build/lazyLoad/vue/vue_RuntimeJs.js +2 -1
- package/build/lazyLoad/vue/vue_RuntimeJs.js.map +1 -1
- package/build/lazyLoad/vue/vue_SingleJsFileOnHome.js +2 -1
- package/build/lazyLoad/vue/vue_SingleJsFileOnHome.js.map +1 -1
- package/build/lazyLoad/vue/vue_discoverJsFiles.js +22 -10
- package/build/lazyLoad/vue/vue_discoverJsFiles.js.map +1 -1
- package/build/lazyLoad/vue/vue_getClientSidePaths.js +2 -1
- package/build/lazyLoad/vue/vue_getClientSidePaths.js.map +1 -1
- package/build/lazyLoad/vue/vue_jsImports.js +2 -1
- package/build/lazyLoad/vue/vue_jsImports.js.map +1 -1
- package/build/lazyLoad/vue/vue_viteMapDeps.js +62 -54
- package/build/lazyLoad/vue/vue_viteMapDeps.js.map +1 -1
- package/build/map/angular_js/getAngularConnections.js +1 -1
- package/build/map/angular_js/getAngularConnections.js.map +1 -1
- package/build/map/graphql/resolveGraphql.js +2 -1
- package/build/map/graphql/resolveGraphql.js.map +1 -1
- package/build/map/next_js/getExports.js +2 -1
- package/build/map/next_js/getExports.js.map +1 -1
- package/build/map/next_js/getFetchInstances.js +2 -1
- package/build/map/next_js/getFetchInstances.js.map +1 -1
- package/build/map/next_js/getTurbopackConnections.js +2 -1
- package/build/map/next_js/getTurbopackConnections.js.map +1 -1
- package/build/map/next_js/getWebpackConnections.js +2 -1
- package/build/map/next_js/getWebpackConnections.js.map +1 -1
- package/build/map/next_js/resolveAxios.js +2 -1
- package/build/map/next_js/resolveAxios.js.map +1 -1
- package/build/map/next_js/resolveAxiosHelpers/directCallsWithoutAssignment.js +2 -1
- package/build/map/next_js/resolveAxiosHelpers/directCallsWithoutAssignment.js.map +1 -1
- package/build/map/next_js/resolveAxiosHelpers/findAxiosInstance.js +2 -1
- package/build/map/next_js/resolveAxiosHelpers/findAxiosInstance.js.map +1 -1
- package/build/map/next_js/resolveAxiosHelpers/findCrossChunkParams.js +2 -1
- package/build/map/next_js/resolveAxiosHelpers/findCrossChunkParams.js.map +1 -1
- package/build/map/next_js/resolveAxiosHelpers/handleAxiosCreate.js +2 -1
- package/build/map/next_js/resolveAxiosHelpers/handleAxiosCreate.js.map +1 -1
- package/build/map/next_js/resolveAxiosHelpers/handleZDotCreate.js +2 -1
- package/build/map/next_js/resolveAxiosHelpers/handleZDotCreate.js.map +1 -1
- package/build/map/next_js/resolveAxiosHelpers/interceptorHeaders.js +2 -1
- package/build/map/next_js/resolveAxiosHelpers/interceptorHeaders.js.map +1 -1
- package/build/map/next_js/resolveAxiosHelpers/processExportedEndpoints.js +2 -1
- package/build/map/next_js/resolveAxiosHelpers/processExportedEndpoints.js.map +1 -1
- package/build/map/next_js/resolveAxiosHelpers/traceAxiosInstanceExports.js +2 -1
- package/build/map/next_js/resolveAxiosHelpers/traceAxiosInstanceExports.js.map +1 -1
- package/build/map/next_js/resolveAxiosHelpers/traceBody.js +2 -1
- package/build/map/next_js/resolveAxiosHelpers/traceBody.js.map +1 -1
- package/build/map/next_js/resolveFetch.js +2 -1
- package/build/map/next_js/resolveFetch.js.map +1 -1
- package/build/map/next_js/resolveNewRequest.js +2 -1
- package/build/map/next_js/resolveNewRequest.js.map +1 -1
- package/build/map/next_js/resolveServerActions.js +2 -1
- package/build/map/next_js/resolveServerActions.js.map +1 -1
- package/build/map/next_js/utils.js +2 -1
- package/build/map/next_js/utils.js.map +1 -1
- package/build/map/react_js/getReactConnections.js +5 -4
- package/build/map/react_js/getReactConnections.js.map +1 -1
- package/build/map/vue_js/crossFileResolver.js +3 -2
- package/build/map/vue_js/crossFileResolver.js.map +1 -1
- package/build/map/vue_js/getViteConnections.js +5 -4
- package/build/map/vue_js/getViteConnections.js.map +1 -1
- package/build/map/vue_js/taint_utils.js +2 -1
- package/build/map/vue_js/taint_utils.js.map +1 -1
- package/build/map/vue_js/vue_resolveFetch.js +3 -2
- package/build/map/vue_js/vue_resolveFetch.js.map +1 -1
- package/build/map/vue_js/vue_resolveHttpClient.js +3 -2
- package/build/map/vue_js/vue_resolveHttpClient.js.map +1 -1
- package/build/map/vue_js/vue_resolveXhr.js +3 -2
- package/build/map/vue_js/vue_resolveXhr.js.map +1 -1
- package/build/refactor/index.js +399 -60
- package/build/refactor/index.js.map +1 -1
- package/build/refactor/next/helpers.js +194 -13
- package/build/refactor/next/helpers.js.map +1 -1
- package/build/refactor/next/index.js +223 -36
- package/build/refactor/next/index.js.map +1 -1
- package/build/refactor/next/transform.js +905 -55
- package/build/refactor/next/transform.js.map +1 -1
- package/build/refactor/react/index.js +3 -3
- package/build/refactor/react/index.js.map +1 -1
- package/build/refactor/react/transform.js +2 -1
- package/build/refactor/react/transform.js.map +1 -1
- package/build/refactor/react/validator.js +2 -1
- package/build/refactor/react/validator.js.map +1 -1
- package/build/refactor/react-vite/index.js +39 -5
- package/build/refactor/react-vite/index.js.map +1 -1
- package/build/refactor/react-vite/vendor-analyze.js +3 -3
- package/build/refactor/react-vite/vendor-analyze.js.map +1 -1
- package/build/refactor/remote/config.js +17 -1
- package/build/refactor/remote/config.js.map +1 -1
- package/build/refactor/remote/hf-client.js +27 -2
- package/build/refactor/remote/hf-client.js.map +1 -1
- package/build/refactor/remote/version-detect.js +355 -0
- package/build/refactor/remote/version-detect.js.map +1 -0
- package/build/refactor/vue/index.js +176 -0
- package/build/refactor/vue/index.js.map +1 -0
- package/build/refactor/vue/vendor-analyze-vue.js +271 -0
- package/build/refactor/vue/vendor-analyze-vue.js.map +1 -0
- package/build/refactor/vue/vite.js +216 -0
- package/build/refactor/vue/vite.js.map +1 -0
- package/build/report/utility/genHtml.js +14 -3
- package/build/report/utility/genHtml.js.map +1 -1
- package/build/run/bundler-detect.js +140 -0
- package/build/run/bundler-detect.js.map +1 -0
- package/build/run/index.js +91 -5
- package/build/run/index.js.map +1 -1
- package/build/strings/index.js +6 -2
- package/build/strings/index.js.map +1 -1
- package/build/strings/trufflehog.js +71 -0
- package/build/strings/trufflehog.js.map +1 -0
- package/build/utility/makeReq.js +10 -9
- package/build/utility/makeReq.js.map +1 -1
- package/build/utility/postmanGenerator.js +2 -2
- package/build/utility/postmanGenerator.js.map +1 -1
- package/build/utility/puppeteerInstance.js +7 -1
- package/build/utility/puppeteerInstance.js.map +1 -1
- package/package.json +9 -5
- package/scripts/smoke-test.js +74 -0
|
@@ -1,31 +1,674 @@
|
|
|
1
|
+
var _a;
|
|
1
2
|
import _traverse from "@babel/traverse";
|
|
3
|
+
import _generator from "@babel/generator";
|
|
2
4
|
import * as t from "@babel/types";
|
|
3
|
-
import { tryExtractTurbopackRequire, tryExtractDefinePropertyExport, isEsModuleMarker, isInteropBoilerplate, tryExtractForInExportLoop, extractExportsFromMap, makeExportStatement, } from "./helpers.js";
|
|
4
|
-
const traverse = _traverse.default;
|
|
5
|
+
import { tryExtractTurbopackRequire, tryExtractWebpackRequire, tryExtractRuntimeSExport, tryExtractDefinePropertyExport, tryExtractBatchIIFEExports, tryExtractRequireDotD, isRequireDotR, isEsModuleMarker, isInteropBoilerplate, tryExtractModuleExportsRhs, tryExtractForInExportLoop, extractExportsFromMap, makeExportStatement, } from "./helpers.js";
|
|
6
|
+
const traverse = ((_a = _traverse.default) !== null && _a !== void 0 ? _a : _traverse);
|
|
7
|
+
const generate = _generator.default;
|
|
8
|
+
// ---------------------------------------------------------------------------
|
|
9
|
+
// Pass F — JSX recovery
|
|
10
|
+
// ---------------------------------------------------------------------------
|
|
11
|
+
function exprToJsxName(expr) {
|
|
12
|
+
var _a;
|
|
13
|
+
if (t.isStringLiteral(expr))
|
|
14
|
+
return t.jsxIdentifier(expr.value);
|
|
15
|
+
if (t.isTemplateLiteral(expr) && expr.expressions.length === 0 && expr.quasis.length === 1) {
|
|
16
|
+
const raw = (_a = expr.quasis[0].value.cooked) !== null && _a !== void 0 ? _a : expr.quasis[0].value.raw;
|
|
17
|
+
if (raw)
|
|
18
|
+
return t.jsxIdentifier(raw);
|
|
19
|
+
}
|
|
20
|
+
if (t.isIdentifier(expr))
|
|
21
|
+
return t.jsxIdentifier(expr.name);
|
|
22
|
+
if (t.isMemberExpression(expr) && !expr.computed && t.isIdentifier(expr.property)) {
|
|
23
|
+
const obj = exprToJsxName(expr.object);
|
|
24
|
+
if (obj)
|
|
25
|
+
return t.jsxMemberExpression(obj, t.jsxIdentifier(expr.property.name));
|
|
26
|
+
}
|
|
27
|
+
return null;
|
|
28
|
+
}
|
|
29
|
+
function exprToJsxAttrValue(expr) {
|
|
30
|
+
if (t.isStringLiteral(expr))
|
|
31
|
+
return expr;
|
|
32
|
+
return t.jsxExpressionContainer(expr);
|
|
33
|
+
}
|
|
34
|
+
function childToJsxChild(child) {
|
|
35
|
+
var _a;
|
|
36
|
+
if (t.isStringLiteral(child))
|
|
37
|
+
return t.jsxText(child.value);
|
|
38
|
+
if (t.isTemplateLiteral(child) && child.expressions.length === 0 && child.quasis.length === 1) {
|
|
39
|
+
const raw = (_a = child.quasis[0].value.cooked) !== null && _a !== void 0 ? _a : child.quasis[0].value.raw;
|
|
40
|
+
if (raw !== undefined)
|
|
41
|
+
return t.jsxText(raw);
|
|
42
|
+
}
|
|
43
|
+
if (t.isJSXElement(child) || t.isJSXFragment(child))
|
|
44
|
+
return child;
|
|
45
|
+
if (t.isCallExpression(child)) {
|
|
46
|
+
const converted = tryConvertToJSX(child);
|
|
47
|
+
if (converted)
|
|
48
|
+
return converted;
|
|
49
|
+
}
|
|
50
|
+
return t.jsxExpressionContainer(child);
|
|
51
|
+
}
|
|
52
|
+
function tryUnpackSpreadIIFE(expr) {
|
|
53
|
+
if (!t.isCallExpression(expr))
|
|
54
|
+
return null;
|
|
55
|
+
const callee = expr.callee;
|
|
56
|
+
if (!t.isFunctionExpression(callee))
|
|
57
|
+
return null;
|
|
58
|
+
if (callee.params.length !== 1 || !t.isIdentifier(callee.params[0]))
|
|
59
|
+
return null;
|
|
60
|
+
const paramName = callee.params[0].name;
|
|
61
|
+
const body = callee.body.body;
|
|
62
|
+
if (body.length < 2)
|
|
63
|
+
return null;
|
|
64
|
+
const hasArgumentsLoop = body.some((s) => {
|
|
65
|
+
if (!t.isForStatement(s) || !s.test || !t.isBinaryExpression(s.test))
|
|
66
|
+
return false;
|
|
67
|
+
const right = s.test.right;
|
|
68
|
+
return (s.test.operator === "<" &&
|
|
69
|
+
t.isMemberExpression(right) &&
|
|
70
|
+
t.isIdentifier(right.object, { name: "arguments" }) &&
|
|
71
|
+
t.isIdentifier(right.property, { name: "length" }));
|
|
72
|
+
});
|
|
73
|
+
if (!hasArgumentsLoop)
|
|
74
|
+
return null;
|
|
75
|
+
const hasReturn = body.some((s) => t.isReturnStatement(s) && s.argument && t.isIdentifier(s.argument, { name: paramName }));
|
|
76
|
+
if (!hasReturn)
|
|
77
|
+
return null;
|
|
78
|
+
const args = expr.arguments;
|
|
79
|
+
if (args.length < 1)
|
|
80
|
+
return null;
|
|
81
|
+
return { base: args[0], spreads: args.slice(1) };
|
|
82
|
+
}
|
|
83
|
+
function propsArgToAttrsAndChildren(propsArg) {
|
|
84
|
+
const attrs = [];
|
|
85
|
+
const childExprs = [];
|
|
86
|
+
const processObjectExpr = (obj) => {
|
|
87
|
+
for (const prop of obj.properties) {
|
|
88
|
+
if (t.isSpreadElement(prop)) {
|
|
89
|
+
attrs.push(t.jsxSpreadAttribute(prop.argument));
|
|
90
|
+
continue;
|
|
91
|
+
}
|
|
92
|
+
if (!t.isObjectProperty(prop) || prop.computed)
|
|
93
|
+
continue;
|
|
94
|
+
const keyNode = prop.key;
|
|
95
|
+
const valNode = prop.value;
|
|
96
|
+
const keyName = t.isIdentifier(keyNode) ? keyNode.name : t.isStringLiteral(keyNode) ? keyNode.value : null;
|
|
97
|
+
if (!keyName)
|
|
98
|
+
continue;
|
|
99
|
+
if (keyName === "children") {
|
|
100
|
+
if (t.isArrayExpression(valNode)) {
|
|
101
|
+
for (const el of valNode.elements) {
|
|
102
|
+
if (el)
|
|
103
|
+
childExprs.push(el);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
else {
|
|
107
|
+
childExprs.push(valNode);
|
|
108
|
+
}
|
|
109
|
+
continue;
|
|
110
|
+
}
|
|
111
|
+
attrs.push(t.jsxAttribute(t.jsxIdentifier(keyName), exprToJsxAttrValue(valNode)));
|
|
112
|
+
}
|
|
113
|
+
};
|
|
114
|
+
if (t.isObjectExpression(propsArg)) {
|
|
115
|
+
processObjectExpr(propsArg);
|
|
116
|
+
}
|
|
117
|
+
else {
|
|
118
|
+
const unpacked = tryUnpackSpreadIIFE(propsArg);
|
|
119
|
+
if (unpacked) {
|
|
120
|
+
if (t.isObjectExpression(unpacked.base)) {
|
|
121
|
+
processObjectExpr(unpacked.base);
|
|
122
|
+
}
|
|
123
|
+
else {
|
|
124
|
+
attrs.push(t.jsxSpreadAttribute(unpacked.base));
|
|
125
|
+
}
|
|
126
|
+
for (const spreadExpr of unpacked.spreads) {
|
|
127
|
+
attrs.push(t.jsxSpreadAttribute(spreadExpr));
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
else {
|
|
131
|
+
attrs.push(t.jsxSpreadAttribute(propsArg));
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
return { attrs, childExprs };
|
|
135
|
+
}
|
|
136
|
+
const JSX_METHOD_NAMES = new Set(["jsx", "jsxs", "jsxDEV"]);
|
|
137
|
+
function getJsxMethodName(callee) {
|
|
138
|
+
if (t.isIdentifier(callee) && JSX_METHOD_NAMES.has(callee.name)) {
|
|
139
|
+
return callee.name;
|
|
140
|
+
}
|
|
141
|
+
if (t.isMemberExpression(callee) && !callee.computed) {
|
|
142
|
+
const prop = callee.property;
|
|
143
|
+
if (t.isIdentifier(prop) && JSX_METHOD_NAMES.has(prop.name)) {
|
|
144
|
+
return prop.name;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
if (t.isSequenceExpression(callee)) {
|
|
148
|
+
const exprs = callee.expressions;
|
|
149
|
+
const last = exprs[exprs.length - 1];
|
|
150
|
+
if (last && t.isMemberExpression(last) && !last.computed) {
|
|
151
|
+
const prop = last.property;
|
|
152
|
+
if (t.isIdentifier(prop) && JSX_METHOD_NAMES.has(prop.name)) {
|
|
153
|
+
return prop.name;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
return null;
|
|
158
|
+
}
|
|
159
|
+
function tryConvertToJSX(call) {
|
|
160
|
+
const callee = call.callee;
|
|
161
|
+
const methodName = getJsxMethodName(callee);
|
|
162
|
+
if (!methodName)
|
|
163
|
+
return null;
|
|
164
|
+
if (call.arguments.length < 2)
|
|
165
|
+
return null;
|
|
166
|
+
const tagArg = call.arguments[0];
|
|
167
|
+
const propsArg = call.arguments[1];
|
|
168
|
+
const jsxName = exprToJsxName(tagArg);
|
|
169
|
+
if (!jsxName)
|
|
170
|
+
return null;
|
|
171
|
+
const { attrs, childExprs } = propsArgToAttrsAndChildren(propsArg);
|
|
172
|
+
const children = childExprs.map((e) => childToJsxChild(e)).filter(Boolean);
|
|
173
|
+
if (t.isJSXIdentifier(jsxName) && jsxName.name === "Fragment") {
|
|
174
|
+
return t.jsxFragment(t.jsxOpeningFragment(), t.jsxClosingFragment(), children);
|
|
175
|
+
}
|
|
176
|
+
const jsxAttrs = attrs.filter((a) => t.isJSXAttribute(a) || t.isJSXSpreadAttribute(a));
|
|
177
|
+
const selfClosing = children.length === 0;
|
|
178
|
+
const openingElement = t.jsxOpeningElement(jsxName, jsxAttrs, selfClosing);
|
|
179
|
+
const closingElement = selfClosing ? null : t.jsxClosingElement(jsxName);
|
|
180
|
+
return t.jsxElement(openingElement, closingElement, children, selfClosing);
|
|
181
|
+
}
|
|
182
|
+
function recoverJSX(statements) {
|
|
183
|
+
const syntheticFile = t.file(t.program(statements, [], "module"));
|
|
184
|
+
traverse(syntheticFile, {
|
|
185
|
+
CallExpression(p) {
|
|
186
|
+
const jsxEl = tryConvertToJSX(p.node);
|
|
187
|
+
if (!jsxEl)
|
|
188
|
+
return;
|
|
189
|
+
p.replaceWith(jsxEl);
|
|
190
|
+
},
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
// ---------------------------------------------------------------------------
|
|
194
|
+
// Pass E — Babel slicedToArray collapse
|
|
195
|
+
// ---------------------------------------------------------------------------
|
|
196
|
+
function isSlicedToArrayTail(node) {
|
|
197
|
+
if (!t.isCallExpression(node) || node.arguments.length !== 0)
|
|
198
|
+
return false;
|
|
199
|
+
const callee = node.callee;
|
|
200
|
+
if (!t.isFunctionExpression(callee) && !t.isArrowFunctionExpression(callee))
|
|
201
|
+
return false;
|
|
202
|
+
const body = callee.body;
|
|
203
|
+
if (!t.isBlockStatement(body))
|
|
204
|
+
return false;
|
|
205
|
+
return body.body.some((s) => t.isThrowStatement(s) &&
|
|
206
|
+
t.isNewExpression(s.argument) &&
|
|
207
|
+
t.isIdentifier(s.argument.callee, { name: "TypeError" }));
|
|
208
|
+
}
|
|
209
|
+
function hasSlicedToArrayTail(node) {
|
|
210
|
+
if (isSlicedToArrayTail(node))
|
|
211
|
+
return true;
|
|
212
|
+
if (t.isLogicalExpression(node) && node.operator === "||")
|
|
213
|
+
return hasSlicedToArrayTail(node.right);
|
|
214
|
+
return false;
|
|
215
|
+
}
|
|
216
|
+
function tryDetectSlicedToArray(decl) {
|
|
217
|
+
let resultVar = null;
|
|
218
|
+
let actualExpr = null;
|
|
219
|
+
for (const d of decl.declarations) {
|
|
220
|
+
if (!d.init || !t.isSequenceExpression(d.init))
|
|
221
|
+
continue;
|
|
222
|
+
const exprs = d.init.expressions;
|
|
223
|
+
if (exprs.length < 3)
|
|
224
|
+
continue;
|
|
225
|
+
if (!t.isAssignmentExpression(exprs[0]) || exprs[0].operator !== "=")
|
|
226
|
+
continue;
|
|
227
|
+
const firstAssign = exprs[0];
|
|
228
|
+
if (!t.isIdentifier(firstAssign.left))
|
|
229
|
+
continue;
|
|
230
|
+
if (!t.isAssignmentExpression(exprs[1]) || !t.isNumericLiteral(exprs[1].right))
|
|
231
|
+
continue;
|
|
232
|
+
if (!hasSlicedToArrayTail(exprs[exprs.length - 1]))
|
|
233
|
+
continue;
|
|
234
|
+
resultVar = t.isIdentifier(d.id) ? d.id.name : null;
|
|
235
|
+
if (!resultVar)
|
|
236
|
+
continue;
|
|
237
|
+
actualExpr = firstAssign.right;
|
|
238
|
+
break;
|
|
239
|
+
}
|
|
240
|
+
if (!resultVar || !actualExpr)
|
|
241
|
+
return null;
|
|
242
|
+
const targets = [];
|
|
243
|
+
for (const d of decl.declarations) {
|
|
244
|
+
if (!d.init || !t.isMemberExpression(d.init))
|
|
245
|
+
continue;
|
|
246
|
+
if (!t.isIdentifier(d.init.object, { name: resultVar }))
|
|
247
|
+
continue;
|
|
248
|
+
if (!d.init.computed || !t.isNumericLiteral(d.init.property))
|
|
249
|
+
continue;
|
|
250
|
+
if (!t.isIdentifier(d.id))
|
|
251
|
+
continue;
|
|
252
|
+
targets.push({ id: d.id, index: d.init.property.value });
|
|
253
|
+
}
|
|
254
|
+
return targets.length > 0 ? { actualExpr, resultVar, targets } : null;
|
|
255
|
+
}
|
|
256
|
+
function collapseSlicedToArray(statements) {
|
|
257
|
+
const out = [];
|
|
258
|
+
for (const stmt of statements) {
|
|
259
|
+
if (t.isBlockStatement(stmt)) {
|
|
260
|
+
stmt.body = collapseSlicedToArray(stmt.body);
|
|
261
|
+
out.push(stmt);
|
|
262
|
+
continue;
|
|
263
|
+
}
|
|
264
|
+
if (!t.isVariableDeclaration(stmt)) {
|
|
265
|
+
out.push(stmt);
|
|
266
|
+
continue;
|
|
267
|
+
}
|
|
268
|
+
const match = tryDetectSlicedToArray(stmt);
|
|
269
|
+
if (!match) {
|
|
270
|
+
out.push(stmt);
|
|
271
|
+
continue;
|
|
272
|
+
}
|
|
273
|
+
const { actualExpr, resultVar, targets } = match;
|
|
274
|
+
const sorted = [...targets].sort((a, b) => a.index - b.index);
|
|
275
|
+
const pattern = t.arrayPattern(sorted.map((tgt) => tgt.id));
|
|
276
|
+
out.push(t.variableDeclaration("const", [t.variableDeclarator(pattern, actualExpr)]));
|
|
277
|
+
}
|
|
278
|
+
return out;
|
|
279
|
+
}
|
|
280
|
+
function isNamedSlicedToArrayHelper(stmt) {
|
|
281
|
+
if (!t.isFunctionDeclaration(stmt) || !stmt.id || stmt.params.length !== 2)
|
|
282
|
+
return null;
|
|
283
|
+
if (stmt.id.name === "_slicedToArray" || stmt.id.name === "slicedToArray")
|
|
284
|
+
return stmt.id.name;
|
|
285
|
+
const body = stmt.body.body;
|
|
286
|
+
if (body.length === 0)
|
|
287
|
+
return null;
|
|
288
|
+
const last = body[body.length - 1];
|
|
289
|
+
if (!t.isReturnStatement(last) || !last.argument)
|
|
290
|
+
return null;
|
|
291
|
+
const hasTypeError = (node) => {
|
|
292
|
+
if (t.isCallExpression(node) &&
|
|
293
|
+
(t.isFunctionExpression(node.callee) || t.isArrowFunctionExpression(node.callee))) {
|
|
294
|
+
const innerBody = node.callee.body;
|
|
295
|
+
if (t.isBlockStatement(innerBody)) {
|
|
296
|
+
return innerBody.body.some((s) => t.isThrowStatement(s) &&
|
|
297
|
+
t.isNewExpression(s.argument) &&
|
|
298
|
+
t.isIdentifier(s.argument.callee, { name: "TypeError" }) &&
|
|
299
|
+
s.argument.arguments.some((a) => t.isStringLiteral(a) && a.value.includes("non-iterable")));
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
if (t.isLogicalExpression(node) && node.operator === "||")
|
|
303
|
+
return hasTypeError(node.right);
|
|
304
|
+
return false;
|
|
305
|
+
};
|
|
306
|
+
return hasTypeError(last.argument) ? stmt.id.name : null;
|
|
307
|
+
}
|
|
308
|
+
function collapseSlicedToArrayCalls(stmts, helperNames) {
|
|
309
|
+
if (helperNames.size === 0)
|
|
310
|
+
return stmts;
|
|
311
|
+
const out = [];
|
|
312
|
+
for (const stmt of stmts) {
|
|
313
|
+
if (!t.isVariableDeclaration(stmt)) {
|
|
314
|
+
out.push(stmt);
|
|
315
|
+
continue;
|
|
316
|
+
}
|
|
317
|
+
const decls = stmt.declarations;
|
|
318
|
+
const newDecls = [];
|
|
319
|
+
let i = 0;
|
|
320
|
+
while (i < decls.length) {
|
|
321
|
+
const d = decls[i];
|
|
322
|
+
if (!t.isIdentifier(d.id) || !d.init || !t.isCallExpression(d.init)) {
|
|
323
|
+
newDecls.push(d);
|
|
324
|
+
i++;
|
|
325
|
+
continue;
|
|
326
|
+
}
|
|
327
|
+
const callExpr = d.init;
|
|
328
|
+
const callee = callExpr.callee;
|
|
329
|
+
if (!t.isIdentifier(callee) || !helperNames.has(callee.name)) {
|
|
330
|
+
newDecls.push(d);
|
|
331
|
+
i++;
|
|
332
|
+
continue;
|
|
333
|
+
}
|
|
334
|
+
const tempName = d.id.name;
|
|
335
|
+
let actualExpr = callExpr.arguments[0];
|
|
336
|
+
if (t.isIdentifier(actualExpr) &&
|
|
337
|
+
newDecls.length > 0 &&
|
|
338
|
+
t.isIdentifier(newDecls[newDecls.length - 1].id) &&
|
|
339
|
+
newDecls[newDecls.length - 1].id.name === actualExpr.name &&
|
|
340
|
+
newDecls[newDecls.length - 1].init != null) {
|
|
341
|
+
actualExpr = newDecls[newDecls.length - 1].init;
|
|
342
|
+
newDecls.pop();
|
|
343
|
+
}
|
|
344
|
+
const targets = [];
|
|
345
|
+
let j = i + 1;
|
|
346
|
+
while (j < decls.length) {
|
|
347
|
+
const nd = decls[j];
|
|
348
|
+
if (!t.isIdentifier(nd.id) ||
|
|
349
|
+
!nd.init ||
|
|
350
|
+
!t.isMemberExpression(nd.init) ||
|
|
351
|
+
!nd.init.computed ||
|
|
352
|
+
!t.isIdentifier(nd.init.object, { name: tempName }) ||
|
|
353
|
+
!t.isNumericLiteral(nd.init.property))
|
|
354
|
+
break;
|
|
355
|
+
targets.push({
|
|
356
|
+
id: nd.id,
|
|
357
|
+
index: nd.init.property.value,
|
|
358
|
+
});
|
|
359
|
+
j++;
|
|
360
|
+
}
|
|
361
|
+
if (targets.length > 0) {
|
|
362
|
+
const sorted = [...targets].sort((a, b) => a.index - b.index);
|
|
363
|
+
const pattern = t.arrayPattern(sorted.map((tgt) => tgt.id));
|
|
364
|
+
newDecls.push(t.variableDeclarator(pattern, actualExpr));
|
|
365
|
+
i = j;
|
|
366
|
+
}
|
|
367
|
+
else {
|
|
368
|
+
newDecls.push(d);
|
|
369
|
+
i++;
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
if (newDecls.length > 0)
|
|
373
|
+
out.push(t.variableDeclaration(stmt.kind, newDecls));
|
|
374
|
+
}
|
|
375
|
+
return out;
|
|
376
|
+
}
|
|
377
|
+
function collapseNamedSlicedToArray(statements) {
|
|
378
|
+
const helperNames = new Set();
|
|
379
|
+
for (const stmt of statements) {
|
|
380
|
+
const name = isNamedSlicedToArrayHelper(stmt);
|
|
381
|
+
if (name)
|
|
382
|
+
helperNames.add(name);
|
|
383
|
+
}
|
|
384
|
+
if (helperNames.size === 0)
|
|
385
|
+
return statements;
|
|
386
|
+
const withoutHelpers = statements.filter((s) => !(t.isFunctionDeclaration(s) && s.id && helperNames.has(s.id.name)));
|
|
387
|
+
const topCollapsed = collapseSlicedToArrayCalls(withoutHelpers, helperNames);
|
|
388
|
+
const syntheticFile = t.file(t.program(topCollapsed, [], "module"));
|
|
389
|
+
traverse(syntheticFile, {
|
|
390
|
+
BlockStatement(p) {
|
|
391
|
+
const collapsed = collapseSlicedToArrayCalls(p.node.body, helperNames);
|
|
392
|
+
if (collapsed !== p.node.body)
|
|
393
|
+
p.node.body = collapsed;
|
|
394
|
+
},
|
|
395
|
+
MemberExpression(p) {
|
|
396
|
+
const node = p.node;
|
|
397
|
+
if (!node.computed)
|
|
398
|
+
return;
|
|
399
|
+
if (!t.isNumericLiteral(node.property))
|
|
400
|
+
return;
|
|
401
|
+
if (!t.isCallExpression(node.object))
|
|
402
|
+
return;
|
|
403
|
+
const call = node.object;
|
|
404
|
+
if (!t.isIdentifier(call.callee) || !helperNames.has(call.callee.name))
|
|
405
|
+
return;
|
|
406
|
+
const actualExpr = call.arguments[0];
|
|
407
|
+
if (!actualExpr)
|
|
408
|
+
return;
|
|
409
|
+
p.replaceWith(t.memberExpression(actualExpr, node.property, true));
|
|
410
|
+
p.skip();
|
|
411
|
+
},
|
|
412
|
+
});
|
|
413
|
+
return topCollapsed;
|
|
414
|
+
}
|
|
415
|
+
function collapseSlicedToArrayDeep(statements) {
|
|
416
|
+
const afterNamed = collapseNamedSlicedToArray(statements);
|
|
417
|
+
const top = collapseSlicedToArray(afterNamed);
|
|
418
|
+
const syntheticFile = t.file(t.program(top, [], "module"));
|
|
419
|
+
traverse(syntheticFile, {
|
|
420
|
+
BlockStatement(p) {
|
|
421
|
+
const collapsed = collapseSlicedToArray(p.node.body);
|
|
422
|
+
if (collapsed !== p.node.body) {
|
|
423
|
+
p.node.body = collapsed;
|
|
424
|
+
}
|
|
425
|
+
},
|
|
426
|
+
});
|
|
427
|
+
return top;
|
|
428
|
+
}
|
|
429
|
+
// ---------------------------------------------------------------------------
|
|
430
|
+
// Pass G — remove Babel helpers
|
|
431
|
+
// ---------------------------------------------------------------------------
|
|
432
|
+
function isBabelArrayLikeToArrayHelper(stmt) {
|
|
433
|
+
if (!t.isFunctionDeclaration(stmt) || !stmt.id)
|
|
434
|
+
return false;
|
|
435
|
+
const body = stmt.body.body;
|
|
436
|
+
if (body.length < 2 || body.length > 4)
|
|
437
|
+
return false;
|
|
438
|
+
return body.some((s) => {
|
|
439
|
+
if (!t.isForStatement(s))
|
|
440
|
+
return false;
|
|
441
|
+
const init = s.init;
|
|
442
|
+
if (!t.isVariableDeclaration(init))
|
|
443
|
+
return false;
|
|
444
|
+
return init.declarations.some((d) => d.init &&
|
|
445
|
+
t.isCallExpression(d.init) &&
|
|
446
|
+
t.isIdentifier(d.init.callee, { name: "Array" }));
|
|
447
|
+
});
|
|
448
|
+
}
|
|
449
|
+
function isBabelTypeofHelper(stmt) {
|
|
450
|
+
if (!t.isFunctionDeclaration(stmt) || !stmt.id || stmt.params.length !== 1)
|
|
451
|
+
return false;
|
|
452
|
+
const body = stmt.body.body;
|
|
453
|
+
if (body.length !== 1 || !t.isReturnStatement(body[0]))
|
|
454
|
+
return false;
|
|
455
|
+
const arg = body[0].argument;
|
|
456
|
+
if (!arg || !t.isSequenceExpression(arg))
|
|
457
|
+
return false;
|
|
458
|
+
const exprs = arg.expressions;
|
|
459
|
+
if (exprs.length !== 2)
|
|
460
|
+
return false;
|
|
461
|
+
const first = exprs[0];
|
|
462
|
+
return (t.isAssignmentExpression(first) &&
|
|
463
|
+
t.isIdentifier(first.left, { name: stmt.id.name }));
|
|
464
|
+
}
|
|
465
|
+
function isBabelDefinePropertyHelper(stmt) {
|
|
466
|
+
if (!t.isFunctionDeclaration(stmt) || !stmt.id || stmt.params.length !== 3)
|
|
467
|
+
return false;
|
|
468
|
+
const bodyCode = stmt.body.body;
|
|
469
|
+
const hasDefProp = (node) => {
|
|
470
|
+
if (t.isCallExpression(node) &&
|
|
471
|
+
t.isMemberExpression(node.callee) &&
|
|
472
|
+
t.isIdentifier(node.callee.object, { name: "Object" }) &&
|
|
473
|
+
t.isIdentifier(node.callee.property, {
|
|
474
|
+
name: "defineProperty",
|
|
475
|
+
})) {
|
|
476
|
+
const args = node.arguments;
|
|
477
|
+
if (args.length >= 3 && t.isObjectExpression(args[2])) {
|
|
478
|
+
const props = args[2].properties;
|
|
479
|
+
const keys = props
|
|
480
|
+
.filter((p) => t.isObjectProperty(p) && t.isIdentifier(p.key))
|
|
481
|
+
.map((p) => p.key.name);
|
|
482
|
+
return keys.includes("enumerable") && keys.includes("configurable") && keys.includes("writable");
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
return false;
|
|
486
|
+
};
|
|
487
|
+
const walk = (node) => {
|
|
488
|
+
if (hasDefProp(node))
|
|
489
|
+
return true;
|
|
490
|
+
for (const key of Object.keys(node)) {
|
|
491
|
+
const child = node[key];
|
|
492
|
+
if (!child || typeof child !== "object")
|
|
493
|
+
continue;
|
|
494
|
+
if (Array.isArray(child)) {
|
|
495
|
+
if (child.some((c) => c && typeof c === "object" && "type" in c && walk(c)))
|
|
496
|
+
return true;
|
|
497
|
+
}
|
|
498
|
+
else if ("type" in child) {
|
|
499
|
+
if (walk(child))
|
|
500
|
+
return true;
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
return false;
|
|
504
|
+
};
|
|
505
|
+
return bodyCode.some((s) => walk(s));
|
|
506
|
+
}
|
|
507
|
+
function isBabelObjectSpreadHelper(stmt) {
|
|
508
|
+
if (!t.isFunctionDeclaration(stmt) || !stmt.id || stmt.params.length !== 2)
|
|
509
|
+
return false;
|
|
510
|
+
const body = stmt.body.body;
|
|
511
|
+
if (body.length < 2)
|
|
512
|
+
return false;
|
|
513
|
+
const first = body[0];
|
|
514
|
+
if (!t.isVariableDeclaration(first))
|
|
515
|
+
return false;
|
|
516
|
+
const firstIsObjectKeys = first.declarations.some((d) => {
|
|
517
|
+
if (!d.init || !t.isCallExpression(d.init))
|
|
518
|
+
return false;
|
|
519
|
+
const callee = d.init.callee;
|
|
520
|
+
return (t.isMemberExpression(callee) &&
|
|
521
|
+
t.isIdentifier(callee.object, { name: "Object" }) &&
|
|
522
|
+
t.isIdentifier(callee.property, { name: "keys" }));
|
|
523
|
+
});
|
|
524
|
+
if (!firstIsObjectKeys)
|
|
525
|
+
return false;
|
|
526
|
+
return JSON.stringify(body).includes('"getOwnPropertySymbols"');
|
|
527
|
+
}
|
|
528
|
+
function isBabelObjectSpread2Helper(stmt) {
|
|
529
|
+
if (!t.isFunctionDeclaration(stmt))
|
|
530
|
+
return false;
|
|
531
|
+
if (stmt.params.length !== 1)
|
|
532
|
+
return false;
|
|
533
|
+
const body = stmt.body.body;
|
|
534
|
+
const hasArgumentsLength = JSON.stringify(body).includes('"arguments"');
|
|
535
|
+
const hasDefineProperties = JSON.stringify(body).includes('"defineProperties"') || JSON.stringify(body).includes('"defineProperty"');
|
|
536
|
+
return hasArgumentsLength && hasDefineProperties;
|
|
537
|
+
}
|
|
538
|
+
function isBabelOwnKeysHelper(stmt) {
|
|
539
|
+
if (!t.isFunctionDeclaration(stmt))
|
|
540
|
+
return false;
|
|
541
|
+
const params = stmt.params;
|
|
542
|
+
if (params.length < 1 || params.length > 2)
|
|
543
|
+
return false;
|
|
544
|
+
const body = stmt.body.body;
|
|
545
|
+
if (body.length === 0)
|
|
546
|
+
return false;
|
|
547
|
+
const first = body[0];
|
|
548
|
+
if (!t.isVariableDeclaration(first))
|
|
549
|
+
return false;
|
|
550
|
+
const bodyStr = JSON.stringify(body);
|
|
551
|
+
return bodyStr.includes('"keys"') && bodyStr.includes('"getOwnPropertySymbols"');
|
|
552
|
+
}
|
|
553
|
+
const NAMED_BABEL_ARRAY_HELPERS = new Set([
|
|
554
|
+
"_arrayWithHoles",
|
|
555
|
+
"_arrayLikeToArray",
|
|
556
|
+
"_iterableToArrayLimit",
|
|
557
|
+
"_unsupportedIterableToArray",
|
|
558
|
+
"_nonIterableRest",
|
|
559
|
+
]);
|
|
560
|
+
function isNamedBabelArrayHelper(stmt) {
|
|
561
|
+
return t.isFunctionDeclaration(stmt) && stmt.id != null && NAMED_BABEL_ARRAY_HELPERS.has(stmt.id.name);
|
|
562
|
+
}
|
|
563
|
+
function removeBabelHelpers(stmts) {
|
|
564
|
+
return stmts.filter((s) => !isBabelArrayLikeToArrayHelper(s) &&
|
|
565
|
+
!isBabelTypeofHelper(s) &&
|
|
566
|
+
!isBabelDefinePropertyHelper(s) &&
|
|
567
|
+
!isBabelObjectSpreadHelper(s) &&
|
|
568
|
+
!isBabelObjectSpread2Helper(s) &&
|
|
569
|
+
!isBabelOwnKeysHelper(s) &&
|
|
570
|
+
!isNamedBabelArrayHelper(s));
|
|
571
|
+
}
|
|
572
|
+
// ---------------------------------------------------------------------------
|
|
573
|
+
// Pass H — prune unused named imports
|
|
574
|
+
// ---------------------------------------------------------------------------
|
|
575
|
+
function collectReferencedNames(stmts) {
|
|
576
|
+
const names = new Set();
|
|
577
|
+
const syntheticFile = t.file(t.program(stmts, [], "module"));
|
|
578
|
+
traverse(syntheticFile, {
|
|
579
|
+
Identifier(p) {
|
|
580
|
+
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
581
|
+
if ((_a = p.parentPath) === null || _a === void 0 ? void 0 : _a.isImportSpecifier())
|
|
582
|
+
return;
|
|
583
|
+
if ((_b = p.parentPath) === null || _b === void 0 ? void 0 : _b.isImportDefaultSpecifier())
|
|
584
|
+
return;
|
|
585
|
+
if ((_c = p.parentPath) === null || _c === void 0 ? void 0 : _c.isImportNamespaceSpecifier())
|
|
586
|
+
return;
|
|
587
|
+
if (((_d = p.parentPath) === null || _d === void 0 ? void 0 : _d.isMemberExpression()) &&
|
|
588
|
+
!p.parent.computed &&
|
|
589
|
+
p.parentPath.get("property") === p)
|
|
590
|
+
return;
|
|
591
|
+
if (((_e = p.parentPath) === null || _e === void 0 ? void 0 : _e.isObjectProperty()) &&
|
|
592
|
+
!p.parent.computed &&
|
|
593
|
+
p.parentPath.get("key") === p)
|
|
594
|
+
return;
|
|
595
|
+
if ((_f = p.parentPath) === null || _f === void 0 ? void 0 : _f.isJSXAttribute())
|
|
596
|
+
return;
|
|
597
|
+
if (((_g = p.parentPath) === null || _g === void 0 ? void 0 : _g.isJSXOpeningElement()) || ((_h = p.parentPath) === null || _h === void 0 ? void 0 : _h.isJSXClosingElement()))
|
|
598
|
+
return;
|
|
599
|
+
names.add(p.node.name);
|
|
600
|
+
},
|
|
601
|
+
JSXIdentifier(p) {
|
|
602
|
+
names.add(p.node.name);
|
|
603
|
+
},
|
|
604
|
+
});
|
|
605
|
+
return names;
|
|
606
|
+
}
|
|
607
|
+
function pruneUnusedNamedImports(importStmts, bodyStmts) {
|
|
608
|
+
const refs = collectReferencedNames(bodyStmts);
|
|
609
|
+
return importStmts
|
|
610
|
+
.map((stmt) => {
|
|
611
|
+
if (!t.isImportDeclaration(stmt))
|
|
612
|
+
return stmt;
|
|
613
|
+
// Side-effect imports (no specifiers) are always kept.
|
|
614
|
+
if (stmt.specifiers.length === 0)
|
|
615
|
+
return stmt;
|
|
616
|
+
const prunedSpecifiers = stmt.specifiers.filter((spec) => {
|
|
617
|
+
if (t.isImportNamespaceSpecifier(spec))
|
|
618
|
+
return true;
|
|
619
|
+
if (t.isImportDefaultSpecifier(spec))
|
|
620
|
+
return true;
|
|
621
|
+
if (t.isImportSpecifier(spec)) {
|
|
622
|
+
const localName = t.isIdentifier(spec.local) ? spec.local.name : null;
|
|
623
|
+
return localName ? refs.has(localName) : true;
|
|
624
|
+
}
|
|
625
|
+
return true;
|
|
626
|
+
});
|
|
627
|
+
if (prunedSpecifiers.length === 0)
|
|
628
|
+
return null;
|
|
629
|
+
if (prunedSpecifiers.length === stmt.specifiers.length)
|
|
630
|
+
return stmt;
|
|
631
|
+
return t.importDeclaration(prunedSpecifiers, stmt.source);
|
|
632
|
+
})
|
|
633
|
+
.filter(Boolean);
|
|
634
|
+
}
|
|
635
|
+
// ---------------------------------------------------------------------------
|
|
636
|
+
// Core transform: turbopack module format
|
|
637
|
+
// ---------------------------------------------------------------------------
|
|
5
638
|
/**
|
|
6
|
-
* Transforms a single Turbopack module arrow function into ECMAScript
|
|
639
|
+
* Transforms a single Turbopack (or webpack-style) module arrow function into ECMAScript statements.
|
|
7
640
|
*
|
|
8
|
-
* Passes
|
|
9
|
-
* 1.
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
641
|
+
* Passes:
|
|
642
|
+
* 1. Collect exports:
|
|
643
|
+
* - ODP(exportsParam, "name", {get:fn}) — direct named export
|
|
644
|
+
* - for-in loop over an object of getters → named exports
|
|
645
|
+
* - IIFE batch: !(fn)(exportsParam, {name:getter,...}) → named exports
|
|
646
|
+
* - runtimeParam.s(["name", 0, fn]) → default/named export (1-param turbopack)
|
|
647
|
+
* - requireParam.d(exportsParam, {...}) → named exports (webpack-style only)
|
|
648
|
+
* Also drops: __esModule markers, require.r(exports), "use strict", interop boilerplate.
|
|
649
|
+
* 2. Hoist: `var x = runtimeParam.r(N)` / `var x = runtimeParam.i(N)` → `import * as x`.
|
|
650
|
+
* Side-effect runtimeParam.r(N) in sequence expressions → `import "./N.js"`.
|
|
651
|
+
* 3. Replace remaining inline runtimeParam.r(N) / runtimeParam.i(N) calls with identifiers.
|
|
652
|
+
* E. Collapse Babel slicedToArray expansions.
|
|
653
|
+
* F. Recover JSX from jsx()/jsxs() calls (runs on both body and export statements).
|
|
654
|
+
* G. Remove Babel helpers.
|
|
655
|
+
* H. Prune unused named imports.
|
|
656
|
+
* 4. Assemble: prepend imports, keep filtered body, append export declarations.
|
|
13
657
|
*/
|
|
14
658
|
export const transformModule = (mod) => {
|
|
15
|
-
const { fnPath, runtimeParam, moduleParam, exportsParam } = mod;
|
|
659
|
+
const { fnPath, runtimeParam, moduleParam, exportsParam, requireParam } = mod;
|
|
16
660
|
const body = fnPath.node.body;
|
|
17
661
|
if (!t.isBlockStatement(body))
|
|
18
662
|
return [];
|
|
19
|
-
//
|
|
20
|
-
|
|
21
|
-
//
|
|
663
|
+
// webpack-style: no turbopack runtime, direct requireParam(N) calls
|
|
664
|
+
const isWebpackStyle = !runtimeParam && !!requireParam;
|
|
665
|
+
// ── Pre-scan: find for-in export loops ────────────────────────────────
|
|
22
666
|
const exportMapVarNames = new Set();
|
|
23
|
-
const exportMapDeclNodes = new Map();
|
|
667
|
+
const exportMapDeclNodes = new Map();
|
|
24
668
|
for (const stmt of body.body) {
|
|
25
669
|
const mapVarName = tryExtractForInExportLoop(stmt, exportsParam);
|
|
26
|
-
if (mapVarName !== null)
|
|
670
|
+
if (mapVarName !== null)
|
|
27
671
|
exportMapVarNames.add(mapVarName);
|
|
28
|
-
}
|
|
29
672
|
}
|
|
30
673
|
for (const stmt of body.body) {
|
|
31
674
|
if (!t.isVariableDeclaration(stmt))
|
|
@@ -39,61 +682,184 @@ export const transformModule = (mod) => {
|
|
|
39
682
|
}
|
|
40
683
|
}
|
|
41
684
|
}
|
|
42
|
-
// ── Pass 1: collect exports
|
|
43
|
-
const exportMap = new Map();
|
|
685
|
+
// ── Pass 1: collect exports + mark for removal ────────────────────────
|
|
686
|
+
const exportMap = new Map();
|
|
44
687
|
const stmtsToRemove = new WeakSet();
|
|
688
|
+
// Collect side-effect requires from sequence expressions (e.g. (ODP, r(N1), r(N2)))
|
|
689
|
+
const sideEffectRequireIds = new Set();
|
|
690
|
+
// Webpack CJS re-exports: e.exports = r(N) → export * from './N.js'
|
|
691
|
+
const reExportIds = new Set();
|
|
45
692
|
for (const stmt of body.body) {
|
|
46
|
-
//
|
|
47
|
-
if (
|
|
693
|
+
// Drop "use strict" directives (they become expressions in lax parse mode)
|
|
694
|
+
if (t.isExpressionStatement(stmt) &&
|
|
695
|
+
t.isStringLiteral(stmt.expression) &&
|
|
696
|
+
stmt.expression.value === "use strict") {
|
|
48
697
|
stmtsToRemove.add(stmt);
|
|
49
698
|
continue;
|
|
50
699
|
}
|
|
700
|
+
// moduleParam.exports = X handling
|
|
701
|
+
if (moduleParam) {
|
|
702
|
+
// Step 1: handle simple top-level e.exports = VALUE assignments
|
|
703
|
+
const cjsRhs = tryExtractModuleExportsRhs(stmt, moduleParam);
|
|
704
|
+
if (cjsRhs !== null) {
|
|
705
|
+
stmtsToRemove.add(stmt);
|
|
706
|
+
// Pure interop: e.exports = exportsParam.default → strip silently
|
|
707
|
+
if (exportsParam &&
|
|
708
|
+
t.isMemberExpression(cjsRhs) &&
|
|
709
|
+
!cjsRhs.computed &&
|
|
710
|
+
t.isIdentifier(cjsRhs.object, { name: exportsParam }) &&
|
|
711
|
+
t.isIdentifier(cjsRhs.property, { name: "default" })) {
|
|
712
|
+
continue;
|
|
713
|
+
}
|
|
714
|
+
// Not turbopack (no runtimeParam): this is an actual CJS export
|
|
715
|
+
if (!runtimeParam) {
|
|
716
|
+
// e.exports = requireParam(N) → re-export all from N
|
|
717
|
+
if (requireParam) {
|
|
718
|
+
const reExportId = tryExtractWebpackRequire(cjsRhs, requireParam);
|
|
719
|
+
if (reExportId !== null) {
|
|
720
|
+
reExportIds.add(reExportId);
|
|
721
|
+
continue;
|
|
722
|
+
}
|
|
723
|
+
}
|
|
724
|
+
// e.exports = VALUE → export default VALUE
|
|
725
|
+
exportMap.set("default", cjsRhs);
|
|
726
|
+
}
|
|
727
|
+
continue;
|
|
728
|
+
}
|
|
729
|
+
// Step 2: strip complex nested interop patterns (e.g. conditional with module.exports=exports.default inside).
|
|
730
|
+
// Only applies when exportsParam is known (2-/3-param modules) — 1-param modules have no
|
|
731
|
+
// exports param so any e.exports=X might be a real CJS export, not interop.
|
|
732
|
+
// Also skip SequenceExpression statements — their sub-expressions are handled element-by-element below.
|
|
733
|
+
if (exportsParam &&
|
|
734
|
+
(!t.isExpressionStatement(stmt) || !t.isSequenceExpression(stmt.expression))) {
|
|
735
|
+
if (isInteropBoilerplate(stmt, moduleParam, exportsParam)) {
|
|
736
|
+
stmtsToRemove.add(stmt);
|
|
737
|
+
continue;
|
|
738
|
+
}
|
|
739
|
+
}
|
|
740
|
+
}
|
|
51
741
|
if (t.isExpressionStatement(stmt)) {
|
|
52
742
|
const expr = stmt.expression;
|
|
53
|
-
// `Object.defineProperty(
|
|
743
|
+
// `Object.defineProperty(exportsParam, "__esModule", …)` — interop marker
|
|
54
744
|
if (isEsModuleMarker(expr, exportsParam)) {
|
|
55
745
|
stmtsToRemove.add(stmt);
|
|
56
746
|
continue;
|
|
57
747
|
}
|
|
58
|
-
// Direct `Object.defineProperty(
|
|
748
|
+
// Direct `Object.defineProperty(exportsParam, "name", { get: fn })`
|
|
59
749
|
const direct = tryExtractDefinePropertyExport(expr, exportsParam);
|
|
60
750
|
if (direct) {
|
|
61
751
|
exportMap.set(direct.exportName, direct.returnExpr);
|
|
62
752
|
stmtsToRemove.add(stmt);
|
|
63
753
|
continue;
|
|
64
754
|
}
|
|
65
|
-
//
|
|
755
|
+
// Turbopack IIFE batch export: !(fn)(exportsParam, {name:getter,...})
|
|
756
|
+
const batchExports = tryExtractBatchIIFEExports(expr, exportsParam);
|
|
757
|
+
if (batchExports) {
|
|
758
|
+
for (const [name, retExpr] of batchExports)
|
|
759
|
+
exportMap.set(name, retExpr);
|
|
760
|
+
stmtsToRemove.add(stmt);
|
|
761
|
+
continue;
|
|
762
|
+
}
|
|
763
|
+
// Turbopack 1-param: runtimeParam.s(["name", 0, fn]) → export
|
|
764
|
+
if (runtimeParam) {
|
|
765
|
+
const sExport = tryExtractRuntimeSExport(expr, runtimeParam);
|
|
766
|
+
if (sExport) {
|
|
767
|
+
exportMap.set(sExport.exportName, sExport.exportFn);
|
|
768
|
+
stmtsToRemove.add(stmt);
|
|
769
|
+
continue;
|
|
770
|
+
}
|
|
771
|
+
}
|
|
772
|
+
// webpack requireParam.r(exportsParam) → drop (Pass 1.5)
|
|
773
|
+
if (requireParam && isRequireDotR(expr, requireParam, exportsParam)) {
|
|
774
|
+
stmtsToRemove.add(stmt);
|
|
775
|
+
continue;
|
|
776
|
+
}
|
|
777
|
+
// webpack requireParam.d(exportsParam, {...}) → named exports (Pass 1.5)
|
|
778
|
+
if (requireParam) {
|
|
779
|
+
const dotDExports = tryExtractRequireDotD(expr, requireParam, exportsParam);
|
|
780
|
+
if (dotDExports) {
|
|
781
|
+
for (const [name, retExpr] of dotDExports)
|
|
782
|
+
exportMap.set(name, retExpr);
|
|
783
|
+
stmtsToRemove.add(stmt);
|
|
784
|
+
continue;
|
|
785
|
+
}
|
|
786
|
+
}
|
|
787
|
+
// Webpack standalone side-effect require: r(N); as a lone expression statement
|
|
788
|
+
if (isWebpackStyle && requireParam) {
|
|
789
|
+
const sideEffectId = tryExtractWebpackRequire(expr, requireParam);
|
|
790
|
+
if (sideEffectId !== null) {
|
|
791
|
+
sideEffectRequireIds.add(sideEffectId);
|
|
792
|
+
stmtsToRemove.add(stmt);
|
|
793
|
+
continue;
|
|
794
|
+
}
|
|
795
|
+
}
|
|
796
|
+
// SequenceExpression: handle (ODP, !(fn)(...), r(N1), r(N2), ...) mixed sequences
|
|
66
797
|
if (t.isSequenceExpression(expr)) {
|
|
67
798
|
const kept = [];
|
|
68
799
|
for (const sub of expr.expressions) {
|
|
69
800
|
if (isEsModuleMarker(sub, exportsParam))
|
|
70
801
|
continue;
|
|
71
|
-
const
|
|
72
|
-
if (
|
|
73
|
-
exportMap.set(
|
|
802
|
+
const directSub = tryExtractDefinePropertyExport(sub, exportsParam);
|
|
803
|
+
if (directSub) {
|
|
804
|
+
exportMap.set(directSub.exportName, directSub.returnExpr);
|
|
805
|
+
continue;
|
|
74
806
|
}
|
|
75
|
-
|
|
76
|
-
|
|
807
|
+
const batchSub = tryExtractBatchIIFEExports(sub, exportsParam);
|
|
808
|
+
if (batchSub) {
|
|
809
|
+
for (const [name, retExpr] of batchSub)
|
|
810
|
+
exportMap.set(name, retExpr);
|
|
811
|
+
continue;
|
|
812
|
+
}
|
|
813
|
+
if (requireParam && isRequireDotR(sub, requireParam, exportsParam))
|
|
814
|
+
continue;
|
|
815
|
+
if (requireParam) {
|
|
816
|
+
const dotDSub = tryExtractRequireDotD(sub, requireParam, exportsParam);
|
|
817
|
+
if (dotDSub) {
|
|
818
|
+
for (const [name, retExpr] of dotDSub)
|
|
819
|
+
exportMap.set(name, retExpr);
|
|
820
|
+
continue;
|
|
821
|
+
}
|
|
77
822
|
}
|
|
823
|
+
// Side-effect require: runtime.r(N) / runtime.i(N) standalone in a sequence
|
|
824
|
+
if (runtimeParam) {
|
|
825
|
+
const sideEffectId = tryExtractTurbopackRequire(sub, runtimeParam);
|
|
826
|
+
if (sideEffectId !== null) {
|
|
827
|
+
sideEffectRequireIds.add(sideEffectId);
|
|
828
|
+
continue;
|
|
829
|
+
}
|
|
830
|
+
}
|
|
831
|
+
// Webpack side-effect require: requireParam(N) standalone in a sequence
|
|
832
|
+
if (isWebpackStyle && requireParam) {
|
|
833
|
+
const sideEffectId = tryExtractWebpackRequire(sub, requireParam);
|
|
834
|
+
if (sideEffectId !== null) {
|
|
835
|
+
sideEffectRequireIds.add(sideEffectId);
|
|
836
|
+
continue;
|
|
837
|
+
}
|
|
838
|
+
}
|
|
839
|
+
// Strip complex interop boilerplate sub-expressions in a sequence
|
|
840
|
+
// (e.g. `("function"==typeof t.default||...)&&e.exports=t.default`)
|
|
841
|
+
if (moduleParam &&
|
|
842
|
+
exportsParam &&
|
|
843
|
+
isInteropBoilerplate(t.expressionStatement(sub), moduleParam, exportsParam))
|
|
844
|
+
continue;
|
|
845
|
+
kept.push(sub);
|
|
78
846
|
}
|
|
79
847
|
if (kept.length === 0) {
|
|
80
848
|
stmtsToRemove.add(stmt);
|
|
81
849
|
}
|
|
82
850
|
else if (kept.length < expr.expressions.length) {
|
|
83
|
-
// Trim the sequence in-place.
|
|
84
851
|
stmt.expression =
|
|
85
852
|
kept.length === 1 ? kept[0] : t.sequenceExpression(kept);
|
|
86
853
|
}
|
|
87
854
|
continue;
|
|
88
855
|
}
|
|
89
856
|
}
|
|
90
|
-
// ForInStatement: `for (var k in mapVar)
|
|
857
|
+
// ForInStatement: `for (var k in mapVar) ODP(exportsParam, k, ...)` (rare direct form)
|
|
91
858
|
const mapVarName = tryExtractForInExportLoop(stmt, exportsParam);
|
|
92
859
|
if (mapVarName !== null) {
|
|
93
860
|
stmtsToRemove.add(stmt);
|
|
94
861
|
const declNode = exportMapDeclNodes.get(mapVarName);
|
|
95
862
|
if (declNode) {
|
|
96
|
-
// Extract exports from the map object.
|
|
97
863
|
for (const decl of declNode.declarations) {
|
|
98
864
|
if (t.isIdentifier(decl.id, { name: mapVarName }) && decl.init && t.isObjectExpression(decl.init)) {
|
|
99
865
|
const extracted = extractExportsFromMap(decl.init);
|
|
@@ -102,25 +868,24 @@ export const transformModule = (mod) => {
|
|
|
102
868
|
break;
|
|
103
869
|
}
|
|
104
870
|
}
|
|
105
|
-
// Mark the map variable declaration for removal.
|
|
106
871
|
if (declNode.declarations.length === 1) {
|
|
107
872
|
stmtsToRemove.add(declNode);
|
|
108
873
|
}
|
|
109
874
|
else {
|
|
110
|
-
// Multiple declarators: remove only the export-map one.
|
|
111
875
|
declNode.declarations = declNode.declarations.filter((d) => !t.isIdentifier(d.id, { name: mapVarName }));
|
|
112
876
|
}
|
|
113
877
|
}
|
|
114
878
|
}
|
|
115
879
|
}
|
|
116
|
-
// ── Pass 2:
|
|
880
|
+
// ── Pass 2: filter + require hoisting ────────────────────────────────
|
|
117
881
|
const hoistedImports = new Map(); // specifier → importName
|
|
118
882
|
const importNameByNumId = new Map(); // numId → importName
|
|
119
883
|
const filteredBody = [];
|
|
120
884
|
for (const stmt of body.body) {
|
|
121
885
|
if (stmtsToRemove.has(stmt))
|
|
122
886
|
continue;
|
|
123
|
-
|
|
887
|
+
// Turbopack-style: `var x = runtime.r(N)` or `var x = runtime.i(N)` → static import
|
|
888
|
+
if (runtimeParam && t.isVariableDeclaration(stmt)) {
|
|
124
889
|
const keptDeclarators = [];
|
|
125
890
|
for (const decl of stmt.declarations) {
|
|
126
891
|
if (!t.isIdentifier(decl.id) || !decl.init) {
|
|
@@ -137,7 +902,32 @@ export const transformModule = (mod) => {
|
|
|
137
902
|
hoistedImports.set(spec, decl.id.name);
|
|
138
903
|
importNameByNumId.set(numId, decl.id.name);
|
|
139
904
|
}
|
|
140
|
-
// Declarator removed — becomes a static import
|
|
905
|
+
// Declarator removed — becomes a static import
|
|
906
|
+
}
|
|
907
|
+
if (keptDeclarators.length > 0) {
|
|
908
|
+
filteredBody.push(t.variableDeclaration(stmt.kind, keptDeclarators));
|
|
909
|
+
}
|
|
910
|
+
continue;
|
|
911
|
+
}
|
|
912
|
+
// Webpack-style: `var x = requireParam(N)` → static import
|
|
913
|
+
if (isWebpackStyle && requireParam && t.isVariableDeclaration(stmt)) {
|
|
914
|
+
const keptDeclarators = [];
|
|
915
|
+
for (const decl of stmt.declarations) {
|
|
916
|
+
if (!t.isIdentifier(decl.id) || !decl.init) {
|
|
917
|
+
keptDeclarators.push(decl);
|
|
918
|
+
continue;
|
|
919
|
+
}
|
|
920
|
+
const numId = tryExtractWebpackRequire(decl.init, requireParam);
|
|
921
|
+
if (numId === null) {
|
|
922
|
+
keptDeclarators.push(decl);
|
|
923
|
+
continue;
|
|
924
|
+
}
|
|
925
|
+
const spec = `./${numId}.js`;
|
|
926
|
+
if (!hoistedImports.has(spec)) {
|
|
927
|
+
hoistedImports.set(spec, decl.id.name);
|
|
928
|
+
importNameByNumId.set(numId, decl.id.name);
|
|
929
|
+
}
|
|
930
|
+
// Declarator removed — becomes a static import
|
|
141
931
|
}
|
|
142
932
|
if (keptDeclarators.length > 0) {
|
|
143
933
|
filteredBody.push(t.variableDeclaration(stmt.kind, keptDeclarators));
|
|
@@ -146,32 +936,92 @@ export const transformModule = (mod) => {
|
|
|
146
936
|
}
|
|
147
937
|
filteredBody.push(stmt);
|
|
148
938
|
}
|
|
149
|
-
// ── Pass 3: replace remaining inline
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
name =
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
939
|
+
// ── Pass 3: replace remaining inline require calls ────────────────────
|
|
940
|
+
if (runtimeParam) {
|
|
941
|
+
// Turbopack-style: replace runtime.r(N) / runtime.i(N)
|
|
942
|
+
body.body = filteredBody;
|
|
943
|
+
fnPath.traverse({
|
|
944
|
+
CallExpression(p) {
|
|
945
|
+
const numId = tryExtractTurbopackRequire(p.node, runtimeParam);
|
|
946
|
+
if (numId === null)
|
|
947
|
+
return;
|
|
948
|
+
let name = importNameByNumId.get(numId);
|
|
949
|
+
if (!name) {
|
|
950
|
+
name = `_jsr_module_${numId}`;
|
|
951
|
+
hoistedImports.set(`./${numId}.js`, name);
|
|
952
|
+
importNameByNumId.set(numId, name);
|
|
953
|
+
}
|
|
954
|
+
p.replaceWith(t.identifier(name));
|
|
955
|
+
p.skip();
|
|
956
|
+
},
|
|
957
|
+
});
|
|
958
|
+
}
|
|
959
|
+
else if (isWebpackStyle && requireParam) {
|
|
960
|
+
// Webpack-style: replace requireParam(N) direct calls
|
|
961
|
+
body.body = filteredBody;
|
|
962
|
+
fnPath.traverse({
|
|
963
|
+
CallExpression(p) {
|
|
964
|
+
const numId = tryExtractWebpackRequire(p.node, requireParam);
|
|
965
|
+
if (numId === null)
|
|
966
|
+
return;
|
|
967
|
+
let name = importNameByNumId.get(numId);
|
|
968
|
+
if (!name) {
|
|
969
|
+
name = `_jsr_module_${numId}`;
|
|
970
|
+
hoistedImports.set(`./${numId}.js`, name);
|
|
971
|
+
importNameByNumId.set(numId, name);
|
|
972
|
+
}
|
|
973
|
+
p.replaceWith(t.identifier(name));
|
|
974
|
+
p.skip();
|
|
975
|
+
},
|
|
976
|
+
});
|
|
977
|
+
}
|
|
978
|
+
else {
|
|
979
|
+
body.body = filteredBody;
|
|
980
|
+
}
|
|
981
|
+
// ── Build import/export statements ────────────────────────────────────
|
|
167
982
|
const importStmts = [];
|
|
983
|
+
// Side-effect imports (r(N) inside a sequence, no binding)
|
|
984
|
+
for (const numId of sideEffectRequireIds) {
|
|
985
|
+
const spec = `./${numId}.js`;
|
|
986
|
+
if (!hoistedImports.has(spec)) {
|
|
987
|
+
importStmts.push(t.importDeclaration([], t.stringLiteral(spec)));
|
|
988
|
+
}
|
|
989
|
+
}
|
|
990
|
+
// Namespace imports
|
|
168
991
|
for (const [spec, name] of hoistedImports) {
|
|
169
992
|
importStmts.push(t.importDeclaration([t.importNamespaceSpecifier(t.identifier(name))], t.stringLiteral(spec)));
|
|
170
993
|
}
|
|
171
|
-
|
|
994
|
+
let exportStmts = [];
|
|
995
|
+
// webpack CJS re-exports: e.exports = r(N) → export * from './N.js'
|
|
996
|
+
for (const numId of reExportIds) {
|
|
997
|
+
const spec = `./${numId}.js`;
|
|
998
|
+
exportStmts.push(t.exportAllDeclaration(t.stringLiteral(spec)));
|
|
999
|
+
}
|
|
172
1000
|
for (const [exportName, returnExpr] of exportMap) {
|
|
173
1001
|
exportStmts.push(makeExportStatement(exportName, returnExpr));
|
|
174
1002
|
}
|
|
175
|
-
|
|
1003
|
+
// ── Passes E/F/G/H — cleanup ──────────────────────────────────────────
|
|
1004
|
+
let bodyStmts = [...body.body];
|
|
1005
|
+
bodyStmts = collapseSlicedToArrayDeep(bodyStmts);
|
|
1006
|
+
recoverJSX(bodyStmts);
|
|
1007
|
+
bodyStmts = removeBabelHelpers(bodyStmts);
|
|
1008
|
+
// Also run JSX recovery on export statements — the exported function bodies
|
|
1009
|
+
// (e.g. `export default function() {...}` from e.s([]) pattern) contain JSX.
|
|
1010
|
+
recoverJSX(exportStmts);
|
|
1011
|
+
exportStmts = collapseSlicedToArrayDeep(exportStmts);
|
|
1012
|
+
const finalImports = pruneUnusedNamedImports(importStmts, [...bodyStmts, ...exportStmts]);
|
|
1013
|
+
return [...finalImports, ...bodyStmts, ...exportStmts];
|
|
1014
|
+
};
|
|
1015
|
+
// ---------------------------------------------------------------------------
|
|
1016
|
+
// Webpack-style module transform (for NNN:(e,t,r)=>{...} chunks in Next.js)
|
|
1017
|
+
// ---------------------------------------------------------------------------
|
|
1018
|
+
/**
|
|
1019
|
+
* Transforms a webpack-style module (NNN:(module,exports,require)=>{...}) into
|
|
1020
|
+
* ECMAScript module statements. Uses the same passes as the turbopack transform
|
|
1021
|
+
* since the param order and patterns are identical — only the export registration
|
|
1022
|
+
* form differs (require.d vs Object.defineProperty, already handled in Pass 1).
|
|
1023
|
+
*/
|
|
1024
|
+
export const transformWebpackModule = (mod) => {
|
|
1025
|
+
return transformModule(mod);
|
|
176
1026
|
};
|
|
177
1027
|
//# sourceMappingURL=transform.js.map
|