source: frontend/node_modules/@babel/plugin-transform-modules-systemjs/lib/index.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 12 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 18.5 KB
Line 
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = void 0;
7exports.getExportSpecifierName = getExportSpecifierName;
8var _helperPluginUtils = require("@babel/helper-plugin-utils");
9var _core = require("@babel/core");
10var _helperModuleTransforms = require("@babel/helper-module-transforms");
11var _helperValidatorIdentifier = require("@babel/helper-validator-identifier");
12const buildTemplate = _core.template.statement(`
13 SYSTEM_REGISTER(MODULE_NAME, SOURCES, function (EXPORT_IDENTIFIER, CONTEXT_IDENTIFIER) {
14 "use strict";
15 BEFORE_BODY;
16 return {
17 setters: SETTERS,
18 execute: EXECUTE,
19 };
20 });
21`);
22const buildExportAll = _core.template.statement(`
23 for (var KEY in TARGET) {
24 if (KEY !== "default" && KEY !== "__esModule") EXPORT_OBJ[KEY] = TARGET[KEY];
25 }
26`);
27const MISSING_PLUGIN_WARNING = `\
28WARNING: Dynamic import() transformation must be enabled using the
29 @babel/plugin-transform-dynamic-import plugin. Babel 8 will
30 no longer transform import() without using that plugin.
31`;
32const MISSING_PLUGIN_ERROR = `\
33ERROR: Dynamic import() transformation must be enabled using the
34 @babel/plugin-transform-dynamic-import plugin. Babel 8
35 no longer transforms import() without using that plugin.
36`;
37function getExportSpecifierName(node, stringSpecifiers) {
38 if (node.type === "Identifier") {
39 return node.name;
40 } else if (node.type === "StringLiteral") {
41 const stringValue = node.value;
42 if (!(0, _helperValidatorIdentifier.isIdentifierName)(stringValue)) {
43 stringSpecifiers.add(stringValue);
44 }
45 return stringValue;
46 } else {
47 throw new Error(`Expected export specifier to be either Identifier or StringLiteral, got ${node.type}`);
48 }
49}
50function constructExportCall(path, exportIdent, exportNames, exportValues, exportStarTarget, stringSpecifiers) {
51 const statements = [];
52 if (!exportStarTarget) {
53 if (exportNames.length === 1) {
54 statements.push(_core.types.expressionStatement(_core.types.callExpression(exportIdent, [_core.types.stringLiteral(exportNames[0]), exportValues[0]])));
55 } else {
56 const objectProperties = [];
57 for (let i = 0; i < exportNames.length; i++) {
58 const exportName = exportNames[i];
59 const exportNameNode = stringSpecifiers.has(exportName) ? _core.types.stringLiteral(exportName) : _core.types.identifier(exportName);
60 const exportValue = exportValues[i];
61 objectProperties.push(_core.types.objectProperty(exportNameNode, exportValue));
62 }
63 statements.push(_core.types.expressionStatement(_core.types.callExpression(exportIdent, [_core.types.objectExpression(objectProperties)])));
64 }
65 } else {
66 const exportObj = path.scope.generateUid("exportObj");
67 statements.push(_core.types.variableDeclaration("var", [_core.types.variableDeclarator(_core.types.identifier(exportObj), _core.types.objectExpression([]))]));
68 statements.push(buildExportAll({
69 KEY: path.scope.generateUidIdentifier("key"),
70 EXPORT_OBJ: _core.types.identifier(exportObj),
71 TARGET: exportStarTarget
72 }));
73 for (let i = 0; i < exportNames.length; i++) {
74 const exportName = exportNames[i];
75 const computed = stringSpecifiers.has(exportName);
76 const exportNameNode = computed ? _core.types.stringLiteral(exportName) : _core.types.identifier(exportName);
77 const exportValue = exportValues[i];
78 statements.push(_core.types.expressionStatement(_core.types.assignmentExpression("=", _core.types.memberExpression(_core.types.identifier(exportObj), exportNameNode, computed), exportValue)));
79 }
80 statements.push(_core.types.expressionStatement(_core.types.callExpression(exportIdent, [_core.types.identifier(exportObj)])));
81 }
82 return statements;
83}
84var _default = exports.default = (0, _helperPluginUtils.declare)((api, options) => {
85 api.assertVersion(7);
86 const {
87 systemGlobal = "System",
88 allowTopLevelThis = false
89 } = options;
90 const reassignmentVisited = new WeakSet();
91 const reassignmentVisitor = {
92 "AssignmentExpression|UpdateExpression"(path) {
93 if (reassignmentVisited.has(path.node)) return;
94 reassignmentVisited.add(path.node);
95 const arg = path.isAssignmentExpression() ? path.get("left") : path.get("argument");
96 if (arg.isObjectPattern() || arg.isArrayPattern()) {
97 const exprs = [path.node];
98 for (const name of Object.keys(arg.getBindingIdentifiers())) {
99 if (this.scope.getBinding(name) !== path.scope.getBinding(name)) {
100 return;
101 }
102 const exportedNames = this.exports[name];
103 if (!exportedNames) continue;
104 for (const exportedName of exportedNames) {
105 exprs.push(this.buildCall(exportedName, _core.types.identifier(name)).expression);
106 }
107 }
108 path.replaceWith(_core.types.sequenceExpression(exprs));
109 return;
110 }
111 if (!arg.isIdentifier()) return;
112 const name = arg.node.name;
113 if (this.scope.getBinding(name) !== path.scope.getBinding(name)) return;
114 const exportedNames = this.exports[name];
115 if (!exportedNames) return;
116 let node = path.node;
117 const isPostUpdateExpression = _core.types.isUpdateExpression(node, {
118 prefix: false
119 });
120 if (isPostUpdateExpression) {
121 node = _core.types.binaryExpression(node.operator[0], _core.types.unaryExpression("+", _core.types.cloneNode(node.argument)), _core.types.numericLiteral(1));
122 }
123 for (const exportedName of exportedNames) {
124 node = this.buildCall(exportedName, node).expression;
125 }
126 if (isPostUpdateExpression) {
127 node = _core.types.sequenceExpression([node, path.node]);
128 }
129 path.replaceWith(node);
130 }
131 };
132 return {
133 name: "transform-modules-systemjs",
134 pre() {
135 this.file.set("@babel/plugin-transform-modules-*", "systemjs");
136 },
137 visitor: {
138 ["CallExpression" + (api.types.importExpression ? "|ImportExpression" : "")](path, state) {
139 if (path.isCallExpression() && !_core.types.isImport(path.node.callee)) return;
140 if (path.isCallExpression()) {
141 if (!this.file.has("@babel/plugin-proposal-dynamic-import")) {
142 console.warn(MISSING_PLUGIN_WARNING);
143 }
144 } else {
145 if (!this.file.has("@babel/plugin-proposal-dynamic-import")) {
146 throw new Error(MISSING_PLUGIN_ERROR);
147 }
148 }
149 path.replaceWith((0, _helperModuleTransforms.buildDynamicImport)(path.node, false, true, specifier => _core.types.callExpression(_core.types.memberExpression(_core.types.identifier(state.contextIdent), _core.types.identifier("import")), [specifier])));
150 },
151 MetaProperty(path, state) {
152 if (path.node.meta.name === "import" && path.node.property.name === "meta") {
153 path.replaceWith(_core.types.memberExpression(_core.types.identifier(state.contextIdent), _core.types.identifier("meta")));
154 }
155 },
156 ReferencedIdentifier(path, state) {
157 if (path.node.name === "__moduleName" && !path.scope.hasBinding("__moduleName")) {
158 path.replaceWith(_core.types.memberExpression(_core.types.identifier(state.contextIdent), _core.types.identifier("id")));
159 }
160 },
161 Program: {
162 enter(path, state) {
163 state.contextIdent = path.scope.generateUid("context");
164 state.stringSpecifiers = new Set();
165 if (!allowTopLevelThis) {
166 (0, _helperModuleTransforms.rewriteThis)(path);
167 }
168 },
169 exit(path, state) {
170 var _path$scope, _path$scope$hoistVari;
171 const scope = path.scope;
172 const exportIdent = scope.generateUid("export");
173 const {
174 contextIdent,
175 stringSpecifiers
176 } = state;
177 const exportMap = Object.create(null);
178 const modules = [];
179 const beforeBody = [];
180 const setters = [];
181 const sources = [];
182 const variableIds = [];
183 const removedPaths = [];
184 function addExportName(key, val) {
185 exportMap[key] = exportMap[key] || [];
186 exportMap[key].push(val);
187 }
188 function pushModule(source, key, specifiers) {
189 let module;
190 modules.forEach(function (m) {
191 if (m.key === source) {
192 module = m;
193 }
194 });
195 if (!module) {
196 modules.push(module = {
197 key: source,
198 imports: [],
199 exports: []
200 });
201 }
202 module[key] = module[key].concat(specifiers);
203 }
204 function buildExportCall(name, val) {
205 return _core.types.expressionStatement(_core.types.callExpression(_core.types.identifier(exportIdent), [_core.types.stringLiteral(name), val]));
206 }
207 const exportNames = [];
208 const exportValues = [];
209 const body = path.get("body");
210 for (const path of body) {
211 if (path.isFunctionDeclaration()) {
212 beforeBody.push(path.node);
213 removedPaths.push(path);
214 } else if (path.isClassDeclaration()) {
215 variableIds.push(_core.types.cloneNode(path.node.id));
216 path.replaceWith(_core.types.expressionStatement(_core.types.assignmentExpression("=", _core.types.cloneNode(path.node.id), _core.types.toExpression(path.node))));
217 } else if (path.isVariableDeclaration()) {
218 path.node.kind = "var";
219 } else if (path.isImportDeclaration()) {
220 const source = path.node.source.value;
221 pushModule(source, "imports", path.node.specifiers);
222 for (const name of Object.keys(path.getBindingIdentifiers())) {
223 scope.removeBinding(name);
224 variableIds.push(_core.types.identifier(name));
225 }
226 path.remove();
227 } else if (path.isExportAllDeclaration()) {
228 pushModule(path.node.source.value, "exports", path.node);
229 path.remove();
230 } else if (path.isExportDefaultDeclaration()) {
231 const declar = path.node.declaration;
232 if (_core.types.isClassDeclaration(declar)) {
233 const id = declar.id;
234 if (id) {
235 exportNames.push("default");
236 exportValues.push(scope.buildUndefinedNode());
237 variableIds.push(_core.types.cloneNode(id));
238 addExportName(id.name, "default");
239 path.replaceWith(_core.types.expressionStatement(_core.types.assignmentExpression("=", _core.types.cloneNode(id), _core.types.toExpression(declar))));
240 } else {
241 exportNames.push("default");
242 exportValues.push(_core.types.toExpression(declar));
243 removedPaths.push(path);
244 }
245 } else if (_core.types.isFunctionDeclaration(declar)) {
246 const id = declar.id;
247 if (id) {
248 beforeBody.push(declar);
249 exportNames.push("default");
250 exportValues.push(_core.types.cloneNode(id));
251 addExportName(id.name, "default");
252 } else {
253 exportNames.push("default");
254 exportValues.push(_core.types.toExpression(declar));
255 }
256 removedPaths.push(path);
257 } else {
258 path.replaceWith(buildExportCall("default", declar));
259 }
260 } else if (path.isExportNamedDeclaration()) {
261 const declar = path.node.declaration;
262 if (declar) {
263 path.replaceWith(declar);
264 if (_core.types.isFunction(declar)) {
265 const name = declar.id.name;
266 addExportName(name, name);
267 beforeBody.push(declar);
268 exportNames.push(name);
269 exportValues.push(_core.types.cloneNode(declar.id));
270 removedPaths.push(path);
271 } else if (_core.types.isClass(declar)) {
272 const name = declar.id.name;
273 exportNames.push(name);
274 exportValues.push(scope.buildUndefinedNode());
275 variableIds.push(_core.types.cloneNode(declar.id));
276 path.replaceWith(_core.types.expressionStatement(_core.types.assignmentExpression("=", _core.types.cloneNode(declar.id), _core.types.toExpression(declar))));
277 addExportName(name, name);
278 } else {
279 if (_core.types.isVariableDeclaration(declar)) {
280 declar.kind = "var";
281 }
282 for (const name of Object.keys(_core.types.getBindingIdentifiers(declar))) {
283 addExportName(name, name);
284 }
285 }
286 } else {
287 const specifiers = path.node.specifiers;
288 if (specifiers != null && specifiers.length) {
289 if (path.node.source) {
290 pushModule(path.node.source.value, "exports", specifiers);
291 path.remove();
292 } else {
293 const nodes = [];
294 for (const specifier of specifiers) {
295 const {
296 local,
297 exported
298 } = specifier;
299 const binding = scope.getBinding(local.name);
300 const exportedName = getExportSpecifierName(exported, stringSpecifiers);
301 if (binding && _core.types.isFunctionDeclaration(binding.path.node)) {
302 exportNames.push(exportedName);
303 exportValues.push(_core.types.cloneNode(local));
304 } else if (!binding) {
305 nodes.push(buildExportCall(exportedName, local));
306 }
307 addExportName(local.name, exportedName);
308 }
309 path.replaceWithMultiple(nodes);
310 }
311 } else {
312 path.remove();
313 }
314 }
315 }
316 }
317 modules.forEach(function (specifiers) {
318 const setterBody = [];
319 const target = scope.generateUid(specifiers.key);
320 for (let specifier of specifiers.imports) {
321 if (_core.types.isImportNamespaceSpecifier(specifier)) {
322 setterBody.push(_core.types.expressionStatement(_core.types.assignmentExpression("=", specifier.local, _core.types.identifier(target))));
323 } else if (_core.types.isImportDefaultSpecifier(specifier)) {
324 specifier = _core.types.importSpecifier(specifier.local, _core.types.identifier("default"));
325 }
326 if (_core.types.isImportSpecifier(specifier)) {
327 const {
328 imported
329 } = specifier;
330 setterBody.push(_core.types.expressionStatement(_core.types.assignmentExpression("=", specifier.local, _core.types.memberExpression(_core.types.identifier(target), specifier.imported, imported.type === "StringLiteral"))));
331 }
332 }
333 if (specifiers.exports.length) {
334 const exportNames = [];
335 const exportValues = [];
336 let hasExportStar = false;
337 for (const node of specifiers.exports) {
338 if (_core.types.isExportAllDeclaration(node)) {
339 hasExportStar = true;
340 } else if (_core.types.isExportSpecifier(node)) {
341 const exportedName = getExportSpecifierName(node.exported, stringSpecifiers);
342 exportNames.push(exportedName);
343 exportValues.push(_core.types.memberExpression(_core.types.identifier(target), node.local, _core.types.isStringLiteral(node.local)));
344 } else {}
345 }
346 setterBody.push(...constructExportCall(path, _core.types.identifier(exportIdent), exportNames, exportValues, hasExportStar ? _core.types.identifier(target) : null, stringSpecifiers));
347 }
348 sources.push(_core.types.stringLiteral(specifiers.key));
349 setters.push(_core.types.functionExpression(null, [_core.types.identifier(target)], _core.types.blockStatement(setterBody)));
350 });
351 let moduleName = (0, _helperModuleTransforms.getModuleName)(this.file.opts, options);
352 if (moduleName) moduleName = _core.types.stringLiteral(moduleName);
353 (_path$scope$hoistVari = (_path$scope = path.scope).hoistVariables) != null ? _path$scope$hoistVari : _path$scope.hoistVariables = require("@babel/traverse").Scope.prototype.hoistVariables;
354 path.scope.hoistVariables((id, hasInit) => {
355 variableIds.push(id);
356 if (!hasInit && id.name in exportMap) {
357 for (const exported of exportMap[id.name]) {
358 exportNames.push(exported);
359 exportValues.push(_core.types.buildUndefinedNode());
360 }
361 }
362 });
363 if (variableIds.length) {
364 beforeBody.unshift(_core.types.variableDeclaration("var", variableIds.map(id => _core.types.variableDeclarator(id))));
365 }
366 if (exportNames.length) {
367 beforeBody.push(...constructExportCall(path, _core.types.identifier(exportIdent), exportNames, exportValues, null, stringSpecifiers));
368 }
369 path.traverse(reassignmentVisitor, {
370 exports: exportMap,
371 buildCall: buildExportCall,
372 scope
373 });
374 for (const path of removedPaths) {
375 path.remove();
376 }
377 let hasTLA = false;
378 path.traverse({
379 AwaitExpression(path) {
380 hasTLA = true;
381 path.stop();
382 },
383 Function(path) {
384 path.skip();
385 },
386 noScope: true
387 });
388 path.node.body = [buildTemplate({
389 SYSTEM_REGISTER: _core.types.memberExpression(_core.types.identifier(systemGlobal), _core.types.identifier("register")),
390 BEFORE_BODY: beforeBody,
391 MODULE_NAME: moduleName,
392 SETTERS: _core.types.arrayExpression(setters),
393 EXECUTE: _core.types.functionExpression(null, [], _core.types.blockStatement(path.node.body), false, hasTLA),
394 SOURCES: _core.types.arrayExpression(sources),
395 EXPORT_IDENTIFIER: _core.types.identifier(exportIdent),
396 CONTEXT_IDENTIFIER: _core.types.identifier(contextIdent)
397 })];
398 path.requeue(path.get("body.0"));
399 }
400 }
401 }
402 };
403});
404
405//# sourceMappingURL=index.js.map
Note: See TracBrowser for help on using the repository browser.