This commit is contained in:
2022-07-18 02:50:52 +00:00
parent befd344ab0
commit 06181b34d6
8569 changed files with 818704 additions and 352705 deletions
+1 -1
View File
@@ -2,7 +2,7 @@
> This plugin transforms ES2015 modules to SystemJS
See our website [@babel/plugin-transform-modules-systemjs](https://babeljs.io/docs/en/next/babel-plugin-transform-modules-systemjs.html) for more information.
See our website [@babel/plugin-transform-modules-systemjs](https://babeljs.io/docs/en/babel-plugin-transform-modules-systemjs) for more information.
## Install
+139 -76
View File
@@ -4,10 +4,11 @@ Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
exports.getExportSpecifierName = getExportSpecifierName;
var _helperPluginUtils = require("@babel/helper-plugin-utils");
var _helperHoistVariables = _interopRequireDefault(require("@babel/helper-hoist-variables"));
var _helperHoistVariables = require("@babel/helper-hoist-variables");
var _core = require("@babel/core");
@@ -15,46 +16,69 @@ var _utils = require("babel-plugin-dynamic-import-node/utils");
var _helperModuleTransforms = require("@babel/helper-module-transforms");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var _helperValidatorIdentifier = require("@babel/helper-validator-identifier");
const buildTemplate = (0, _core.template)(`
const buildTemplate = _core.template.statement(`
SYSTEM_REGISTER(MODULE_NAME, SOURCES, function (EXPORT_IDENTIFIER, CONTEXT_IDENTIFIER) {
"use strict";
BEFORE_BODY;
return {
setters: SETTERS,
execute: function () {
BODY;
}
execute: EXECUTE,
};
});
`);
const buildExportAll = (0, _core.template)(`
const buildExportAll = _core.template.statement(`
for (var KEY in TARGET) {
if (KEY !== "default" && KEY !== "__esModule") EXPORT_OBJ[KEY] = TARGET[KEY];
}
`);
const MISSING_PLUGIN_WARNING = `\
WARNING: Dynamic import() transformation must be enabled using the
@babel/plugin-proposal-dynamic-import plugin. Babel 8 will
no longer transform import() without using that plugin.
`;
const MISSING_PLUGIN_ERROR = `\
ERROR: Dynamic import() transformation must be enabled using the
@babel/plugin-proposal-dynamic-import plugin. Babel 8
no longer transforms import() without using that plugin.
`;
function constructExportCall(path, exportIdent, exportNames, exportValues, exportStarTarget) {
const statements = [];
function getExportSpecifierName(node, stringSpecifiers) {
if (node.type === "Identifier") {
return node.name;
} else if (node.type === "StringLiteral") {
const stringValue = node.value;
if (exportNames.length === 1) {
statements.push(_core.types.expressionStatement(_core.types.callExpression(exportIdent, [_core.types.stringLiteral(exportNames[0]), exportValues[0]])));
} else if (!exportStarTarget) {
const objectProperties = [];
for (let i = 0; i < exportNames.length; i++) {
const exportName = exportNames[i];
const exportValue = exportValues[i];
objectProperties.push(_core.types.objectProperty(_core.types.identifier(exportName), exportValue));
if (!(0, _helperValidatorIdentifier.isIdentifierName)(stringValue)) {
stringSpecifiers.add(stringValue);
}
statements.push(_core.types.expressionStatement(_core.types.callExpression(exportIdent, [_core.types.objectExpression(objectProperties)])));
return stringValue;
} else {
throw new Error(`Expected export specifier to be either Identifier or StringLiteral, got ${node.type}`);
}
}
function constructExportCall(path, exportIdent, exportNames, exportValues, exportStarTarget, stringSpecifiers) {
const statements = [];
if (!exportStarTarget) {
if (exportNames.length === 1) {
statements.push(_core.types.expressionStatement(_core.types.callExpression(exportIdent, [_core.types.stringLiteral(exportNames[0]), exportValues[0]])));
} else {
const objectProperties = [];
for (let i = 0; i < exportNames.length; i++) {
const exportName = exportNames[i];
const exportValue = exportValues[i];
objectProperties.push(_core.types.objectProperty(stringSpecifiers.has(exportName) ? _core.types.stringLiteral(exportName) : _core.types.identifier(exportName), exportValue));
}
statements.push(_core.types.expressionStatement(_core.types.callExpression(exportIdent, [_core.types.objectExpression(objectProperties)])));
}
} else {
const exportObj = path.scope.generateUid("exportObj");
statements.push(_core.types.variableDeclaration("var", [_core.types.variableDeclarator(_core.types.identifier(exportObj), _core.types.objectExpression([]))]));
@@ -82,12 +106,12 @@ var _default = (0, _helperPluginUtils.declare)((api, options) => {
systemGlobal = "System",
allowTopLevelThis = false
} = options;
const IGNORE_REASSIGNMENT_SYMBOL = Symbol();
const reassignmentVisited = new WeakSet();
const reassignmentVisitor = {
"AssignmentExpression|UpdateExpression"(path) {
if (path.node[IGNORE_REASSIGNMENT_SYMBOL]) return;
path.node[IGNORE_REASSIGNMENT_SYMBOL] = true;
const arg = path.get(path.isAssignmentExpression() ? "left" : "argument");
if (reassignmentVisited.has(path.node)) return;
reassignmentVisited.add(path.node);
const arg = path.isAssignmentExpression() ? path.get("left") : path.get("argument");
if (arg.isObjectPattern() || arg.isArrayPattern()) {
const exprs = [path.node];
@@ -98,7 +122,7 @@ var _default = (0, _helperPluginUtils.declare)((api, options) => {
}
const exportedNames = this.exports[name];
if (!exportedNames) return;
if (!exportedNames) continue;
for (const exportedName of exportedNames) {
exprs.push(this.buildCall(exportedName, _core.types.identifier(name)).expression);
@@ -115,7 +139,8 @@ var _default = (0, _helperPluginUtils.declare)((api, options) => {
const exportedNames = this.exports[name];
if (!exportedNames) return;
let node = path.node;
const isPostUpdateExpression = path.isUpdateExpression({
const isPostUpdateExpression = _core.types.isUpdateExpression(node, {
prefix: false
});
@@ -146,7 +171,9 @@ var _default = (0, _helperPluginUtils.declare)((api, options) => {
CallExpression(path, state) {
if (_core.types.isImport(path.node.callee)) {
if (!this.file.has("@babel/plugin-proposal-dynamic-import")) {
console.warn(MISSING_PLUGIN_WARNING);
{
console.warn(MISSING_PLUGIN_WARNING);
}
}
path.replaceWith(_core.types.callExpression(_core.types.memberExpression(_core.types.identifier(state.contextIdent), _core.types.identifier("import")), [(0, _utils.getImportSource)(_core.types, path.node)]));
@@ -168,6 +195,7 @@ var _default = (0, _helperPluginUtils.declare)((api, options) => {
Program: {
enter(path, state) {
state.contextIdent = path.scope.generateUid("context");
state.stringSpecifiers = new Set();
if (!allowTopLevelThis) {
(0, _helperModuleTransforms.rewriteThis)(path);
@@ -175,12 +203,15 @@ var _default = (0, _helperPluginUtils.declare)((api, options) => {
},
exit(path, state) {
const undefinedIdent = path.scope.buildUndefinedNode();
const exportIdent = path.scope.generateUid("export");
const contextIdent = state.contextIdent;
const scope = path.scope;
const exportIdent = scope.generateUid("export");
const {
contextIdent,
stringSpecifiers
} = state;
const exportMap = Object.create(null);
const modules = [];
let beforeBody = [];
const beforeBody = [];
const setters = [];
const sources = [];
const variableIds = [];
@@ -223,14 +254,16 @@ var _default = (0, _helperPluginUtils.declare)((api, options) => {
beforeBody.push(path.node);
removedPaths.push(path);
} else if (path.isClassDeclaration()) {
variableIds.push(path.node.id);
variableIds.push(_core.types.cloneNode(path.node.id));
path.replaceWith(_core.types.expressionStatement(_core.types.assignmentExpression("=", _core.types.cloneNode(path.node.id), _core.types.toExpression(path.node))));
} else if (path.isVariableDeclaration()) {
path.node.kind = "var";
} else if (path.isImportDeclaration()) {
const source = path.node.source.value;
pushModule(source, "imports", path.node.specifiers);
for (const name of Object.keys(path.getBindingIdentifiers())) {
path.scope.removeBinding(name);
scope.removeBinding(name);
variableIds.push(_core.types.identifier(name));
}
@@ -239,66 +272,72 @@ var _default = (0, _helperPluginUtils.declare)((api, options) => {
pushModule(path.node.source.value, "exports", path.node);
path.remove();
} else if (path.isExportDefaultDeclaration()) {
const declar = path.get("declaration");
const id = declar.node.id;
const declar = path.node.declaration;
if (_core.types.isClassDeclaration(declar)) {
const id = declar.id;
if (declar.isClassDeclaration()) {
if (id) {
exportNames.push("default");
exportValues.push(undefinedIdent);
variableIds.push(id);
exportValues.push(scope.buildUndefinedNode());
variableIds.push(_core.types.cloneNode(id));
addExportName(id.name, "default");
path.replaceWith(_core.types.expressionStatement(_core.types.assignmentExpression("=", _core.types.cloneNode(id), _core.types.toExpression(declar.node))));
path.replaceWith(_core.types.expressionStatement(_core.types.assignmentExpression("=", _core.types.cloneNode(id), _core.types.toExpression(declar))));
} else {
exportNames.push("default");
exportValues.push(_core.types.toExpression(declar.node));
exportValues.push(_core.types.toExpression(declar));
removedPaths.push(path);
}
} else if (declar.isFunctionDeclaration()) {
} else if (_core.types.isFunctionDeclaration(declar)) {
const id = declar.id;
if (id) {
beforeBody.push(declar.node);
beforeBody.push(declar);
exportNames.push("default");
exportValues.push(_core.types.cloneNode(id));
addExportName(id.name, "default");
} else {
exportNames.push("default");
exportValues.push(_core.types.toExpression(declar.node));
exportValues.push(_core.types.toExpression(declar));
}
removedPaths.push(path);
} else {
path.replaceWith(buildExportCall("default", declar.node));
path.replaceWith(buildExportCall("default", declar));
}
} else if (path.isExportNamedDeclaration()) {
const declar = path.get("declaration");
const declar = path.node.declaration;
if (declar.node) {
if (declar) {
path.replaceWith(declar);
if (path.isFunction()) {
const node = declar.node;
const name = node.id.name;
if (_core.types.isFunction(declar)) {
const name = declar.id.name;
addExportName(name, name);
beforeBody.push(node);
beforeBody.push(declar);
exportNames.push(name);
exportValues.push(_core.types.cloneNode(node.id));
exportValues.push(_core.types.cloneNode(declar.id));
removedPaths.push(path);
} else if (path.isClass()) {
const name = declar.node.id.name;
} else if (_core.types.isClass(declar)) {
const name = declar.id.name;
exportNames.push(name);
exportValues.push(undefinedIdent);
variableIds.push(declar.node.id);
path.replaceWith(_core.types.expressionStatement(_core.types.assignmentExpression("=", _core.types.cloneNode(declar.node.id), _core.types.toExpression(declar.node))));
exportValues.push(scope.buildUndefinedNode());
variableIds.push(_core.types.cloneNode(declar.id));
path.replaceWith(_core.types.expressionStatement(_core.types.assignmentExpression("=", _core.types.cloneNode(declar.id), _core.types.toExpression(declar))));
addExportName(name, name);
} else {
for (const name of Object.keys(declar.getBindingIdentifiers())) {
if (_core.types.isVariableDeclaration(declar)) {
declar.kind = "var";
}
for (const name of Object.keys(_core.types.getBindingIdentifiers(declar))) {
addExportName(name, name);
}
}
} else {
const specifiers = path.node.specifiers;
if (specifiers == null ? void 0 : specifiers.length) {
if (specifiers != null && specifiers.length) {
if (path.node.source) {
pushModule(path.node.source.value, "exports", specifiers);
path.remove();
@@ -306,16 +345,21 @@ var _default = (0, _helperPluginUtils.declare)((api, options) => {
const nodes = [];
for (const specifier of specifiers) {
const binding = path.scope.getBinding(specifier.local.name);
const {
local,
exported
} = specifier;
const binding = scope.getBinding(local.name);
const exportedName = getExportSpecifierName(exported, stringSpecifiers);
if (binding && _core.types.isFunctionDeclaration(binding.path.node)) {
exportNames.push(specifier.exported.name);
exportValues.push(_core.types.cloneNode(specifier.local));
exportNames.push(exportedName);
exportValues.push(_core.types.cloneNode(local));
} else if (!binding) {
nodes.push(buildExportCall(specifier.exported.name, specifier.local));
}
nodes.push(buildExportCall(exportedName, local));
}
addExportName(specifier.local.name, specifier.exported.name);
addExportName(local.name, exportedName);
}
path.replaceWithMultiple(nodes);
@@ -328,8 +372,8 @@ var _default = (0, _helperPluginUtils.declare)((api, options) => {
}
modules.forEach(function (specifiers) {
let setterBody = [];
const target = path.scope.generateUid(specifiers.key);
const setterBody = [];
const target = scope.generateUid(specifiers.key);
for (let specifier of specifiers.imports) {
if (_core.types.isImportNamespaceSpecifier(specifier)) {
@@ -339,7 +383,10 @@ var _default = (0, _helperPluginUtils.declare)((api, options) => {
}
if (_core.types.isImportSpecifier(specifier)) {
setterBody.push(_core.types.expressionStatement(_core.types.assignmentExpression("=", specifier.local, _core.types.memberExpression(_core.types.identifier(target), specifier.imported))));
const {
imported
} = specifier;
setterBody.push(_core.types.expressionStatement(_core.types.assignmentExpression("=", specifier.local, _core.types.memberExpression(_core.types.identifier(target), specifier.imported, imported.type === "StringLiteral"))));
}
}
@@ -352,12 +399,13 @@ var _default = (0, _helperPluginUtils.declare)((api, options) => {
if (_core.types.isExportAllDeclaration(node)) {
hasExportStar = true;
} else if (_core.types.isExportSpecifier(node)) {
exportNames.push(node.exported.name);
exportValues.push(_core.types.memberExpression(_core.types.identifier(target), node.local));
const exportedName = getExportSpecifierName(node.exported, stringSpecifiers);
exportNames.push(exportedName);
exportValues.push(_core.types.memberExpression(_core.types.identifier(target), node.local, _core.types.isStringLiteral(node.local)));
} else {}
}
setterBody = setterBody.concat(constructExportCall(path, _core.types.identifier(exportIdent), exportNames, exportValues, hasExportStar ? _core.types.identifier(target) : null));
setterBody.push(...constructExportCall(path, _core.types.identifier(exportIdent), exportNames, exportValues, hasExportStar ? _core.types.identifier(target) : null, stringSpecifiers));
}
sources.push(_core.types.stringLiteral(specifiers.key));
@@ -368,37 +416,52 @@ var _default = (0, _helperPluginUtils.declare)((api, options) => {
(0, _helperHoistVariables.default)(path, (id, name, hasInit) => {
variableIds.push(id);
if (!hasInit) {
exportNames.push(name);
exportValues.push(undefinedIdent);
if (!hasInit && name in exportMap) {
for (const exported of exportMap[name]) {
exportNames.push(exported);
exportValues.push(scope.buildUndefinedNode());
}
}
}, null);
});
if (variableIds.length) {
beforeBody.unshift(_core.types.variableDeclaration("var", variableIds.map(id => _core.types.variableDeclarator(id))));
}
if (exportNames.length) {
beforeBody = beforeBody.concat(constructExportCall(path, _core.types.identifier(exportIdent), exportNames, exportValues, null));
beforeBody.push(...constructExportCall(path, _core.types.identifier(exportIdent), exportNames, exportValues, null, stringSpecifiers));
}
path.traverse(reassignmentVisitor, {
exports: exportMap,
buildCall: buildExportCall,
scope: path.scope
scope
});
for (const path of removedPaths) {
path.remove();
}
let hasTLA = false;
path.traverse({
AwaitExpression(path) {
hasTLA = true;
path.stop();
},
Function(path) {
path.skip();
},
noScope: true
});
path.node.body = [buildTemplate({
SYSTEM_REGISTER: _core.types.memberExpression(_core.types.identifier(systemGlobal), _core.types.identifier("register")),
BEFORE_BODY: beforeBody,
MODULE_NAME: moduleName,
SETTERS: _core.types.arrayExpression(setters),
EXECUTE: _core.types.functionExpression(null, [], _core.types.blockStatement(path.node.body), false, hasTLA),
SOURCES: _core.types.arrayExpression(sources),
BODY: path.node.body,
EXPORT_IDENTIFIER: _core.types.identifier(exportIdent),
CONTEXT_IDENTIFIER: _core.types.identifier(contextIdent)
})];
+29 -21
View File
@@ -1,55 +1,62 @@
{
"_args": [
[
"@babel/plugin-transform-modules-systemjs@7.10.4",
"/mnt/Foxconn/Digitalent/Deverloper/liff-push_2series"
"@babel/plugin-transform-modules-systemjs@7.18.6",
"/home/node/nuxt"
]
],
"_from": "@babel/plugin-transform-modules-systemjs@7.10.4",
"_id": "@babel/plugin-transform-modules-systemjs@7.10.4",
"_from": "@babel/plugin-transform-modules-systemjs@7.18.6",
"_id": "@babel/plugin-transform-modules-systemjs@7.18.6",
"_inBundle": false,
"_integrity": "sha512-Tb28LlfxrTiOTGtZFsvkjpyjCl9IoaRI52AEU/VIwOwvDQWtbNJsAqTXzh+5R7i74e/OZHH2c2w2fsOqAfnQYQ==",
"_integrity": "sha512-UbPYpXxLjTw6w6yXX2BYNxF3p6QY225wcTkfQCy3OMnSlS/C3xGtwUjEzGkldb/sy6PWLiCQ3NbYfjWUTI3t4g==",
"_location": "/@babel/plugin-transform-modules-systemjs",
"_phantomChildren": {},
"_requested": {
"type": "version",
"registry": true,
"raw": "@babel/plugin-transform-modules-systemjs@7.10.4",
"raw": "@babel/plugin-transform-modules-systemjs@7.18.6",
"name": "@babel/plugin-transform-modules-systemjs",
"escapedName": "@babel%2fplugin-transform-modules-systemjs",
"scope": "@babel",
"rawSpec": "7.10.4",
"rawSpec": "7.18.6",
"saveSpec": null,
"fetchSpec": "7.10.4"
"fetchSpec": "7.18.6"
},
"_requiredBy": [
"/@babel/preset-env"
],
"_resolved": "https://registry.npmjs.org/@babel/plugin-transform-modules-systemjs/-/plugin-transform-modules-systemjs-7.10.4.tgz",
"_spec": "7.10.4",
"_where": "/mnt/Foxconn/Digitalent/Deverloper/liff-push_2series",
"_resolved": "https://registry.npmjs.org/@babel/plugin-transform-modules-systemjs/-/plugin-transform-modules-systemjs-7.18.6.tgz",
"_spec": "7.18.6",
"_where": "/home/node/nuxt",
"author": {
"name": "The Babel Team",
"url": "https://babel.dev/team"
},
"bugs": {
"url": "https://github.com/babel/babel/issues"
},
"dependencies": {
"@babel/helper-hoist-variables": "^7.10.4",
"@babel/helper-module-transforms": "^7.10.4",
"@babel/helper-plugin-utils": "^7.10.4",
"@babel/helper-hoist-variables": "^7.18.6",
"@babel/helper-module-transforms": "^7.18.6",
"@babel/helper-plugin-utils": "^7.18.6",
"@babel/helper-validator-identifier": "^7.18.6",
"babel-plugin-dynamic-import-node": "^2.3.3"
},
"description": "This plugin transforms ES2015 modules to SystemJS",
"devDependencies": {
"@babel/core": "^7.10.4",
"@babel/helper-plugin-test-runner": "^7.10.4",
"@babel/plugin-syntax-dynamic-import": "^7.8.0"
"@babel/core": "^7.18.6",
"@babel/helper-plugin-test-runner": "^7.18.6",
"@babel/plugin-syntax-dynamic-import": "^7.8.3"
},
"gitHead": "7fd40d86a0d03ff0e9c3ea16b29689945433d4df",
"homepage": "https://github.com/babel/babel#readme",
"engines": {
"node": ">=6.9.0"
},
"homepage": "https://babel.dev/docs/en/next/babel-plugin-transform-modules-systemjs",
"keywords": [
"babel-plugin"
],
"license": "MIT",
"main": "lib/index.js",
"main": "./lib/index.js",
"name": "@babel/plugin-transform-modules-systemjs",
"peerDependencies": {
"@babel/core": "^7.0.0-0"
@@ -62,5 +69,6 @@
"url": "git+https://github.com/babel/babel.git",
"directory": "packages/babel-plugin-transform-modules-systemjs"
},
"version": "7.10.4"
"type": "commonjs",
"version": "7.18.6"
}