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

View File

@@ -0,0 +1,17 @@
{
"name": "babel-plugin-transform-export-default",
"version": "7.0.0-alpha.19",
"description": "Compile export default to ES2015",
"repository": "https://github.com/babel/babel/tree/master/packages/babel-plugin-transform-export-default",
"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 hasNoExportDefault = node.specifiers.every(
specifier => !t.isExportDefaultSpecifier(specifier),
);
if (hasNoExportDefault) 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, { x, y as w } from "mod";

View File

@@ -0,0 +1,3 @@
import _v from "mod";
export { _v as v };
export { x, y as w } from "mod";

View File

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

View File

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

View File

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

View File

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