@jsenv/core 34.0.0 → 34.0.1
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/dist/jsenv.js +69 -25
- package/package.json +1 -1
- package/src/plugins/supervisor/js_supervisor_injection.js +3 -1
- package/src/plugins/transpilation/as_js_classic/convert_js_module_to_js_classic.js +67 -30
- package/dist/js/babel_plugin_transform_modules_systemjs.cjs +0 -392
- package/src/plugins/transpilation/as_js_classic/babel_plugin_transform_modules_systemjs.cjs +0 -608
package/dist/jsenv.js
CHANGED
|
@@ -14753,21 +14753,7 @@ function default_1({
|
|
|
14753
14753
|
};
|
|
14754
14754
|
}
|
|
14755
14755
|
|
|
14756
|
-
/*
|
|
14757
|
-
* When systemjs format is used by babel, it will generated UID based on
|
|
14758
|
-
* the import specifier:
|
|
14759
|
-
* https://github.com/babel/babel/blob/97d1967826077f15e766778c0d64711399e9a72a/packages/babel-plugin-transform-modules-systemjs/src/index.ts#L498
|
|
14760
|
-
* But at this stage import specifier are absolute file urls
|
|
14761
|
-
* So without minification these specifier are long and dependent
|
|
14762
|
-
* on where the files are on the filesystem.
|
|
14763
|
-
* This can be mitigated by minification that will rename them.
|
|
14764
|
-
* But to fix this issue once and for all I have copy-pasted
|
|
14765
|
-
* "@babel/plugin-transform-modules-systemjs" to introduce
|
|
14766
|
-
* "generateIdentifierHint" options and prevent that from hapenning
|
|
14767
|
-
*/
|
|
14768
|
-
const TRANSFORM_MODULES_SYSTEMJS_PATH = fileURLToPath(new URL("./js/babel_plugin_transform_modules_systemjs.cjs", import.meta.url));
|
|
14769
14756
|
const convertJsModuleToJsClassic = async ({
|
|
14770
|
-
rootDirectoryUrl,
|
|
14771
14757
|
systemJsInjection,
|
|
14772
14758
|
systemJsClientFileUrl,
|
|
14773
14759
|
urlInfo,
|
|
@@ -14792,15 +14778,8 @@ const convertJsModuleToJsClassic = async ({
|
|
|
14792
14778
|
babelPlugins: [...(jsClassicFormat === "system" ? [
|
|
14793
14779
|
// proposal-dynamic-import required with systemjs for babel8:
|
|
14794
14780
|
// https://github.com/babel/babel/issues/10746
|
|
14795
|
-
requireFromJsenv("@babel/plugin-proposal-dynamic-import"), [
|
|
14796
|
-
|
|
14797
|
-
requireFromJsenv(TRANSFORM_MODULES_SYSTEMJS_PATH), {
|
|
14798
|
-
generateIdentifierHint: key => {
|
|
14799
|
-
if (key.startsWith("file://")) {
|
|
14800
|
-
return urlToRelativeUrl(key, rootDirectoryUrl);
|
|
14801
|
-
}
|
|
14802
|
-
return key;
|
|
14803
|
-
}
|
|
14781
|
+
requireFromJsenv("@babel/plugin-proposal-dynamic-import"), requireFromJsenv("@babel/plugin-transform-modules-systemjs"), [babelPluginRelativeImports, {
|
|
14782
|
+
rootUrl: jsModuleUrlInfo.url
|
|
14804
14783
|
}], [default_1, {
|
|
14805
14784
|
asyncAwait: false,
|
|
14806
14785
|
// already handled + we might not needs it at all
|
|
@@ -14809,7 +14788,9 @@ const convertJsModuleToJsClassic = async ({
|
|
|
14809
14788
|
asyncAwait: false,
|
|
14810
14789
|
// already handled + we might not needs it at all
|
|
14811
14790
|
topLevelAwait: "simple"
|
|
14812
|
-
}], babelPluginTransformImportMetaUrl, babelPluginTransformImportMetaResolve, requireFromJsenv("@babel/plugin-transform-modules-umd")
|
|
14791
|
+
}], babelPluginTransformImportMetaUrl, babelPluginTransformImportMetaResolve, requireFromJsenv("@babel/plugin-transform-modules-umd"), [babelPluginRelativeImports, {
|
|
14792
|
+
rootUrl: jsModuleUrlInfo.url
|
|
14793
|
+
}]])],
|
|
14813
14794
|
urlInfo: jsModuleUrlInfo
|
|
14814
14795
|
});
|
|
14815
14796
|
let sourcemap = jsModuleUrlInfo.sourcemap;
|
|
@@ -14846,6 +14827,68 @@ const convertJsModuleToJsClassic = async ({
|
|
|
14846
14827
|
};
|
|
14847
14828
|
};
|
|
14848
14829
|
|
|
14830
|
+
/*
|
|
14831
|
+
* When systemjs or umd format is used by babel, it will generated UID based on
|
|
14832
|
+
* the import specifier:
|
|
14833
|
+
* https://github.com/babel/babel/blob/97d1967826077f15e766778c0d64711399e9a72a/packages/babel-plugin-transform-modules-systemjs/src/index.ts#L498
|
|
14834
|
+
* But at this stage import specifier are absolute file urls
|
|
14835
|
+
* This can be mitigated by minification that will rename them.
|
|
14836
|
+
* But to fix this issue once and for all there is babelPluginRelativeImports below
|
|
14837
|
+
*/
|
|
14838
|
+
const babelPluginRelativeImports = babel => {
|
|
14839
|
+
const t = babel.types;
|
|
14840
|
+
const replaceSpecifierAtPath = (path, state) => {
|
|
14841
|
+
const specifier = path.node.value;
|
|
14842
|
+
if (specifier.startsWith("file://")) {
|
|
14843
|
+
const specifierRelative = urlToRelativeUrl(specifier, state.opts.rootUrl);
|
|
14844
|
+
path.replaceWith(t.stringLiteral(specifierRelative));
|
|
14845
|
+
}
|
|
14846
|
+
};
|
|
14847
|
+
return {
|
|
14848
|
+
name: "relative-imports",
|
|
14849
|
+
visitor: {
|
|
14850
|
+
CallExpression: (path, state) => {
|
|
14851
|
+
if (path.node.callee.type !== "Import") {
|
|
14852
|
+
// Some other function call, not import();
|
|
14853
|
+
return;
|
|
14854
|
+
}
|
|
14855
|
+
if (path.node.arguments[0].type !== "StringLiteral") {
|
|
14856
|
+
// Non-string argument, probably a variable or expression, e.g.
|
|
14857
|
+
// import(moduleId)
|
|
14858
|
+
// import('./' + moduleName)
|
|
14859
|
+
return;
|
|
14860
|
+
}
|
|
14861
|
+
const sourcePath = path.get("arguments")[0];
|
|
14862
|
+
if (sourcePath.node.type === "StringLiteral") {
|
|
14863
|
+
replaceSpecifierAtPath(sourcePath, state);
|
|
14864
|
+
}
|
|
14865
|
+
},
|
|
14866
|
+
ImportDeclaration: (path, state) => {
|
|
14867
|
+
const sourcePath = path.get("source");
|
|
14868
|
+
replaceSpecifierAtPath(sourcePath, state);
|
|
14869
|
+
},
|
|
14870
|
+
ExportAllDeclaration: (path, state) => {
|
|
14871
|
+
const sourcePath = path.get("source");
|
|
14872
|
+
replaceSpecifierAtPath(sourcePath, state);
|
|
14873
|
+
},
|
|
14874
|
+
ExportNamedDeclaration: (path, state) => {
|
|
14875
|
+
if (!path.node.source) {
|
|
14876
|
+
// This export has no "source", so it's probably
|
|
14877
|
+
// a local variable or function, e.g.
|
|
14878
|
+
// export { varName }
|
|
14879
|
+
// export const constName = ...
|
|
14880
|
+
// export function funcName() {}
|
|
14881
|
+
return;
|
|
14882
|
+
}
|
|
14883
|
+
const sourcePath = path.get("source");
|
|
14884
|
+
if (sourcePath.node.type === "StringLiteral") {
|
|
14885
|
+
replaceSpecifierAtPath(sourcePath, state);
|
|
14886
|
+
}
|
|
14887
|
+
}
|
|
14888
|
+
}
|
|
14889
|
+
};
|
|
14890
|
+
};
|
|
14891
|
+
|
|
14849
14892
|
/*
|
|
14850
14893
|
* - propagate ?as_js_classic to urls
|
|
14851
14894
|
* - perform conversion from js module to js classic when url uses ?as_js_classic
|
|
@@ -17539,6 +17582,7 @@ const jsenvPluginHttpUrls = () => {
|
|
|
17539
17582
|
* and create a variable named "undefined"
|
|
17540
17583
|
*/
|
|
17541
17584
|
const injectSupervisorIntoJs = async ({
|
|
17585
|
+
webServer,
|
|
17542
17586
|
content,
|
|
17543
17587
|
url,
|
|
17544
17588
|
type,
|
|
@@ -17564,7 +17608,7 @@ const injectSupervisorIntoJs = async ({
|
|
|
17564
17608
|
specifier: sourcemapDataUrl
|
|
17565
17609
|
});
|
|
17566
17610
|
code = `${code}
|
|
17567
|
-
//# sourceURL=${url}`;
|
|
17611
|
+
//# sourceURL=${urlToRelativeUrl(url, webServer.rootDirectoryUrl)}`;
|
|
17568
17612
|
return code;
|
|
17569
17613
|
};
|
|
17570
17614
|
const babelPluginJsModuleSupervisor = babel => {
|
package/package.json
CHANGED
|
@@ -36,10 +36,12 @@
|
|
|
36
36
|
* and create a variable named "undefined"
|
|
37
37
|
*/
|
|
38
38
|
|
|
39
|
+
import { urlToRelativeUrl } from "@jsenv/urls"
|
|
39
40
|
import { applyBabelPlugins } from "@jsenv/ast"
|
|
40
41
|
import { SOURCEMAP, generateSourcemapDataUrl } from "@jsenv/sourcemap"
|
|
41
42
|
|
|
42
43
|
export const injectSupervisorIntoJs = async ({
|
|
44
|
+
webServer,
|
|
43
45
|
content,
|
|
44
46
|
url,
|
|
45
47
|
type,
|
|
@@ -66,7 +68,7 @@ export const injectSupervisorIntoJs = async ({
|
|
|
66
68
|
specifier: sourcemapDataUrl,
|
|
67
69
|
})
|
|
68
70
|
code = `${code}
|
|
69
|
-
//# sourceURL=${url}`
|
|
71
|
+
//# sourceURL=${urlToRelativeUrl(url, webServer.rootDirectoryUrl)}`
|
|
70
72
|
return code
|
|
71
73
|
}
|
|
72
74
|
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { fileURLToPath } from "node:url"
|
|
2
1
|
import { urlToRelativeUrl } from "@jsenv/urls"
|
|
3
2
|
import { readFileSync } from "@jsenv/filesystem"
|
|
4
3
|
import {
|
|
@@ -17,24 +16,7 @@ import { babelPluginTransformImportMetaResolve } from "./helpers/babel_plugin_tr
|
|
|
17
16
|
// because of https://github.com/rpetrich/babel-plugin-transform-async-to-promises/issues/84
|
|
18
17
|
import customAsyncToPromises from "./async-to-promises.js"
|
|
19
18
|
|
|
20
|
-
/*
|
|
21
|
-
* When systemjs format is used by babel, it will generated UID based on
|
|
22
|
-
* the import specifier:
|
|
23
|
-
* https://github.com/babel/babel/blob/97d1967826077f15e766778c0d64711399e9a72a/packages/babel-plugin-transform-modules-systemjs/src/index.ts#L498
|
|
24
|
-
* But at this stage import specifier are absolute file urls
|
|
25
|
-
* So without minification these specifier are long and dependent
|
|
26
|
-
* on where the files are on the filesystem.
|
|
27
|
-
* This can be mitigated by minification that will rename them.
|
|
28
|
-
* But to fix this issue once and for all I have copy-pasted
|
|
29
|
-
* "@babel/plugin-transform-modules-systemjs" to introduce
|
|
30
|
-
* "generateIdentifierHint" options and prevent that from hapenning
|
|
31
|
-
*/
|
|
32
|
-
const TRANSFORM_MODULES_SYSTEMJS_PATH = fileURLToPath(
|
|
33
|
-
new URL("./babel_plugin_transform_modules_systemjs.cjs", import.meta.url),
|
|
34
|
-
)
|
|
35
|
-
|
|
36
19
|
export const convertJsModuleToJsClassic = async ({
|
|
37
|
-
rootDirectoryUrl,
|
|
38
20
|
systemJsInjection,
|
|
39
21
|
systemJsClientFileUrl,
|
|
40
22
|
urlInfo,
|
|
@@ -60,18 +42,8 @@ export const convertJsModuleToJsClassic = async ({
|
|
|
60
42
|
// proposal-dynamic-import required with systemjs for babel8:
|
|
61
43
|
// https://github.com/babel/babel/issues/10746
|
|
62
44
|
requireFromJsenv("@babel/plugin-proposal-dynamic-import"),
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
requireFromJsenv(TRANSFORM_MODULES_SYSTEMJS_PATH),
|
|
66
|
-
{
|
|
67
|
-
generateIdentifierHint: (key) => {
|
|
68
|
-
if (key.startsWith("file://")) {
|
|
69
|
-
return urlToRelativeUrl(key, rootDirectoryUrl)
|
|
70
|
-
}
|
|
71
|
-
return key
|
|
72
|
-
},
|
|
73
|
-
},
|
|
74
|
-
],
|
|
45
|
+
requireFromJsenv("@babel/plugin-transform-modules-systemjs"),
|
|
46
|
+
[babelPluginRelativeImports, { rootUrl: jsModuleUrlInfo.url }],
|
|
75
47
|
[
|
|
76
48
|
customAsyncToPromises,
|
|
77
49
|
{
|
|
@@ -91,6 +63,7 @@ export const convertJsModuleToJsClassic = async ({
|
|
|
91
63
|
babelPluginTransformImportMetaUrl,
|
|
92
64
|
babelPluginTransformImportMetaResolve,
|
|
93
65
|
requireFromJsenv("@babel/plugin-transform-modules-umd"),
|
|
66
|
+
[babelPluginRelativeImports, { rootUrl: jsModuleUrlInfo.url }],
|
|
94
67
|
]),
|
|
95
68
|
],
|
|
96
69
|
urlInfo: jsModuleUrlInfo,
|
|
@@ -132,3 +105,67 @@ export const convertJsModuleToJsClassic = async ({
|
|
|
132
105
|
sourcemap,
|
|
133
106
|
}
|
|
134
107
|
}
|
|
108
|
+
|
|
109
|
+
/*
|
|
110
|
+
* When systemjs or umd format is used by babel, it will generated UID based on
|
|
111
|
+
* the import specifier:
|
|
112
|
+
* https://github.com/babel/babel/blob/97d1967826077f15e766778c0d64711399e9a72a/packages/babel-plugin-transform-modules-systemjs/src/index.ts#L498
|
|
113
|
+
* But at this stage import specifier are absolute file urls
|
|
114
|
+
* This can be mitigated by minification that will rename them.
|
|
115
|
+
* But to fix this issue once and for all there is babelPluginRelativeImports below
|
|
116
|
+
*/
|
|
117
|
+
const babelPluginRelativeImports = (babel) => {
|
|
118
|
+
const t = babel.types
|
|
119
|
+
|
|
120
|
+
const replaceSpecifierAtPath = (path, state) => {
|
|
121
|
+
const specifier = path.node.value
|
|
122
|
+
if (specifier.startsWith("file://")) {
|
|
123
|
+
const specifierRelative = urlToRelativeUrl(specifier, state.opts.rootUrl)
|
|
124
|
+
path.replaceWith(t.stringLiteral(specifierRelative))
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
return {
|
|
129
|
+
name: "relative-imports",
|
|
130
|
+
visitor: {
|
|
131
|
+
CallExpression: (path, state) => {
|
|
132
|
+
if (path.node.callee.type !== "Import") {
|
|
133
|
+
// Some other function call, not import();
|
|
134
|
+
return
|
|
135
|
+
}
|
|
136
|
+
if (path.node.arguments[0].type !== "StringLiteral") {
|
|
137
|
+
// Non-string argument, probably a variable or expression, e.g.
|
|
138
|
+
// import(moduleId)
|
|
139
|
+
// import('./' + moduleName)
|
|
140
|
+
return
|
|
141
|
+
}
|
|
142
|
+
const sourcePath = path.get("arguments")[0]
|
|
143
|
+
if (sourcePath.node.type === "StringLiteral") {
|
|
144
|
+
replaceSpecifierAtPath(sourcePath, state)
|
|
145
|
+
}
|
|
146
|
+
},
|
|
147
|
+
ImportDeclaration: (path, state) => {
|
|
148
|
+
const sourcePath = path.get("source")
|
|
149
|
+
replaceSpecifierAtPath(sourcePath, state)
|
|
150
|
+
},
|
|
151
|
+
ExportAllDeclaration: (path, state) => {
|
|
152
|
+
const sourcePath = path.get("source")
|
|
153
|
+
replaceSpecifierAtPath(sourcePath, state)
|
|
154
|
+
},
|
|
155
|
+
ExportNamedDeclaration: (path, state) => {
|
|
156
|
+
if (!path.node.source) {
|
|
157
|
+
// This export has no "source", so it's probably
|
|
158
|
+
// a local variable or function, e.g.
|
|
159
|
+
// export { varName }
|
|
160
|
+
// export const constName = ...
|
|
161
|
+
// export function funcName() {}
|
|
162
|
+
return
|
|
163
|
+
}
|
|
164
|
+
const sourcePath = path.get("source")
|
|
165
|
+
if (sourcePath.node.type === "StringLiteral") {
|
|
166
|
+
replaceSpecifierAtPath(sourcePath, state)
|
|
167
|
+
}
|
|
168
|
+
},
|
|
169
|
+
},
|
|
170
|
+
}
|
|
171
|
+
}
|
|
@@ -1,392 +0,0 @@
|
|
|
1
|
-
Object.defineProperty(exports, "__esModule", {
|
|
2
|
-
value: true
|
|
3
|
-
});
|
|
4
|
-
exports.default = void 0;
|
|
5
|
-
exports.getExportSpecifierName = getExportSpecifierName;
|
|
6
|
-
var _helperPluginUtils = require("@babel/helper-plugin-utils");
|
|
7
|
-
var _helperHoistVariables = require("@babel/helper-hoist-variables");
|
|
8
|
-
var _core = require("@babel/core");
|
|
9
|
-
var _helperModuleTransforms = require("@babel/helper-module-transforms");
|
|
10
|
-
var _helperValidatorIdentifier = require("@babel/helper-validator-identifier");
|
|
11
|
-
const buildTemplate = _core.template.statement(`
|
|
12
|
-
SYSTEM_REGISTER(MODULE_NAME, SOURCES, function (EXPORT_IDENTIFIER, CONTEXT_IDENTIFIER) {
|
|
13
|
-
"use strict";
|
|
14
|
-
BEFORE_BODY;
|
|
15
|
-
return {
|
|
16
|
-
setters: SETTERS,
|
|
17
|
-
execute: EXECUTE,
|
|
18
|
-
};
|
|
19
|
-
});
|
|
20
|
-
`);
|
|
21
|
-
const buildExportAll = _core.template.statement(`
|
|
22
|
-
for (var KEY in TARGET) {
|
|
23
|
-
if (KEY !== "default" && KEY !== "__esModule") EXPORT_OBJ[KEY] = TARGET[KEY];
|
|
24
|
-
}
|
|
25
|
-
`);
|
|
26
|
-
const MISSING_PLUGIN_WARNING = `\
|
|
27
|
-
WARNING: Dynamic import() transformation must be enabled using the
|
|
28
|
-
@babel/plugin-proposal-dynamic-import plugin. Babel 8 will
|
|
29
|
-
no longer transform import() without using that plugin.
|
|
30
|
-
`;
|
|
31
|
-
function getExportSpecifierName(node, stringSpecifiers) {
|
|
32
|
-
if (node.type === "Identifier") {
|
|
33
|
-
return node.name;
|
|
34
|
-
} else if (node.type === "StringLiteral") {
|
|
35
|
-
const stringValue = node.value;
|
|
36
|
-
if (!(0, _helperValidatorIdentifier.isIdentifierName)(stringValue)) {
|
|
37
|
-
stringSpecifiers.add(stringValue);
|
|
38
|
-
}
|
|
39
|
-
return stringValue;
|
|
40
|
-
} else {
|
|
41
|
-
throw new Error(`Expected export specifier to be either Identifier or StringLiteral, got ${node.type}`);
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
function constructExportCall(path, exportIdent, exportNames, exportValues, exportStarTarget, stringSpecifiers) {
|
|
45
|
-
const statements = [];
|
|
46
|
-
if (!exportStarTarget) {
|
|
47
|
-
if (exportNames.length === 1) {
|
|
48
|
-
statements.push(_core.types.expressionStatement(_core.types.callExpression(exportIdent, [_core.types.stringLiteral(exportNames[0]), exportValues[0]])));
|
|
49
|
-
} else {
|
|
50
|
-
const objectProperties = [];
|
|
51
|
-
for (let i = 0; i < exportNames.length; i++) {
|
|
52
|
-
const exportName = exportNames[i];
|
|
53
|
-
const exportValue = exportValues[i];
|
|
54
|
-
objectProperties.push(_core.types.objectProperty(stringSpecifiers.has(exportName) ? _core.types.stringLiteral(exportName) : _core.types.identifier(exportName), exportValue));
|
|
55
|
-
}
|
|
56
|
-
statements.push(_core.types.expressionStatement(_core.types.callExpression(exportIdent, [_core.types.objectExpression(objectProperties)])));
|
|
57
|
-
}
|
|
58
|
-
} else {
|
|
59
|
-
const exportObj = path.scope.generateUid("exportObj");
|
|
60
|
-
statements.push(_core.types.variableDeclaration("var", [_core.types.variableDeclarator(_core.types.identifier(exportObj), _core.types.objectExpression([]))]));
|
|
61
|
-
statements.push(buildExportAll({
|
|
62
|
-
KEY: path.scope.generateUidIdentifier("key"),
|
|
63
|
-
EXPORT_OBJ: _core.types.identifier(exportObj),
|
|
64
|
-
TARGET: exportStarTarget
|
|
65
|
-
}));
|
|
66
|
-
for (let i = 0; i < exportNames.length; i++) {
|
|
67
|
-
const exportName = exportNames[i];
|
|
68
|
-
const exportValue = exportValues[i];
|
|
69
|
-
statements.push(_core.types.expressionStatement(_core.types.assignmentExpression("=", _core.types.memberExpression(_core.types.identifier(exportObj), _core.types.identifier(exportName)), exportValue)));
|
|
70
|
-
}
|
|
71
|
-
statements.push(_core.types.expressionStatement(_core.types.callExpression(exportIdent, [_core.types.identifier(exportObj)])));
|
|
72
|
-
}
|
|
73
|
-
return statements;
|
|
74
|
-
}
|
|
75
|
-
var _default = (0, _helperPluginUtils.declare)((api, options) => {
|
|
76
|
-
api.assertVersion(7);
|
|
77
|
-
const {
|
|
78
|
-
systemGlobal = "System",
|
|
79
|
-
allowTopLevelThis = false
|
|
80
|
-
} = options;
|
|
81
|
-
const reassignmentVisited = new WeakSet();
|
|
82
|
-
const reassignmentVisitor = {
|
|
83
|
-
"AssignmentExpression|UpdateExpression"(path) {
|
|
84
|
-
if (reassignmentVisited.has(path.node)) return;
|
|
85
|
-
reassignmentVisited.add(path.node);
|
|
86
|
-
const arg = path.isAssignmentExpression() ? path.get("left") : path.get("argument");
|
|
87
|
-
if (arg.isObjectPattern() || arg.isArrayPattern()) {
|
|
88
|
-
const exprs = [path.node];
|
|
89
|
-
for (const name of Object.keys(arg.getBindingIdentifiers())) {
|
|
90
|
-
if (this.scope.getBinding(name) !== path.scope.getBinding(name)) {
|
|
91
|
-
return;
|
|
92
|
-
}
|
|
93
|
-
const exportedNames = this.exports[name];
|
|
94
|
-
if (!exportedNames) continue;
|
|
95
|
-
for (const exportedName of exportedNames) {
|
|
96
|
-
exprs.push(this.buildCall(exportedName, _core.types.identifier(name)).expression);
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
path.replaceWith(_core.types.sequenceExpression(exprs));
|
|
100
|
-
return;
|
|
101
|
-
}
|
|
102
|
-
if (!arg.isIdentifier()) return;
|
|
103
|
-
const name = arg.node.name;
|
|
104
|
-
if (this.scope.getBinding(name) !== path.scope.getBinding(name)) return;
|
|
105
|
-
const exportedNames = this.exports[name];
|
|
106
|
-
if (!exportedNames) return;
|
|
107
|
-
let node = path.node;
|
|
108
|
-
const isPostUpdateExpression = _core.types.isUpdateExpression(node, {
|
|
109
|
-
prefix: false
|
|
110
|
-
});
|
|
111
|
-
if (isPostUpdateExpression) {
|
|
112
|
-
node = _core.types.binaryExpression(node.operator[0], _core.types.unaryExpression("+", _core.types.cloneNode(node.argument)), _core.types.numericLiteral(1));
|
|
113
|
-
}
|
|
114
|
-
for (const exportedName of exportedNames) {
|
|
115
|
-
node = this.buildCall(exportedName, node).expression;
|
|
116
|
-
}
|
|
117
|
-
if (isPostUpdateExpression) {
|
|
118
|
-
node = _core.types.sequenceExpression([node, path.node]);
|
|
119
|
-
}
|
|
120
|
-
path.replaceWith(node);
|
|
121
|
-
}
|
|
122
|
-
};
|
|
123
|
-
return {
|
|
124
|
-
name: "transform-modules-systemjs",
|
|
125
|
-
pre() {
|
|
126
|
-
this.file.set("@babel/plugin-transform-modules-*", "systemjs");
|
|
127
|
-
},
|
|
128
|
-
visitor: {
|
|
129
|
-
CallExpression(path, state) {
|
|
130
|
-
if (_core.types.isImport(path.node.callee)) {
|
|
131
|
-
if (!this.file.has("@babel/plugin-proposal-dynamic-import")) {
|
|
132
|
-
{
|
|
133
|
-
console.warn(MISSING_PLUGIN_WARNING);
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
path.replaceWith((0, _helperModuleTransforms.buildDynamicImport)(path.node, false, true, specifier => _core.types.callExpression(_core.types.memberExpression(_core.types.identifier(state.contextIdent), _core.types.identifier("import")), [specifier])));
|
|
137
|
-
}
|
|
138
|
-
},
|
|
139
|
-
MetaProperty(path, state) {
|
|
140
|
-
if (path.node.meta.name === "import" && path.node.property.name === "meta") {
|
|
141
|
-
path.replaceWith(_core.types.memberExpression(_core.types.identifier(state.contextIdent), _core.types.identifier("meta")));
|
|
142
|
-
}
|
|
143
|
-
},
|
|
144
|
-
ReferencedIdentifier(path, state) {
|
|
145
|
-
if (path.node.name === "__moduleName" && !path.scope.hasBinding("__moduleName")) {
|
|
146
|
-
path.replaceWith(_core.types.memberExpression(_core.types.identifier(state.contextIdent), _core.types.identifier("id")));
|
|
147
|
-
}
|
|
148
|
-
},
|
|
149
|
-
Program: {
|
|
150
|
-
enter(path, state) {
|
|
151
|
-
state.contextIdent = path.scope.generateUid("context");
|
|
152
|
-
state.stringSpecifiers = new Set();
|
|
153
|
-
if (!allowTopLevelThis) {
|
|
154
|
-
(0, _helperModuleTransforms.rewriteThis)(path);
|
|
155
|
-
}
|
|
156
|
-
},
|
|
157
|
-
exit(path, state) {
|
|
158
|
-
const scope = path.scope;
|
|
159
|
-
const exportIdent = scope.generateUid("export");
|
|
160
|
-
const {
|
|
161
|
-
contextIdent,
|
|
162
|
-
stringSpecifiers
|
|
163
|
-
} = state;
|
|
164
|
-
const exportMap = Object.create(null);
|
|
165
|
-
const modules = [];
|
|
166
|
-
const beforeBody = [];
|
|
167
|
-
const setters = [];
|
|
168
|
-
const sources = [];
|
|
169
|
-
const variableIds = [];
|
|
170
|
-
const removedPaths = [];
|
|
171
|
-
function addExportName(key, val) {
|
|
172
|
-
exportMap[key] = exportMap[key] || [];
|
|
173
|
-
exportMap[key].push(val);
|
|
174
|
-
}
|
|
175
|
-
function pushModule(source, key, specifiers) {
|
|
176
|
-
let module;
|
|
177
|
-
modules.forEach(function (m) {
|
|
178
|
-
if (m.key === source) {
|
|
179
|
-
module = m;
|
|
180
|
-
}
|
|
181
|
-
});
|
|
182
|
-
if (!module) {
|
|
183
|
-
modules.push(module = {
|
|
184
|
-
key: source,
|
|
185
|
-
imports: [],
|
|
186
|
-
exports: []
|
|
187
|
-
});
|
|
188
|
-
}
|
|
189
|
-
module[key] = module[key].concat(specifiers);
|
|
190
|
-
}
|
|
191
|
-
function buildExportCall(name, val) {
|
|
192
|
-
return _core.types.expressionStatement(_core.types.callExpression(_core.types.identifier(exportIdent), [_core.types.stringLiteral(name), val]));
|
|
193
|
-
}
|
|
194
|
-
const exportNames = [];
|
|
195
|
-
const exportValues = [];
|
|
196
|
-
const body = path.get("body");
|
|
197
|
-
for (const path of body) {
|
|
198
|
-
if (path.isFunctionDeclaration()) {
|
|
199
|
-
beforeBody.push(path.node);
|
|
200
|
-
removedPaths.push(path);
|
|
201
|
-
} else if (path.isClassDeclaration()) {
|
|
202
|
-
variableIds.push(_core.types.cloneNode(path.node.id));
|
|
203
|
-
path.replaceWith(_core.types.expressionStatement(_core.types.assignmentExpression("=", _core.types.cloneNode(path.node.id), _core.types.toExpression(path.node))));
|
|
204
|
-
} else if (path.isVariableDeclaration()) {
|
|
205
|
-
path.node.kind = "var";
|
|
206
|
-
} else if (path.isImportDeclaration()) {
|
|
207
|
-
const source = path.node.source.value;
|
|
208
|
-
pushModule(source, "imports", path.node.specifiers);
|
|
209
|
-
for (const name of Object.keys(path.getBindingIdentifiers())) {
|
|
210
|
-
scope.removeBinding(name);
|
|
211
|
-
variableIds.push(_core.types.identifier(name));
|
|
212
|
-
}
|
|
213
|
-
path.remove();
|
|
214
|
-
} else if (path.isExportAllDeclaration()) {
|
|
215
|
-
pushModule(path.node.source.value, "exports", path.node);
|
|
216
|
-
path.remove();
|
|
217
|
-
} else if (path.isExportDefaultDeclaration()) {
|
|
218
|
-
const declar = path.node.declaration;
|
|
219
|
-
if (_core.types.isClassDeclaration(declar)) {
|
|
220
|
-
const id = declar.id;
|
|
221
|
-
if (id) {
|
|
222
|
-
exportNames.push("default");
|
|
223
|
-
exportValues.push(scope.buildUndefinedNode());
|
|
224
|
-
variableIds.push(_core.types.cloneNode(id));
|
|
225
|
-
addExportName(id.name, "default");
|
|
226
|
-
path.replaceWith(_core.types.expressionStatement(_core.types.assignmentExpression("=", _core.types.cloneNode(id), _core.types.toExpression(declar))));
|
|
227
|
-
} else {
|
|
228
|
-
exportNames.push("default");
|
|
229
|
-
exportValues.push(_core.types.toExpression(declar));
|
|
230
|
-
removedPaths.push(path);
|
|
231
|
-
}
|
|
232
|
-
} else if (_core.types.isFunctionDeclaration(declar)) {
|
|
233
|
-
const id = declar.id;
|
|
234
|
-
if (id) {
|
|
235
|
-
beforeBody.push(declar);
|
|
236
|
-
exportNames.push("default");
|
|
237
|
-
exportValues.push(_core.types.cloneNode(id));
|
|
238
|
-
addExportName(id.name, "default");
|
|
239
|
-
} else {
|
|
240
|
-
exportNames.push("default");
|
|
241
|
-
exportValues.push(_core.types.toExpression(declar));
|
|
242
|
-
}
|
|
243
|
-
removedPaths.push(path);
|
|
244
|
-
} else {
|
|
245
|
-
path.replaceWith(buildExportCall("default", declar));
|
|
246
|
-
}
|
|
247
|
-
} else if (path.isExportNamedDeclaration()) {
|
|
248
|
-
const declar = path.node.declaration;
|
|
249
|
-
if (declar) {
|
|
250
|
-
path.replaceWith(declar);
|
|
251
|
-
if (_core.types.isFunction(declar)) {
|
|
252
|
-
const name = declar.id.name;
|
|
253
|
-
addExportName(name, name);
|
|
254
|
-
beforeBody.push(declar);
|
|
255
|
-
exportNames.push(name);
|
|
256
|
-
exportValues.push(_core.types.cloneNode(declar.id));
|
|
257
|
-
removedPaths.push(path);
|
|
258
|
-
} else if (_core.types.isClass(declar)) {
|
|
259
|
-
const name = declar.id.name;
|
|
260
|
-
exportNames.push(name);
|
|
261
|
-
exportValues.push(scope.buildUndefinedNode());
|
|
262
|
-
variableIds.push(_core.types.cloneNode(declar.id));
|
|
263
|
-
path.replaceWith(_core.types.expressionStatement(_core.types.assignmentExpression("=", _core.types.cloneNode(declar.id), _core.types.toExpression(declar))));
|
|
264
|
-
addExportName(name, name);
|
|
265
|
-
} else {
|
|
266
|
-
if (_core.types.isVariableDeclaration(declar)) {
|
|
267
|
-
declar.kind = "var";
|
|
268
|
-
}
|
|
269
|
-
for (const name of Object.keys(_core.types.getBindingIdentifiers(declar))) {
|
|
270
|
-
addExportName(name, name);
|
|
271
|
-
}
|
|
272
|
-
}
|
|
273
|
-
} else {
|
|
274
|
-
const specifiers = path.node.specifiers;
|
|
275
|
-
if (specifiers != null && specifiers.length) {
|
|
276
|
-
if (path.node.source) {
|
|
277
|
-
pushModule(path.node.source.value, "exports", specifiers);
|
|
278
|
-
path.remove();
|
|
279
|
-
} else {
|
|
280
|
-
const nodes = [];
|
|
281
|
-
for (const specifier of specifiers) {
|
|
282
|
-
const {
|
|
283
|
-
local,
|
|
284
|
-
exported
|
|
285
|
-
} = specifier;
|
|
286
|
-
const binding = scope.getBinding(local.name);
|
|
287
|
-
const exportedName = getExportSpecifierName(exported, stringSpecifiers);
|
|
288
|
-
if (binding && _core.types.isFunctionDeclaration(binding.path.node)) {
|
|
289
|
-
exportNames.push(exportedName);
|
|
290
|
-
exportValues.push(_core.types.cloneNode(local));
|
|
291
|
-
} else if (!binding) {
|
|
292
|
-
nodes.push(buildExportCall(exportedName, local));
|
|
293
|
-
}
|
|
294
|
-
addExportName(local.name, exportedName);
|
|
295
|
-
}
|
|
296
|
-
path.replaceWithMultiple(nodes);
|
|
297
|
-
}
|
|
298
|
-
} else {
|
|
299
|
-
path.remove();
|
|
300
|
-
}
|
|
301
|
-
}
|
|
302
|
-
}
|
|
303
|
-
}
|
|
304
|
-
modules.forEach(function (specifiers) {
|
|
305
|
-
const setterBody = [];
|
|
306
|
-
// --- START FORK CHANGES ---
|
|
307
|
-
const targetHint = state.opts.generateIdentifierHint ? state.opts.generateIdentifierHint(specifiers.key) : specifiers.key;
|
|
308
|
-
const target = scope.generateUid(targetHint);
|
|
309
|
-
// --- END FORK CHANGES ---
|
|
310
|
-
for (let specifier of specifiers.imports) {
|
|
311
|
-
if (_core.types.isImportNamespaceSpecifier(specifier)) {
|
|
312
|
-
setterBody.push(_core.types.expressionStatement(_core.types.assignmentExpression("=", specifier.local, _core.types.identifier(target))));
|
|
313
|
-
} else if (_core.types.isImportDefaultSpecifier(specifier)) {
|
|
314
|
-
specifier = _core.types.importSpecifier(specifier.local, _core.types.identifier("default"));
|
|
315
|
-
}
|
|
316
|
-
if (_core.types.isImportSpecifier(specifier)) {
|
|
317
|
-
const {
|
|
318
|
-
imported
|
|
319
|
-
} = specifier;
|
|
320
|
-
setterBody.push(_core.types.expressionStatement(_core.types.assignmentExpression("=", specifier.local, _core.types.memberExpression(_core.types.identifier(target), specifier.imported, imported.type === "StringLiteral"))));
|
|
321
|
-
}
|
|
322
|
-
}
|
|
323
|
-
if (specifiers.exports.length) {
|
|
324
|
-
const exportNames = [];
|
|
325
|
-
const exportValues = [];
|
|
326
|
-
let hasExportStar = false;
|
|
327
|
-
for (const node of specifiers.exports) {
|
|
328
|
-
if (_core.types.isExportAllDeclaration(node)) {
|
|
329
|
-
hasExportStar = true;
|
|
330
|
-
} else if (_core.types.isExportSpecifier(node)) {
|
|
331
|
-
const exportedName = getExportSpecifierName(node.exported, stringSpecifiers);
|
|
332
|
-
exportNames.push(exportedName);
|
|
333
|
-
exportValues.push(_core.types.memberExpression(_core.types.identifier(target), node.local, _core.types.isStringLiteral(node.local)));
|
|
334
|
-
} else ;
|
|
335
|
-
}
|
|
336
|
-
setterBody.push(...constructExportCall(path, _core.types.identifier(exportIdent), exportNames, exportValues, hasExportStar ? _core.types.identifier(target) : null, stringSpecifiers));
|
|
337
|
-
}
|
|
338
|
-
sources.push(_core.types.stringLiteral(specifiers.key));
|
|
339
|
-
setters.push(_core.types.functionExpression(null, [_core.types.identifier(target)], _core.types.blockStatement(setterBody)));
|
|
340
|
-
});
|
|
341
|
-
let moduleName = (0, _helperModuleTransforms.getModuleName)(this.file.opts, options);
|
|
342
|
-
if (moduleName) moduleName = _core.types.stringLiteral(moduleName);
|
|
343
|
-
(0, _helperHoistVariables.default)(path, (id, name, hasInit) => {
|
|
344
|
-
variableIds.push(id);
|
|
345
|
-
if (!hasInit && name in exportMap) {
|
|
346
|
-
for (const exported of exportMap[name]) {
|
|
347
|
-
exportNames.push(exported);
|
|
348
|
-
exportValues.push(scope.buildUndefinedNode());
|
|
349
|
-
}
|
|
350
|
-
}
|
|
351
|
-
});
|
|
352
|
-
if (variableIds.length) {
|
|
353
|
-
beforeBody.unshift(_core.types.variableDeclaration("var", variableIds.map(id => _core.types.variableDeclarator(id))));
|
|
354
|
-
}
|
|
355
|
-
if (exportNames.length) {
|
|
356
|
-
beforeBody.push(...constructExportCall(path, _core.types.identifier(exportIdent), exportNames, exportValues, null, stringSpecifiers));
|
|
357
|
-
}
|
|
358
|
-
path.traverse(reassignmentVisitor, {
|
|
359
|
-
exports: exportMap,
|
|
360
|
-
buildCall: buildExportCall,
|
|
361
|
-
scope
|
|
362
|
-
});
|
|
363
|
-
for (const path of removedPaths) {
|
|
364
|
-
path.remove();
|
|
365
|
-
}
|
|
366
|
-
let hasTLA = false;
|
|
367
|
-
path.traverse({
|
|
368
|
-
AwaitExpression(path) {
|
|
369
|
-
hasTLA = true;
|
|
370
|
-
path.stop();
|
|
371
|
-
},
|
|
372
|
-
Function(path) {
|
|
373
|
-
path.skip();
|
|
374
|
-
},
|
|
375
|
-
noScope: true
|
|
376
|
-
});
|
|
377
|
-
path.node.body = [buildTemplate({
|
|
378
|
-
SYSTEM_REGISTER: _core.types.memberExpression(_core.types.identifier(systemGlobal), _core.types.identifier("register")),
|
|
379
|
-
BEFORE_BODY: beforeBody,
|
|
380
|
-
MODULE_NAME: moduleName,
|
|
381
|
-
SETTERS: _core.types.arrayExpression(setters),
|
|
382
|
-
EXECUTE: _core.types.functionExpression(null, [], _core.types.blockStatement(path.node.body), false, hasTLA),
|
|
383
|
-
SOURCES: _core.types.arrayExpression(sources),
|
|
384
|
-
EXPORT_IDENTIFIER: _core.types.identifier(exportIdent),
|
|
385
|
-
CONTEXT_IDENTIFIER: _core.types.identifier(contextIdent)
|
|
386
|
-
})];
|
|
387
|
-
}
|
|
388
|
-
}
|
|
389
|
-
}
|
|
390
|
-
};
|
|
391
|
-
});
|
|
392
|
-
exports.default = _default;
|
|
@@ -1,608 +0,0 @@
|
|
|
1
|
-
"use strict"
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true,
|
|
5
|
-
})
|
|
6
|
-
exports.default = void 0
|
|
7
|
-
exports.getExportSpecifierName = getExportSpecifierName
|
|
8
|
-
var _helperPluginUtils = require("@babel/helper-plugin-utils")
|
|
9
|
-
var _helperHoistVariables = require("@babel/helper-hoist-variables")
|
|
10
|
-
var _core = require("@babel/core")
|
|
11
|
-
var _helperModuleTransforms = require("@babel/helper-module-transforms")
|
|
12
|
-
var _helperValidatorIdentifier = require("@babel/helper-validator-identifier")
|
|
13
|
-
const buildTemplate = _core.template.statement(`
|
|
14
|
-
SYSTEM_REGISTER(MODULE_NAME, SOURCES, function (EXPORT_IDENTIFIER, CONTEXT_IDENTIFIER) {
|
|
15
|
-
"use strict";
|
|
16
|
-
BEFORE_BODY;
|
|
17
|
-
return {
|
|
18
|
-
setters: SETTERS,
|
|
19
|
-
execute: EXECUTE,
|
|
20
|
-
};
|
|
21
|
-
});
|
|
22
|
-
`)
|
|
23
|
-
const buildExportAll = _core.template.statement(`
|
|
24
|
-
for (var KEY in TARGET) {
|
|
25
|
-
if (KEY !== "default" && KEY !== "__esModule") EXPORT_OBJ[KEY] = TARGET[KEY];
|
|
26
|
-
}
|
|
27
|
-
`)
|
|
28
|
-
const MISSING_PLUGIN_WARNING = `\
|
|
29
|
-
WARNING: Dynamic import() transformation must be enabled using the
|
|
30
|
-
@babel/plugin-proposal-dynamic-import plugin. Babel 8 will
|
|
31
|
-
no longer transform import() without using that plugin.
|
|
32
|
-
`
|
|
33
|
-
const MISSING_PLUGIN_ERROR = `\
|
|
34
|
-
ERROR: Dynamic import() transformation must be enabled using the
|
|
35
|
-
@babel/plugin-proposal-dynamic-import plugin. Babel 8
|
|
36
|
-
no longer transforms import() without using that plugin.
|
|
37
|
-
`
|
|
38
|
-
|
|
39
|
-
function getExportSpecifierName(node, stringSpecifiers) {
|
|
40
|
-
if (node.type === "Identifier") {
|
|
41
|
-
return node.name
|
|
42
|
-
} else if (node.type === "StringLiteral") {
|
|
43
|
-
const stringValue = node.value
|
|
44
|
-
if (!(0, _helperValidatorIdentifier.isIdentifierName)(stringValue)) {
|
|
45
|
-
stringSpecifiers.add(stringValue)
|
|
46
|
-
}
|
|
47
|
-
return stringValue
|
|
48
|
-
} else {
|
|
49
|
-
throw new Error(
|
|
50
|
-
`Expected export specifier to be either Identifier or StringLiteral, got ${node.type}`,
|
|
51
|
-
)
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
function constructExportCall(
|
|
55
|
-
path,
|
|
56
|
-
exportIdent,
|
|
57
|
-
exportNames,
|
|
58
|
-
exportValues,
|
|
59
|
-
exportStarTarget,
|
|
60
|
-
stringSpecifiers,
|
|
61
|
-
) {
|
|
62
|
-
const statements = []
|
|
63
|
-
if (!exportStarTarget) {
|
|
64
|
-
if (exportNames.length === 1) {
|
|
65
|
-
statements.push(
|
|
66
|
-
_core.types.expressionStatement(
|
|
67
|
-
_core.types.callExpression(exportIdent, [
|
|
68
|
-
_core.types.stringLiteral(exportNames[0]),
|
|
69
|
-
exportValues[0],
|
|
70
|
-
]),
|
|
71
|
-
),
|
|
72
|
-
)
|
|
73
|
-
} else {
|
|
74
|
-
const objectProperties = []
|
|
75
|
-
for (let i = 0; i < exportNames.length; i++) {
|
|
76
|
-
const exportName = exportNames[i]
|
|
77
|
-
const exportValue = exportValues[i]
|
|
78
|
-
objectProperties.push(
|
|
79
|
-
_core.types.objectProperty(
|
|
80
|
-
stringSpecifiers.has(exportName)
|
|
81
|
-
? _core.types.stringLiteral(exportName)
|
|
82
|
-
: _core.types.identifier(exportName),
|
|
83
|
-
exportValue,
|
|
84
|
-
),
|
|
85
|
-
)
|
|
86
|
-
}
|
|
87
|
-
statements.push(
|
|
88
|
-
_core.types.expressionStatement(
|
|
89
|
-
_core.types.callExpression(exportIdent, [
|
|
90
|
-
_core.types.objectExpression(objectProperties),
|
|
91
|
-
]),
|
|
92
|
-
),
|
|
93
|
-
)
|
|
94
|
-
}
|
|
95
|
-
} else {
|
|
96
|
-
const exportObj = path.scope.generateUid("exportObj")
|
|
97
|
-
statements.push(
|
|
98
|
-
_core.types.variableDeclaration("var", [
|
|
99
|
-
_core.types.variableDeclarator(
|
|
100
|
-
_core.types.identifier(exportObj),
|
|
101
|
-
_core.types.objectExpression([]),
|
|
102
|
-
),
|
|
103
|
-
]),
|
|
104
|
-
)
|
|
105
|
-
statements.push(
|
|
106
|
-
buildExportAll({
|
|
107
|
-
KEY: path.scope.generateUidIdentifier("key"),
|
|
108
|
-
EXPORT_OBJ: _core.types.identifier(exportObj),
|
|
109
|
-
TARGET: exportStarTarget,
|
|
110
|
-
}),
|
|
111
|
-
)
|
|
112
|
-
for (let i = 0; i < exportNames.length; i++) {
|
|
113
|
-
const exportName = exportNames[i]
|
|
114
|
-
const exportValue = exportValues[i]
|
|
115
|
-
statements.push(
|
|
116
|
-
_core.types.expressionStatement(
|
|
117
|
-
_core.types.assignmentExpression(
|
|
118
|
-
"=",
|
|
119
|
-
_core.types.memberExpression(
|
|
120
|
-
_core.types.identifier(exportObj),
|
|
121
|
-
_core.types.identifier(exportName),
|
|
122
|
-
),
|
|
123
|
-
exportValue,
|
|
124
|
-
),
|
|
125
|
-
),
|
|
126
|
-
)
|
|
127
|
-
}
|
|
128
|
-
statements.push(
|
|
129
|
-
_core.types.expressionStatement(
|
|
130
|
-
_core.types.callExpression(exportIdent, [
|
|
131
|
-
_core.types.identifier(exportObj),
|
|
132
|
-
]),
|
|
133
|
-
),
|
|
134
|
-
)
|
|
135
|
-
}
|
|
136
|
-
return statements
|
|
137
|
-
}
|
|
138
|
-
var _default = (0, _helperPluginUtils.declare)((api, options) => {
|
|
139
|
-
api.assertVersion(7)
|
|
140
|
-
const { systemGlobal = "System", allowTopLevelThis = false } = options
|
|
141
|
-
const reassignmentVisited = new WeakSet()
|
|
142
|
-
const reassignmentVisitor = {
|
|
143
|
-
"AssignmentExpression|UpdateExpression"(path) {
|
|
144
|
-
if (reassignmentVisited.has(path.node)) return
|
|
145
|
-
reassignmentVisited.add(path.node)
|
|
146
|
-
const arg = path.isAssignmentExpression()
|
|
147
|
-
? path.get("left")
|
|
148
|
-
: path.get("argument")
|
|
149
|
-
if (arg.isObjectPattern() || arg.isArrayPattern()) {
|
|
150
|
-
const exprs = [path.node]
|
|
151
|
-
for (const name of Object.keys(arg.getBindingIdentifiers())) {
|
|
152
|
-
if (this.scope.getBinding(name) !== path.scope.getBinding(name)) {
|
|
153
|
-
return
|
|
154
|
-
}
|
|
155
|
-
const exportedNames = this.exports[name]
|
|
156
|
-
if (!exportedNames) continue
|
|
157
|
-
for (const exportedName of exportedNames) {
|
|
158
|
-
exprs.push(
|
|
159
|
-
this.buildCall(exportedName, _core.types.identifier(name))
|
|
160
|
-
.expression,
|
|
161
|
-
)
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
path.replaceWith(_core.types.sequenceExpression(exprs))
|
|
165
|
-
return
|
|
166
|
-
}
|
|
167
|
-
if (!arg.isIdentifier()) return
|
|
168
|
-
const name = arg.node.name
|
|
169
|
-
|
|
170
|
-
if (this.scope.getBinding(name) !== path.scope.getBinding(name)) return
|
|
171
|
-
const exportedNames = this.exports[name]
|
|
172
|
-
if (!exportedNames) return
|
|
173
|
-
let node = path.node
|
|
174
|
-
|
|
175
|
-
const isPostUpdateExpression = _core.types.isUpdateExpression(node, {
|
|
176
|
-
prefix: false,
|
|
177
|
-
})
|
|
178
|
-
if (isPostUpdateExpression) {
|
|
179
|
-
node = _core.types.binaryExpression(
|
|
180
|
-
node.operator[0],
|
|
181
|
-
_core.types.unaryExpression(
|
|
182
|
-
"+",
|
|
183
|
-
_core.types.cloneNode(node.argument),
|
|
184
|
-
),
|
|
185
|
-
_core.types.numericLiteral(1),
|
|
186
|
-
)
|
|
187
|
-
}
|
|
188
|
-
for (const exportedName of exportedNames) {
|
|
189
|
-
node = this.buildCall(exportedName, node).expression
|
|
190
|
-
}
|
|
191
|
-
if (isPostUpdateExpression) {
|
|
192
|
-
node = _core.types.sequenceExpression([node, path.node])
|
|
193
|
-
}
|
|
194
|
-
path.replaceWith(node)
|
|
195
|
-
},
|
|
196
|
-
}
|
|
197
|
-
return {
|
|
198
|
-
name: "transform-modules-systemjs",
|
|
199
|
-
pre() {
|
|
200
|
-
this.file.set("@babel/plugin-transform-modules-*", "systemjs")
|
|
201
|
-
},
|
|
202
|
-
visitor: {
|
|
203
|
-
CallExpression(path, state) {
|
|
204
|
-
if (_core.types.isImport(path.node.callee)) {
|
|
205
|
-
if (!this.file.has("@babel/plugin-proposal-dynamic-import")) {
|
|
206
|
-
{
|
|
207
|
-
console.warn(MISSING_PLUGIN_WARNING)
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
path.replaceWith(
|
|
211
|
-
(0, _helperModuleTransforms.buildDynamicImport)(
|
|
212
|
-
path.node,
|
|
213
|
-
false,
|
|
214
|
-
true,
|
|
215
|
-
(specifier) =>
|
|
216
|
-
_core.types.callExpression(
|
|
217
|
-
_core.types.memberExpression(
|
|
218
|
-
_core.types.identifier(state.contextIdent),
|
|
219
|
-
_core.types.identifier("import"),
|
|
220
|
-
),
|
|
221
|
-
[specifier],
|
|
222
|
-
),
|
|
223
|
-
),
|
|
224
|
-
)
|
|
225
|
-
}
|
|
226
|
-
},
|
|
227
|
-
MetaProperty(path, state) {
|
|
228
|
-
if (
|
|
229
|
-
path.node.meta.name === "import" &&
|
|
230
|
-
path.node.property.name === "meta"
|
|
231
|
-
) {
|
|
232
|
-
path.replaceWith(
|
|
233
|
-
_core.types.memberExpression(
|
|
234
|
-
_core.types.identifier(state.contextIdent),
|
|
235
|
-
_core.types.identifier("meta"),
|
|
236
|
-
),
|
|
237
|
-
)
|
|
238
|
-
}
|
|
239
|
-
},
|
|
240
|
-
ReferencedIdentifier(path, state) {
|
|
241
|
-
if (
|
|
242
|
-
path.node.name === "__moduleName" &&
|
|
243
|
-
!path.scope.hasBinding("__moduleName")
|
|
244
|
-
) {
|
|
245
|
-
path.replaceWith(
|
|
246
|
-
_core.types.memberExpression(
|
|
247
|
-
_core.types.identifier(state.contextIdent),
|
|
248
|
-
_core.types.identifier("id"),
|
|
249
|
-
),
|
|
250
|
-
)
|
|
251
|
-
}
|
|
252
|
-
},
|
|
253
|
-
Program: {
|
|
254
|
-
enter(path, state) {
|
|
255
|
-
state.contextIdent = path.scope.generateUid("context")
|
|
256
|
-
state.stringSpecifiers = new Set()
|
|
257
|
-
if (!allowTopLevelThis) {
|
|
258
|
-
;(0, _helperModuleTransforms.rewriteThis)(path)
|
|
259
|
-
}
|
|
260
|
-
},
|
|
261
|
-
exit(path, state) {
|
|
262
|
-
const scope = path.scope
|
|
263
|
-
const exportIdent = scope.generateUid("export")
|
|
264
|
-
const { contextIdent, stringSpecifiers } = state
|
|
265
|
-
const exportMap = Object.create(null)
|
|
266
|
-
const modules = []
|
|
267
|
-
const beforeBody = []
|
|
268
|
-
const setters = []
|
|
269
|
-
const sources = []
|
|
270
|
-
const variableIds = []
|
|
271
|
-
const removedPaths = []
|
|
272
|
-
function addExportName(key, val) {
|
|
273
|
-
exportMap[key] = exportMap[key] || []
|
|
274
|
-
exportMap[key].push(val)
|
|
275
|
-
}
|
|
276
|
-
function pushModule(source, key, specifiers) {
|
|
277
|
-
let module
|
|
278
|
-
modules.forEach(function (m) {
|
|
279
|
-
if (m.key === source) {
|
|
280
|
-
module = m
|
|
281
|
-
}
|
|
282
|
-
})
|
|
283
|
-
if (!module) {
|
|
284
|
-
modules.push(
|
|
285
|
-
(module = {
|
|
286
|
-
key: source,
|
|
287
|
-
imports: [],
|
|
288
|
-
exports: [],
|
|
289
|
-
}),
|
|
290
|
-
)
|
|
291
|
-
}
|
|
292
|
-
module[key] = module[key].concat(specifiers)
|
|
293
|
-
}
|
|
294
|
-
function buildExportCall(name, val) {
|
|
295
|
-
return _core.types.expressionStatement(
|
|
296
|
-
_core.types.callExpression(_core.types.identifier(exportIdent), [
|
|
297
|
-
_core.types.stringLiteral(name),
|
|
298
|
-
val,
|
|
299
|
-
]),
|
|
300
|
-
)
|
|
301
|
-
}
|
|
302
|
-
const exportNames = []
|
|
303
|
-
const exportValues = []
|
|
304
|
-
const body = path.get("body")
|
|
305
|
-
for (const path of body) {
|
|
306
|
-
if (path.isFunctionDeclaration()) {
|
|
307
|
-
beforeBody.push(path.node)
|
|
308
|
-
removedPaths.push(path)
|
|
309
|
-
} else if (path.isClassDeclaration()) {
|
|
310
|
-
variableIds.push(_core.types.cloneNode(path.node.id))
|
|
311
|
-
path.replaceWith(
|
|
312
|
-
_core.types.expressionStatement(
|
|
313
|
-
_core.types.assignmentExpression(
|
|
314
|
-
"=",
|
|
315
|
-
_core.types.cloneNode(path.node.id),
|
|
316
|
-
_core.types.toExpression(path.node),
|
|
317
|
-
),
|
|
318
|
-
),
|
|
319
|
-
)
|
|
320
|
-
} else if (path.isVariableDeclaration()) {
|
|
321
|
-
path.node.kind = "var"
|
|
322
|
-
} else if (path.isImportDeclaration()) {
|
|
323
|
-
const source = path.node.source.value
|
|
324
|
-
pushModule(source, "imports", path.node.specifiers)
|
|
325
|
-
for (const name of Object.keys(path.getBindingIdentifiers())) {
|
|
326
|
-
scope.removeBinding(name)
|
|
327
|
-
variableIds.push(_core.types.identifier(name))
|
|
328
|
-
}
|
|
329
|
-
path.remove()
|
|
330
|
-
} else if (path.isExportAllDeclaration()) {
|
|
331
|
-
pushModule(path.node.source.value, "exports", path.node)
|
|
332
|
-
path.remove()
|
|
333
|
-
} else if (path.isExportDefaultDeclaration()) {
|
|
334
|
-
const declar = path.node.declaration
|
|
335
|
-
if (_core.types.isClassDeclaration(declar)) {
|
|
336
|
-
const id = declar.id
|
|
337
|
-
if (id) {
|
|
338
|
-
exportNames.push("default")
|
|
339
|
-
exportValues.push(scope.buildUndefinedNode())
|
|
340
|
-
variableIds.push(_core.types.cloneNode(id))
|
|
341
|
-
addExportName(id.name, "default")
|
|
342
|
-
path.replaceWith(
|
|
343
|
-
_core.types.expressionStatement(
|
|
344
|
-
_core.types.assignmentExpression(
|
|
345
|
-
"=",
|
|
346
|
-
_core.types.cloneNode(id),
|
|
347
|
-
_core.types.toExpression(declar),
|
|
348
|
-
),
|
|
349
|
-
),
|
|
350
|
-
)
|
|
351
|
-
} else {
|
|
352
|
-
exportNames.push("default")
|
|
353
|
-
exportValues.push(_core.types.toExpression(declar))
|
|
354
|
-
removedPaths.push(path)
|
|
355
|
-
}
|
|
356
|
-
} else if (_core.types.isFunctionDeclaration(declar)) {
|
|
357
|
-
const id = declar.id
|
|
358
|
-
if (id) {
|
|
359
|
-
beforeBody.push(declar)
|
|
360
|
-
exportNames.push("default")
|
|
361
|
-
exportValues.push(_core.types.cloneNode(id))
|
|
362
|
-
addExportName(id.name, "default")
|
|
363
|
-
} else {
|
|
364
|
-
exportNames.push("default")
|
|
365
|
-
exportValues.push(_core.types.toExpression(declar))
|
|
366
|
-
}
|
|
367
|
-
removedPaths.push(path)
|
|
368
|
-
} else {
|
|
369
|
-
path.replaceWith(buildExportCall("default", declar))
|
|
370
|
-
}
|
|
371
|
-
} else if (path.isExportNamedDeclaration()) {
|
|
372
|
-
const declar = path.node.declaration
|
|
373
|
-
if (declar) {
|
|
374
|
-
path.replaceWith(declar)
|
|
375
|
-
if (_core.types.isFunction(declar)) {
|
|
376
|
-
const name = declar.id.name
|
|
377
|
-
addExportName(name, name)
|
|
378
|
-
beforeBody.push(declar)
|
|
379
|
-
exportNames.push(name)
|
|
380
|
-
exportValues.push(_core.types.cloneNode(declar.id))
|
|
381
|
-
removedPaths.push(path)
|
|
382
|
-
} else if (_core.types.isClass(declar)) {
|
|
383
|
-
const name = declar.id.name
|
|
384
|
-
exportNames.push(name)
|
|
385
|
-
exportValues.push(scope.buildUndefinedNode())
|
|
386
|
-
variableIds.push(_core.types.cloneNode(declar.id))
|
|
387
|
-
path.replaceWith(
|
|
388
|
-
_core.types.expressionStatement(
|
|
389
|
-
_core.types.assignmentExpression(
|
|
390
|
-
"=",
|
|
391
|
-
_core.types.cloneNode(declar.id),
|
|
392
|
-
_core.types.toExpression(declar),
|
|
393
|
-
),
|
|
394
|
-
),
|
|
395
|
-
)
|
|
396
|
-
addExportName(name, name)
|
|
397
|
-
} else {
|
|
398
|
-
if (_core.types.isVariableDeclaration(declar)) {
|
|
399
|
-
declar.kind = "var"
|
|
400
|
-
}
|
|
401
|
-
for (const name of Object.keys(
|
|
402
|
-
_core.types.getBindingIdentifiers(declar),
|
|
403
|
-
)) {
|
|
404
|
-
addExportName(name, name)
|
|
405
|
-
}
|
|
406
|
-
}
|
|
407
|
-
} else {
|
|
408
|
-
const specifiers = path.node.specifiers
|
|
409
|
-
if (specifiers != null && specifiers.length) {
|
|
410
|
-
if (path.node.source) {
|
|
411
|
-
pushModule(path.node.source.value, "exports", specifiers)
|
|
412
|
-
path.remove()
|
|
413
|
-
} else {
|
|
414
|
-
const nodes = []
|
|
415
|
-
for (const specifier of specifiers) {
|
|
416
|
-
const { local, exported } = specifier
|
|
417
|
-
const binding = scope.getBinding(local.name)
|
|
418
|
-
const exportedName = getExportSpecifierName(
|
|
419
|
-
exported,
|
|
420
|
-
stringSpecifiers,
|
|
421
|
-
)
|
|
422
|
-
if (
|
|
423
|
-
binding &&
|
|
424
|
-
_core.types.isFunctionDeclaration(binding.path.node)
|
|
425
|
-
) {
|
|
426
|
-
exportNames.push(exportedName)
|
|
427
|
-
exportValues.push(_core.types.cloneNode(local))
|
|
428
|
-
} else if (!binding) {
|
|
429
|
-
nodes.push(buildExportCall(exportedName, local))
|
|
430
|
-
}
|
|
431
|
-
addExportName(local.name, exportedName)
|
|
432
|
-
}
|
|
433
|
-
path.replaceWithMultiple(nodes)
|
|
434
|
-
}
|
|
435
|
-
} else {
|
|
436
|
-
path.remove()
|
|
437
|
-
}
|
|
438
|
-
}
|
|
439
|
-
}
|
|
440
|
-
}
|
|
441
|
-
modules.forEach(function (specifiers) {
|
|
442
|
-
const setterBody = []
|
|
443
|
-
// --- START FORK CHANGES ---
|
|
444
|
-
const targetHint = state.opts.generateIdentifierHint
|
|
445
|
-
? state.opts.generateIdentifierHint(specifiers.key)
|
|
446
|
-
: specifiers.key
|
|
447
|
-
const target = scope.generateUid(targetHint)
|
|
448
|
-
// --- END FORK CHANGES ---
|
|
449
|
-
for (let specifier of specifiers.imports) {
|
|
450
|
-
if (_core.types.isImportNamespaceSpecifier(specifier)) {
|
|
451
|
-
setterBody.push(
|
|
452
|
-
_core.types.expressionStatement(
|
|
453
|
-
_core.types.assignmentExpression(
|
|
454
|
-
"=",
|
|
455
|
-
specifier.local,
|
|
456
|
-
_core.types.identifier(target),
|
|
457
|
-
),
|
|
458
|
-
),
|
|
459
|
-
)
|
|
460
|
-
} else if (_core.types.isImportDefaultSpecifier(specifier)) {
|
|
461
|
-
specifier = _core.types.importSpecifier(
|
|
462
|
-
specifier.local,
|
|
463
|
-
_core.types.identifier("default"),
|
|
464
|
-
)
|
|
465
|
-
}
|
|
466
|
-
if (_core.types.isImportSpecifier(specifier)) {
|
|
467
|
-
const { imported } = specifier
|
|
468
|
-
setterBody.push(
|
|
469
|
-
_core.types.expressionStatement(
|
|
470
|
-
_core.types.assignmentExpression(
|
|
471
|
-
"=",
|
|
472
|
-
specifier.local,
|
|
473
|
-
_core.types.memberExpression(
|
|
474
|
-
_core.types.identifier(target),
|
|
475
|
-
specifier.imported,
|
|
476
|
-
imported.type === "StringLiteral",
|
|
477
|
-
),
|
|
478
|
-
),
|
|
479
|
-
),
|
|
480
|
-
)
|
|
481
|
-
}
|
|
482
|
-
}
|
|
483
|
-
if (specifiers.exports.length) {
|
|
484
|
-
const exportNames = []
|
|
485
|
-
const exportValues = []
|
|
486
|
-
let hasExportStar = false
|
|
487
|
-
for (const node of specifiers.exports) {
|
|
488
|
-
if (_core.types.isExportAllDeclaration(node)) {
|
|
489
|
-
hasExportStar = true
|
|
490
|
-
} else if (_core.types.isExportSpecifier(node)) {
|
|
491
|
-
const exportedName = getExportSpecifierName(
|
|
492
|
-
node.exported,
|
|
493
|
-
stringSpecifiers,
|
|
494
|
-
)
|
|
495
|
-
exportNames.push(exportedName)
|
|
496
|
-
exportValues.push(
|
|
497
|
-
_core.types.memberExpression(
|
|
498
|
-
_core.types.identifier(target),
|
|
499
|
-
node.local,
|
|
500
|
-
_core.types.isStringLiteral(node.local),
|
|
501
|
-
),
|
|
502
|
-
)
|
|
503
|
-
} else {
|
|
504
|
-
}
|
|
505
|
-
}
|
|
506
|
-
|
|
507
|
-
setterBody.push(
|
|
508
|
-
...constructExportCall(
|
|
509
|
-
path,
|
|
510
|
-
_core.types.identifier(exportIdent),
|
|
511
|
-
exportNames,
|
|
512
|
-
exportValues,
|
|
513
|
-
hasExportStar ? _core.types.identifier(target) : null,
|
|
514
|
-
stringSpecifiers,
|
|
515
|
-
),
|
|
516
|
-
)
|
|
517
|
-
}
|
|
518
|
-
sources.push(_core.types.stringLiteral(specifiers.key))
|
|
519
|
-
setters.push(
|
|
520
|
-
_core.types.functionExpression(
|
|
521
|
-
null,
|
|
522
|
-
[_core.types.identifier(target)],
|
|
523
|
-
_core.types.blockStatement(setterBody),
|
|
524
|
-
),
|
|
525
|
-
)
|
|
526
|
-
})
|
|
527
|
-
let moduleName = (0, _helperModuleTransforms.getModuleName)(
|
|
528
|
-
this.file.opts,
|
|
529
|
-
options,
|
|
530
|
-
)
|
|
531
|
-
if (moduleName) moduleName = _core.types.stringLiteral(moduleName)
|
|
532
|
-
;(0, _helperHoistVariables.default)(path, (id, name, hasInit) => {
|
|
533
|
-
variableIds.push(id)
|
|
534
|
-
if (!hasInit && name in exportMap) {
|
|
535
|
-
for (const exported of exportMap[name]) {
|
|
536
|
-
exportNames.push(exported)
|
|
537
|
-
exportValues.push(scope.buildUndefinedNode())
|
|
538
|
-
}
|
|
539
|
-
}
|
|
540
|
-
})
|
|
541
|
-
if (variableIds.length) {
|
|
542
|
-
beforeBody.unshift(
|
|
543
|
-
_core.types.variableDeclaration(
|
|
544
|
-
"var",
|
|
545
|
-
variableIds.map((id) => _core.types.variableDeclarator(id)),
|
|
546
|
-
),
|
|
547
|
-
)
|
|
548
|
-
}
|
|
549
|
-
if (exportNames.length) {
|
|
550
|
-
beforeBody.push(
|
|
551
|
-
...constructExportCall(
|
|
552
|
-
path,
|
|
553
|
-
_core.types.identifier(exportIdent),
|
|
554
|
-
exportNames,
|
|
555
|
-
exportValues,
|
|
556
|
-
null,
|
|
557
|
-
stringSpecifiers,
|
|
558
|
-
),
|
|
559
|
-
)
|
|
560
|
-
}
|
|
561
|
-
path.traverse(reassignmentVisitor, {
|
|
562
|
-
exports: exportMap,
|
|
563
|
-
buildCall: buildExportCall,
|
|
564
|
-
scope,
|
|
565
|
-
})
|
|
566
|
-
for (const path of removedPaths) {
|
|
567
|
-
path.remove()
|
|
568
|
-
}
|
|
569
|
-
let hasTLA = false
|
|
570
|
-
path.traverse({
|
|
571
|
-
AwaitExpression(path) {
|
|
572
|
-
hasTLA = true
|
|
573
|
-
path.stop()
|
|
574
|
-
},
|
|
575
|
-
Function(path) {
|
|
576
|
-
path.skip()
|
|
577
|
-
},
|
|
578
|
-
noScope: true,
|
|
579
|
-
})
|
|
580
|
-
path.node.body = [
|
|
581
|
-
buildTemplate({
|
|
582
|
-
SYSTEM_REGISTER: _core.types.memberExpression(
|
|
583
|
-
_core.types.identifier(systemGlobal),
|
|
584
|
-
_core.types.identifier("register"),
|
|
585
|
-
),
|
|
586
|
-
BEFORE_BODY: beforeBody,
|
|
587
|
-
MODULE_NAME: moduleName,
|
|
588
|
-
SETTERS: _core.types.arrayExpression(setters),
|
|
589
|
-
EXECUTE: _core.types.functionExpression(
|
|
590
|
-
null,
|
|
591
|
-
[],
|
|
592
|
-
_core.types.blockStatement(path.node.body),
|
|
593
|
-
false,
|
|
594
|
-
hasTLA,
|
|
595
|
-
),
|
|
596
|
-
SOURCES: _core.types.arrayExpression(sources),
|
|
597
|
-
EXPORT_IDENTIFIER: _core.types.identifier(exportIdent),
|
|
598
|
-
CONTEXT_IDENTIFIER: _core.types.identifier(contextIdent),
|
|
599
|
-
}),
|
|
600
|
-
]
|
|
601
|
-
},
|
|
602
|
-
},
|
|
603
|
-
},
|
|
604
|
-
}
|
|
605
|
-
})
|
|
606
|
-
exports.default = _default
|
|
607
|
-
|
|
608
|
-
//# sourceMappingURL=index.js.map
|