1 | "use strict";
|
---|
2 |
|
---|
3 | Object.defineProperty(exports, "__esModule", {
|
---|
4 | value: true
|
---|
5 | });
|
---|
6 | exports.default = void 0;
|
---|
7 |
|
---|
8 | var _assert = require("assert");
|
---|
9 |
|
---|
10 | var _t = require("@babel/types");
|
---|
11 |
|
---|
12 | const {
|
---|
13 | callExpression,
|
---|
14 | cloneNode,
|
---|
15 | expressionStatement,
|
---|
16 | identifier,
|
---|
17 | importDeclaration,
|
---|
18 | importDefaultSpecifier,
|
---|
19 | importNamespaceSpecifier,
|
---|
20 | importSpecifier,
|
---|
21 | memberExpression,
|
---|
22 | stringLiteral,
|
---|
23 | variableDeclaration,
|
---|
24 | variableDeclarator
|
---|
25 | } = _t;
|
---|
26 |
|
---|
27 | class ImportBuilder {
|
---|
28 | constructor(importedSource, scope, hub) {
|
---|
29 | this._statements = [];
|
---|
30 | this._resultName = null;
|
---|
31 | this._scope = null;
|
---|
32 | this._hub = null;
|
---|
33 | this._importedSource = void 0;
|
---|
34 | this._scope = scope;
|
---|
35 | this._hub = hub;
|
---|
36 | this._importedSource = importedSource;
|
---|
37 | }
|
---|
38 |
|
---|
39 | done() {
|
---|
40 | return {
|
---|
41 | statements: this._statements,
|
---|
42 | resultName: this._resultName
|
---|
43 | };
|
---|
44 | }
|
---|
45 |
|
---|
46 | import() {
|
---|
47 | this._statements.push(importDeclaration([], stringLiteral(this._importedSource)));
|
---|
48 |
|
---|
49 | return this;
|
---|
50 | }
|
---|
51 |
|
---|
52 | require() {
|
---|
53 | this._statements.push(expressionStatement(callExpression(identifier("require"), [stringLiteral(this._importedSource)])));
|
---|
54 |
|
---|
55 | return this;
|
---|
56 | }
|
---|
57 |
|
---|
58 | namespace(name = "namespace") {
|
---|
59 | const local = this._scope.generateUidIdentifier(name);
|
---|
60 |
|
---|
61 | const statement = this._statements[this._statements.length - 1];
|
---|
62 |
|
---|
63 | _assert(statement.type === "ImportDeclaration");
|
---|
64 |
|
---|
65 | _assert(statement.specifiers.length === 0);
|
---|
66 |
|
---|
67 | statement.specifiers = [importNamespaceSpecifier(local)];
|
---|
68 | this._resultName = cloneNode(local);
|
---|
69 | return this;
|
---|
70 | }
|
---|
71 |
|
---|
72 | default(name) {
|
---|
73 | name = this._scope.generateUidIdentifier(name);
|
---|
74 | const statement = this._statements[this._statements.length - 1];
|
---|
75 |
|
---|
76 | _assert(statement.type === "ImportDeclaration");
|
---|
77 |
|
---|
78 | _assert(statement.specifiers.length === 0);
|
---|
79 |
|
---|
80 | statement.specifiers = [importDefaultSpecifier(name)];
|
---|
81 | this._resultName = cloneNode(name);
|
---|
82 | return this;
|
---|
83 | }
|
---|
84 |
|
---|
85 | named(name, importName) {
|
---|
86 | if (importName === "default") return this.default(name);
|
---|
87 | name = this._scope.generateUidIdentifier(name);
|
---|
88 | const statement = this._statements[this._statements.length - 1];
|
---|
89 |
|
---|
90 | _assert(statement.type === "ImportDeclaration");
|
---|
91 |
|
---|
92 | _assert(statement.specifiers.length === 0);
|
---|
93 |
|
---|
94 | statement.specifiers = [importSpecifier(name, identifier(importName))];
|
---|
95 | this._resultName = cloneNode(name);
|
---|
96 | return this;
|
---|
97 | }
|
---|
98 |
|
---|
99 | var(name) {
|
---|
100 | name = this._scope.generateUidIdentifier(name);
|
---|
101 | let statement = this._statements[this._statements.length - 1];
|
---|
102 |
|
---|
103 | if (statement.type !== "ExpressionStatement") {
|
---|
104 | _assert(this._resultName);
|
---|
105 |
|
---|
106 | statement = expressionStatement(this._resultName);
|
---|
107 |
|
---|
108 | this._statements.push(statement);
|
---|
109 | }
|
---|
110 |
|
---|
111 | this._statements[this._statements.length - 1] = variableDeclaration("var", [variableDeclarator(name, statement.expression)]);
|
---|
112 | this._resultName = cloneNode(name);
|
---|
113 | return this;
|
---|
114 | }
|
---|
115 |
|
---|
116 | defaultInterop() {
|
---|
117 | return this._interop(this._hub.addHelper("interopRequireDefault"));
|
---|
118 | }
|
---|
119 |
|
---|
120 | wildcardInterop() {
|
---|
121 | return this._interop(this._hub.addHelper("interopRequireWildcard"));
|
---|
122 | }
|
---|
123 |
|
---|
124 | _interop(callee) {
|
---|
125 | const statement = this._statements[this._statements.length - 1];
|
---|
126 |
|
---|
127 | if (statement.type === "ExpressionStatement") {
|
---|
128 | statement.expression = callExpression(callee, [statement.expression]);
|
---|
129 | } else if (statement.type === "VariableDeclaration") {
|
---|
130 | _assert(statement.declarations.length === 1);
|
---|
131 |
|
---|
132 | statement.declarations[0].init = callExpression(callee, [statement.declarations[0].init]);
|
---|
133 | } else {
|
---|
134 | _assert.fail("Unexpected type.");
|
---|
135 | }
|
---|
136 |
|
---|
137 | return this;
|
---|
138 | }
|
---|
139 |
|
---|
140 | prop(name) {
|
---|
141 | const statement = this._statements[this._statements.length - 1];
|
---|
142 |
|
---|
143 | if (statement.type === "ExpressionStatement") {
|
---|
144 | statement.expression = memberExpression(statement.expression, identifier(name));
|
---|
145 | } else if (statement.type === "VariableDeclaration") {
|
---|
146 | _assert(statement.declarations.length === 1);
|
---|
147 |
|
---|
148 | statement.declarations[0].init = memberExpression(statement.declarations[0].init, identifier(name));
|
---|
149 | } else {
|
---|
150 | _assert.fail("Unexpected type:" + statement.type);
|
---|
151 | }
|
---|
152 |
|
---|
153 | return this;
|
---|
154 | }
|
---|
155 |
|
---|
156 | read(name) {
|
---|
157 | this._resultName = memberExpression(this._resultName, identifier(name));
|
---|
158 | }
|
---|
159 |
|
---|
160 | }
|
---|
161 |
|
---|
162 | exports.default = ImportBuilder; |
---|