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