项目作者: babel-utils

项目描述 :
Serialize a module into an easier format to work with
高级语言: JavaScript
项目地址: git://github.com/babel-utils/babel-explode-module.git
创建时间: 2017-05-16T13:48:29Z
项目社区:https://github.com/babel-utils/babel-explode-module

开源协议:MIT License

下载


babel-explode-module

Serialize a module into an easier format to work with

  1. import {foo, bar} from "mod";
  2. export default function() {
  3. // ...
  4. }
  5. const baz = 42,
  6. bat = class Bat {};
  7. export {
  8. baz,
  9. bat
  10. };

Creating this AST:

  1. Program
  2. body:
  3. - ImportDeclaration
  4. specifiers:
  5. - ImportSpecifier
  6. - ImportSpecifier
  7. - ExportDefaultDeclaration
  8. declaration: FunctionDeclaration
  9. - VariableDeclaration
  10. declarations:
  11. - VariableDeclarator
  12. - VariableDeclarator
  13. - ExportNamedDeclaration
  14. specifiers:
  15. - ExportSpecifier
  16. - ExportSpecifier

Will be exploded to this:

  1. {
  2. imports: [
  3. { kind: "value", local: "foo", external: "foo", source: "mod", loc: {...} },
  4. { kind: "value", local: "bar", external: "bar", source: "mod", loc: {...} },
  5. ],
  6. exports: [
  7. { local: "_default", external: "default", loc: {...} },
  8. { local: "baz", external: "baz", loc: {...} },
  9. { local: "bat", external: "bat", loc: {...} },
  10. },
  11. statements: [
  12. { type: "FunctionDeclaration" },
  13. { type: "VariableDeclaration", declarations: VariableDeclarator },
  14. { type: "VariableDeclaration", declarations: VariableDeclarator },
  15. ],
  16. }

Serializes imports/exports to an easy to work with format

  1. // input
  2. import a, {b} from "mod";
  3. import * as c from "mod";
  4. export default function d() {}
  5. export {e, f as g};
  6. export {default as h} from "mod";
  7. export * from "mod";
  1. // output
  2. {
  3. imports: [
  4. { kind: "value", local: "a", external: "a", source: "mod" },
  5. { kind: "value", local: "b", external: "b", source: "mod" },
  6. { kind: "value", local: "c", source: "d" },
  7. ],
  8. exports: [
  9. { local: "d", external: "d" },
  10. { local: "e", external: "e" },
  11. { local: "f", external: "g" },
  12. { local: "default", external: "g", source: "mod" },
  13. { source: "mod" },
  14. ]
  15. }

Simplifies declarations to create 1 binding per statement (i.e. variables)

  1. // input
  2. function a() {}
  3. var b,
  4. c;
  1. // output (printed)
  2. function a() {}
  3. var b;
  4. var c;

Splits export values away from their exports

  1. // input
  2. export function a() {}
  3. export default function() {}
  1. // output (printed)
  2. function a() {}
  3. var _default = function() {};
  4. export {a};
  5. export default _default;