@babel/plugin-transform-modules-systemjs
Advanced tools
Comparing version 7.0.0-beta.49 to 7.0.0-beta.50
152
lib/index.js
@@ -57,2 +57,39 @@ "use strict"; | ||
`); | ||
function constructExportCall(path, exportIdent, exportNames, exportValues, exportStarTarget) { | ||
const statements = []; | ||
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)); | ||
} | ||
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([]))])); | ||
statements.push(buildExportAll({ | ||
KEY: path.scope.generateUidIdentifier("key"), | ||
EXPORT_OBJ: _core().types.identifier(exportObj), | ||
TARGET: exportStarTarget | ||
})); | ||
for (let i = 0; i < exportNames.length; i++) { | ||
const exportName = exportNames[i]; | ||
const exportValue = exportValues[i]; | ||
statements.push(_core().types.expressionStatement(_core().types.assignmentExpression("=", _core().types.memberExpression(_core().types.identifier(exportObj), _core().types.identifier(exportName)), exportValue))); | ||
} | ||
statements.push(_core().types.expressionStatement(_core().types.callExpression(exportIdent, [_core().types.identifier(exportObj)]))); | ||
} | ||
return statements; | ||
} | ||
const TYPE_IMPORT = "Import"; | ||
@@ -71,2 +108,23 @@ | ||
const arg = path.get(path.isAssignmentExpression() ? "left" : "argument"); | ||
if (arg.isObjectPattern() || arg.isArrayPattern()) { | ||
const exprs = [path.node]; | ||
for (const name in arg.getBindingIdentifiers()) { | ||
if (this.scope.getBinding(name) !== path.scope.getBinding(name)) { | ||
return; | ||
} | ||
const exportedNames = this.exports[name]; | ||
if (!exportedNames) return; | ||
for (const exportedName of exportedNames) { | ||
exprs.push(this.buildCall(exportedName, _core().types.identifier(name)).expression); | ||
} | ||
} | ||
path.replaceWith(_core().types.sequenceExpression(exprs)); | ||
return; | ||
} | ||
if (!arg.isIdentifier()) return; | ||
@@ -106,2 +164,8 @@ const name = arg.node.name; | ||
MetaProperty(path, state) { | ||
if (path.node.meta.name === "import" && path.node.property.name === "meta") { | ||
path.replaceWith(_core().types.memberExpression(_core().types.identifier(state.contextIdent), _core().types.identifier("meta"))); | ||
} | ||
}, | ||
ReferencedIdentifier(path, state) { | ||
@@ -158,15 +222,5 @@ if (path.node.name == "__moduleName" && !path.scope.hasBinding("__moduleName")) { | ||
const body = path.get("body"); | ||
let canHoist = true; | ||
for (let path of body) { | ||
if (path.isExportDeclaration()) path = path.get("declaration"); | ||
if (path.isVariableDeclaration() && path.node.kind !== "var") { | ||
canHoist = false; | ||
break; | ||
} | ||
} | ||
for (const path of body) { | ||
if (canHoist && path.isFunctionDeclaration()) { | ||
if (path.isFunctionDeclaration()) { | ||
beforeBody.push(path.node); | ||
@@ -202,3 +256,3 @@ removedPaths.push(path); | ||
if (!canHoist || declar.isClassDeclaration()) { | ||
if (declar.isClassDeclaration()) { | ||
path.replaceWithMultiple(nodes); | ||
@@ -217,4 +271,2 @@ } else { | ||
path.replaceWith(declar); | ||
const nodes = []; | ||
let bindingIdentifiers; | ||
@@ -224,23 +276,15 @@ if (path.isFunction()) { | ||
const name = node.id.name; | ||
if (canHoist) { | ||
addExportName(name, name); | ||
beforeBody.push(node); | ||
beforeBody.push(buildExportCall(name, _core().types.cloneNode(node.id))); | ||
removedPaths.push(path); | ||
} else if (path.isClass()) { | ||
const name = declar.node.id.name; | ||
addExportName(name, name); | ||
path.insertAfter([buildExportCall(name, _core().types.identifier(name))]); | ||
} else { | ||
for (const name in declar.getBindingIdentifiers()) { | ||
addExportName(name, name); | ||
beforeBody.push(node); | ||
beforeBody.push(buildExportCall(name, _core().types.cloneNode(node.id))); | ||
removedPaths.push(path); | ||
} else { | ||
bindingIdentifiers = { | ||
[name]: node.id | ||
}; | ||
} | ||
} else { | ||
bindingIdentifiers = declar.getBindingIdentifiers(); | ||
} | ||
for (const name in bindingIdentifiers) { | ||
addExportName(name, name); | ||
nodes.push(buildExportCall(name, _core().types.identifier(name))); | ||
} | ||
path.insertAfter(nodes); | ||
} else { | ||
@@ -257,3 +301,6 @@ const specifiers = path.node.specifiers; | ||
for (const specifier of specifiers) { | ||
nodes.push(buildExportCall(specifier.exported.name, specifier.local)); | ||
if (!path.scope.getBinding(specifier.local.name)) { | ||
nodes.push(buildExportCall(specifier.exported.name, specifier.local)); | ||
} | ||
addExportName(specifier.local.name, specifier.exported.name); | ||
@@ -270,3 +317,3 @@ } | ||
modules.forEach(function (specifiers) { | ||
const setterBody = []; | ||
let setterBody = []; | ||
const target = path.scope.generateUid(specifiers.key); | ||
@@ -287,18 +334,16 @@ | ||
if (specifiers.exports.length) { | ||
const exportObj = path.scope.generateUid("exportObj"); | ||
setterBody.push(_core().types.variableDeclaration("var", [_core().types.variableDeclarator(_core().types.identifier(exportObj), _core().types.objectExpression([]))])); | ||
const exportNames = []; | ||
const exportValues = []; | ||
let hasExportStar = false; | ||
for (const node of specifiers.exports) { | ||
if (_core().types.isExportAllDeclaration(node)) { | ||
setterBody.push(buildExportAll({ | ||
KEY: path.scope.generateUidIdentifier("key"), | ||
EXPORT_OBJ: _core().types.identifier(exportObj), | ||
TARGET: _core().types.identifier(target) | ||
})); | ||
hasExportStar = true; | ||
} else if (_core().types.isExportSpecifier(node)) { | ||
setterBody.push(_core().types.expressionStatement(_core().types.assignmentExpression("=", _core().types.memberExpression(_core().types.identifier(exportObj), node.exported), _core().types.memberExpression(_core().types.identifier(target), node.local)))); | ||
exportNames.push(node.exported.name); | ||
exportValues.push(_core().types.memberExpression(_core().types.identifier(target), node.local)); | ||
} else {} | ||
} | ||
setterBody.push(_core().types.expressionStatement(_core().types.callExpression(_core().types.identifier(exportIdent), [_core().types.identifier(exportObj)]))); | ||
setterBody = setterBody.concat(constructExportCall(path, _core().types.identifier(exportIdent), exportNames, exportValues, hasExportStar ? _core().types.identifier(target) : null)); | ||
} | ||
@@ -311,6 +356,10 @@ | ||
if (moduleName) moduleName = _core().types.stringLiteral(moduleName); | ||
const uninitializedVars = []; | ||
(0, _helperHoistVariables().default)(path, (id, name, hasInit) => { | ||
variableIds.push(id); | ||
if (canHoist) { | ||
(0, _helperHoistVariables().default)(path, id => variableIds.push(id)); | ||
} | ||
if (!hasInit) { | ||
uninitializedVars.push(name); | ||
} | ||
}, null); | ||
@@ -321,2 +370,13 @@ if (variableIds.length) { | ||
if (uninitializedVars.length) { | ||
const undefinedValues = []; | ||
const undefinedIdent = path.scope.buildUndefinedNode(); | ||
for (let i = 0; i < uninitializedVars.length; i++) { | ||
undefinedValues[i] = undefinedIdent; | ||
} | ||
beforeBody = beforeBody.concat(constructExportCall(path, _core().types.identifier(exportIdent), uninitializedVars, undefinedValues, null)); | ||
} | ||
path.traverse(reassignmentVisitor, { | ||
@@ -323,0 +383,0 @@ exports: exportNames, |
{ | ||
"name": "@babel/plugin-transform-modules-systemjs", | ||
"version": "7.0.0-beta.49", | ||
"version": "7.0.0-beta.50", | ||
"description": "This plugin transforms ES2015 modules to SystemJS", | ||
@@ -9,4 +9,4 @@ "repository": "https://github.com/babel/babel/tree/master/packages/babel-plugin-transform-modules-systemjs", | ||
"dependencies": { | ||
"@babel/helper-hoist-variables": "7.0.0-beta.49", | ||
"@babel/helper-plugin-utils": "7.0.0-beta.49" | ||
"@babel/helper-hoist-variables": "7.0.0-beta.50", | ||
"@babel/helper-plugin-utils": "7.0.0-beta.50" | ||
}, | ||
@@ -20,6 +20,6 @@ "keywords": [ | ||
"devDependencies": { | ||
"@babel/core": "7.0.0-beta.49", | ||
"@babel/helper-plugin-test-runner": "7.0.0-beta.49", | ||
"@babel/plugin-syntax-dynamic-import": "7.0.0-beta.49" | ||
"@babel/core": "7.0.0-beta.50", | ||
"@babel/helper-plugin-test-runner": "7.0.0-beta.50", | ||
"@babel/plugin-syntax-dynamic-import": "7.0.0-beta.50" | ||
} | ||
} |
# @babel/plugin-transform-modules-systemjs | ||
> This plugin transforms ES2015 modules to [SystemJS](https://github.com/systemjs/systemjs). | ||
> This plugin transforms ES2015 modules to SystemJS | ||
## Example | ||
See our website [@babel/plugin-transform-modules-systemjs](https://new.babeljs.io/docs/en/next/babel-plugin-transform-modules-systemjs.html) for more information. | ||
**In** | ||
## Install | ||
```javascript | ||
export default 42; | ||
``` | ||
Using npm: | ||
**Out** | ||
```javascript | ||
System.register([], function (_export, _context) { | ||
return { | ||
setters: [], | ||
execute: function () { | ||
_export("default", 42); | ||
} | ||
}; | ||
}); | ||
``` | ||
For dynamic import support (`import('./lazy.js').then(m => ...)`), enable the [@babel/plugin-syntax-dynamic-import](https://babeljs.io/docs/plugins/syntax-dynamic-import/) plugin before this one. | ||
## Installation | ||
```sh | ||
npm install --save-dev @babel/plugin-transform-modules-systemjs | ||
npm install --save @babel/plugin-transform-modules-systemjs | ||
``` | ||
## Usage | ||
or using yarn: | ||
### Via `.babelrc` (Recommended) | ||
**.babelrc** | ||
Without options: | ||
```json | ||
{ | ||
"plugins": ["@babel/plugin-transform-modules-systemjs"] | ||
} | ||
``` | ||
With options: | ||
```json | ||
{ | ||
"plugins": [ | ||
["@babel/plugin-transform-modules-systemjs", { | ||
// outputs SystemJS.register(...) | ||
"systemGlobal": "SystemJS" | ||
}] | ||
] | ||
} | ||
``` | ||
### Via CLI | ||
```sh | ||
babel --plugins @babel/plugin-transform-modules-systemjs script.js | ||
yarn add --save @babel/plugin-transform-modules-systemjs | ||
``` | ||
### Via Node API | ||
```javascript | ||
require("@babel/core").transform("code", { | ||
plugins: ["@babel/plugin-transform-modules-systemjs"] | ||
}); | ||
``` |
15742
325
3
20
+ Added@babel/helper-hoist-variables@7.0.0-beta.50(transitive)
+ Added@babel/helper-plugin-utils@7.0.0-beta.50(transitive)
- Removed@babel/helper-hoist-variables@7.0.0-beta.49(transitive)
- Removed@babel/helper-plugin-utils@7.0.0-beta.49(transitive)