Split export extensions into 2 different plugins, update stage presets (#6080)

This commit is contained in:
Sangboak Lee
2017-08-19 22:35:40 +09:00
committed by Henry Zhu
parent 6ab3b4c0e3
commit c6a094a9d2
29 changed files with 149 additions and 29 deletions

View File

@@ -0,0 +1,3 @@
src
test
*.log

View File

@@ -0,0 +1,45 @@
# babel-plugin-transform-export-namespace
> Compile export-ns-from statements to ES2015
## Example
```js
export * as ns from 'mod';
```
## Installation
```sh
npm install --save-dev babel-plugin-transform-export-namespace
```
## Usage
### Via `.babelrc` (Recommended)
**.babelrc**
```json
{
"plugins": ["transform-export-namespace"]
}
```
### Via CLI
```sh
babel --plugins transform-export-namespace script.js
```
### Via Node API
```javascript
require("babel-core").transform("code", {
plugins: ["transform-export-namespace"]
});
```
## References
* ~~[Proposal: Additional export-from statements in ES7](https://github.com/leebyron/ecmascript-more-export-from)~~ (Withdrawn)
* [ECMAScript Proposal: export ns from](https://github.com/leebyron/ecmascript-export-ns-from)

View File

@@ -0,0 +1,17 @@
{
"name": "babel-plugin-transform-export-namespace",
"version": "7.0.0-alpha.19",
"description": "Compile export namespace to ES2015",
"repository": "https://github.com/babel/babel/tree/master/packages/babel-plugin-transform-export-namespace",
"license": "MIT",
"main": "lib/index.js",
"keywords": [
"babel-plugin"
],
"dependencies": {
"babel-plugin-syntax-export-extensions": "7.0.0-alpha.19"
},
"devDependencies": {
"babel-helper-plugin-test-runner": "7.0.0-alpha.19"
}
}

View File

@@ -0,0 +1,46 @@
import syntaxExportExtensions from "babel-plugin-syntax-export-extensions";
export default function({ types: t }) {
function build(node, nodes, scope) {
const hasNoExportNs = node.specifiers.every(
specifier => !t.isExportNamespaceSpecifier(specifier),
);
if (hasNoExportNs) return;
const specifier = node.specifiers.shift();
const uid = scope.generateUidIdentifier(specifier.exported.name);
let newSpecifier;
if (t.isExportNamespaceSpecifier(specifier)) {
newSpecifier = t.importNamespaceSpecifier(uid);
} else {
newSpecifier = t.importDefaultSpecifier(uid);
}
nodes.push(t.importDeclaration([newSpecifier], node.source));
nodes.push(
t.exportNamedDeclaration(null, [
t.exportSpecifier(uid, specifier.exported),
]),
);
build(node, nodes, scope);
}
return {
inherits: syntaxExportExtensions,
visitor: {
ExportNamedDeclaration(path) {
const { node, scope } = path;
const nodes = [];
build(node, nodes, scope);
if (!nodes.length) return;
if (node.specifiers.length >= 1) {
nodes.push(node);
}
path.replaceWithMultiple(nodes);
},
},
};
}

View File

@@ -0,0 +1 @@
export v, * as ns from "mod";

View File

@@ -0,0 +1,4 @@
import _v from "mod";
export { _v as v };
import * as _ns from "mod";
export { _ns as ns };

View File

@@ -0,0 +1 @@
export * as default from "foo";

View File

@@ -0,0 +1,2 @@
import * as _default from "foo";
export { _default as default };

View File

@@ -0,0 +1 @@
export * as foo from "bar";

View File

@@ -0,0 +1,2 @@
import * as _foo from "bar";
export { _foo as foo };

View File

@@ -0,0 +1,3 @@
{
"plugins": ["external-helpers", "transform-export-namespace"]
}

View File

@@ -0,0 +1,3 @@
import runner from "babel-helper-plugin-test-runner";
runner(__dirname);