1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.default = splitExportDeclaration;
|
7 |
|
8 | var _t = require("@babel/types");
|
9 |
|
10 | const {
|
11 | cloneNode,
|
12 | exportNamedDeclaration,
|
13 | exportSpecifier,
|
14 | identifier,
|
15 | variableDeclaration,
|
16 | variableDeclarator
|
17 | } = _t;
|
18 |
|
19 | function splitExportDeclaration(exportDeclaration) {
|
20 | if (!exportDeclaration.isExportDeclaration()) {
|
21 | throw new Error("Only export declarations can be split.");
|
22 | }
|
23 |
|
24 | const isDefault = exportDeclaration.isExportDefaultDeclaration();
|
25 | const declaration = exportDeclaration.get("declaration");
|
26 | const isClassDeclaration = declaration.isClassDeclaration();
|
27 |
|
28 | if (isDefault) {
|
29 | const standaloneDeclaration = declaration.isFunctionDeclaration() || isClassDeclaration;
|
30 | const scope = declaration.isScope() ? declaration.scope.parent : declaration.scope;
|
31 | let id = declaration.node.id;
|
32 | let needBindingRegistration = false;
|
33 |
|
34 | if (!id) {
|
35 | needBindingRegistration = true;
|
36 | id = scope.generateUidIdentifier("default");
|
37 |
|
38 | if (standaloneDeclaration || declaration.isFunctionExpression() || declaration.isClassExpression()) {
|
39 | declaration.node.id = cloneNode(id);
|
40 | }
|
41 | }
|
42 |
|
43 | const updatedDeclaration = standaloneDeclaration ? declaration : variableDeclaration("var", [variableDeclarator(cloneNode(id), declaration.node)]);
|
44 | const updatedExportDeclaration = exportNamedDeclaration(null, [exportSpecifier(cloneNode(id), identifier("default"))]);
|
45 | exportDeclaration.insertAfter(updatedExportDeclaration);
|
46 | exportDeclaration.replaceWith(updatedDeclaration);
|
47 |
|
48 | if (needBindingRegistration) {
|
49 | scope.registerDeclaration(exportDeclaration);
|
50 | }
|
51 |
|
52 | return exportDeclaration;
|
53 | }
|
54 |
|
55 | if (exportDeclaration.get("specifiers").length > 0) {
|
56 | throw new Error("It doesn't make sense to split exported specifiers.");
|
57 | }
|
58 |
|
59 | const bindingIdentifiers = declaration.getOuterBindingIdentifiers();
|
60 | const specifiers = Object.keys(bindingIdentifiers).map(name => {
|
61 | return exportSpecifier(identifier(name), identifier(name));
|
62 | });
|
63 | const aliasDeclar = exportNamedDeclaration(null, specifiers);
|
64 | exportDeclaration.insertAfter(aliasDeclar);
|
65 | exportDeclaration.replaceWith(declaration.node);
|
66 | return exportDeclaration;
|
67 | } |
\ | No newline at end of file |