source: trip-planner-front/node_modules/@babel/helper-module-transforms/lib/index.js

Last change on this file was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 11.8 KB
Line 
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.rewriteModuleStatementsAndPrepareHeader = rewriteModuleStatementsAndPrepareHeader;
7exports.ensureStatementsHoisted = ensureStatementsHoisted;
8exports.wrapInterop = wrapInterop;
9exports.buildNamespaceInitStatements = buildNamespaceInitStatements;
10Object.defineProperty(exports, "isModule", {
11 enumerable: true,
12 get: function () {
13 return _helperModuleImports.isModule;
14 }
15});
16Object.defineProperty(exports, "rewriteThis", {
17 enumerable: true,
18 get: function () {
19 return _rewriteThis.default;
20 }
21});
22Object.defineProperty(exports, "hasExports", {
23 enumerable: true,
24 get: function () {
25 return _normalizeAndLoadMetadata.hasExports;
26 }
27});
28Object.defineProperty(exports, "isSideEffectImport", {
29 enumerable: true,
30 get: function () {
31 return _normalizeAndLoadMetadata.isSideEffectImport;
32 }
33});
34Object.defineProperty(exports, "getModuleName", {
35 enumerable: true,
36 get: function () {
37 return _getModuleName.default;
38 }
39});
40
41var _assert = require("assert");
42
43var _t = require("@babel/types");
44
45var _template = require("@babel/template");
46
47var _helperModuleImports = require("@babel/helper-module-imports");
48
49var _rewriteThis = require("./rewrite-this");
50
51var _rewriteLiveReferences = require("./rewrite-live-references");
52
53var _normalizeAndLoadMetadata = require("./normalize-and-load-metadata");
54
55var _getModuleName = require("./get-module-name");
56
57const {
58 booleanLiteral,
59 callExpression,
60 cloneNode,
61 directive,
62 directiveLiteral,
63 expressionStatement,
64 identifier,
65 isIdentifier,
66 memberExpression,
67 stringLiteral,
68 valueToNode,
69 variableDeclaration,
70 variableDeclarator
71} = _t;
72
73function rewriteModuleStatementsAndPrepareHeader(path, {
74 loose,
75 exportName,
76 strict,
77 allowTopLevelThis,
78 strictMode,
79 noInterop,
80 importInterop = noInterop ? "none" : "babel",
81 lazy,
82 esNamespaceOnly,
83 constantReexports = loose,
84 enumerableModuleMeta = loose,
85 noIncompleteNsImportDetection
86}) {
87 (0, _normalizeAndLoadMetadata.validateImportInteropOption)(importInterop);
88
89 _assert((0, _helperModuleImports.isModule)(path), "Cannot process module statements in a script");
90
91 path.node.sourceType = "script";
92 const meta = (0, _normalizeAndLoadMetadata.default)(path, exportName, {
93 importInterop,
94 initializeReexports: constantReexports,
95 lazy,
96 esNamespaceOnly
97 });
98
99 if (!allowTopLevelThis) {
100 (0, _rewriteThis.default)(path);
101 }
102
103 (0, _rewriteLiveReferences.default)(path, meta);
104
105 if (strictMode !== false) {
106 const hasStrict = path.node.directives.some(directive => {
107 return directive.value.value === "use strict";
108 });
109
110 if (!hasStrict) {
111 path.unshiftContainer("directives", directive(directiveLiteral("use strict")));
112 }
113 }
114
115 const headers = [];
116
117 if ((0, _normalizeAndLoadMetadata.hasExports)(meta) && !strict) {
118 headers.push(buildESModuleHeader(meta, enumerableModuleMeta));
119 }
120
121 const nameList = buildExportNameListDeclaration(path, meta);
122
123 if (nameList) {
124 meta.exportNameListName = nameList.name;
125 headers.push(nameList.statement);
126 }
127
128 headers.push(...buildExportInitializationStatements(path, meta, constantReexports, noIncompleteNsImportDetection));
129 return {
130 meta,
131 headers
132 };
133}
134
135function ensureStatementsHoisted(statements) {
136 statements.forEach(header => {
137 header._blockHoist = 3;
138 });
139}
140
141function wrapInterop(programPath, expr, type) {
142 if (type === "none") {
143 return null;
144 }
145
146 if (type === "node-namespace") {
147 return callExpression(programPath.hub.addHelper("interopRequireWildcard"), [expr, booleanLiteral(true)]);
148 } else if (type === "node-default") {
149 return null;
150 }
151
152 let helper;
153
154 if (type === "default") {
155 helper = "interopRequireDefault";
156 } else if (type === "namespace") {
157 helper = "interopRequireWildcard";
158 } else {
159 throw new Error(`Unknown interop: ${type}`);
160 }
161
162 return callExpression(programPath.hub.addHelper(helper), [expr]);
163}
164
165function buildNamespaceInitStatements(metadata, sourceMetadata, constantReexports = false) {
166 const statements = [];
167 let srcNamespace = identifier(sourceMetadata.name);
168 if (sourceMetadata.lazy) srcNamespace = callExpression(srcNamespace, []);
169
170 for (const localName of sourceMetadata.importsNamespace) {
171 if (localName === sourceMetadata.name) continue;
172 statements.push(_template.default.statement`var NAME = SOURCE;`({
173 NAME: localName,
174 SOURCE: cloneNode(srcNamespace)
175 }));
176 }
177
178 if (constantReexports) {
179 statements.push(...buildReexportsFromMeta(metadata, sourceMetadata, true));
180 }
181
182 for (const exportName of sourceMetadata.reexportNamespace) {
183 statements.push((sourceMetadata.lazy ? _template.default.statement`
184 Object.defineProperty(EXPORTS, "NAME", {
185 enumerable: true,
186 get: function() {
187 return NAMESPACE;
188 }
189 });
190 ` : _template.default.statement`EXPORTS.NAME = NAMESPACE;`)({
191 EXPORTS: metadata.exportName,
192 NAME: exportName,
193 NAMESPACE: cloneNode(srcNamespace)
194 }));
195 }
196
197 if (sourceMetadata.reexportAll) {
198 const statement = buildNamespaceReexport(metadata, cloneNode(srcNamespace), constantReexports);
199 statement.loc = sourceMetadata.reexportAll.loc;
200 statements.push(statement);
201 }
202
203 return statements;
204}
205
206const ReexportTemplate = {
207 constant: _template.default.statement`EXPORTS.EXPORT_NAME = NAMESPACE_IMPORT;`,
208 constantComputed: _template.default.statement`EXPORTS["EXPORT_NAME"] = NAMESPACE_IMPORT;`,
209 spec: _template.default.statement`
210 Object.defineProperty(EXPORTS, "EXPORT_NAME", {
211 enumerable: true,
212 get: function() {
213 return NAMESPACE_IMPORT;
214 },
215 });
216 `
217};
218
219const buildReexportsFromMeta = (meta, metadata, constantReexports) => {
220 const namespace = metadata.lazy ? callExpression(identifier(metadata.name), []) : identifier(metadata.name);
221 const {
222 stringSpecifiers
223 } = meta;
224 return Array.from(metadata.reexports, ([exportName, importName]) => {
225 let NAMESPACE_IMPORT = cloneNode(namespace);
226
227 if (importName === "default" && metadata.interop === "node-default") {} else if (stringSpecifiers.has(importName)) {
228 NAMESPACE_IMPORT = memberExpression(NAMESPACE_IMPORT, stringLiteral(importName), true);
229 } else {
230 NAMESPACE_IMPORT = memberExpression(NAMESPACE_IMPORT, identifier(importName));
231 }
232
233 const astNodes = {
234 EXPORTS: meta.exportName,
235 EXPORT_NAME: exportName,
236 NAMESPACE_IMPORT
237 };
238
239 if (constantReexports || isIdentifier(NAMESPACE_IMPORT)) {
240 if (stringSpecifiers.has(exportName)) {
241 return ReexportTemplate.constantComputed(astNodes);
242 } else {
243 return ReexportTemplate.constant(astNodes);
244 }
245 } else {
246 return ReexportTemplate.spec(astNodes);
247 }
248 });
249};
250
251function buildESModuleHeader(metadata, enumerableModuleMeta = false) {
252 return (enumerableModuleMeta ? _template.default.statement`
253 EXPORTS.__esModule = true;
254 ` : _template.default.statement`
255 Object.defineProperty(EXPORTS, "__esModule", {
256 value: true,
257 });
258 `)({
259 EXPORTS: metadata.exportName
260 });
261}
262
263function buildNamespaceReexport(metadata, namespace, constantReexports) {
264 return (constantReexports ? _template.default.statement`
265 Object.keys(NAMESPACE).forEach(function(key) {
266 if (key === "default" || key === "__esModule") return;
267 VERIFY_NAME_LIST;
268 if (key in EXPORTS && EXPORTS[key] === NAMESPACE[key]) return;
269
270 EXPORTS[key] = NAMESPACE[key];
271 });
272 ` : _template.default.statement`
273 Object.keys(NAMESPACE).forEach(function(key) {
274 if (key === "default" || key === "__esModule") return;
275 VERIFY_NAME_LIST;
276 if (key in EXPORTS && EXPORTS[key] === NAMESPACE[key]) return;
277
278 Object.defineProperty(EXPORTS, key, {
279 enumerable: true,
280 get: function() {
281 return NAMESPACE[key];
282 },
283 });
284 });
285 `)({
286 NAMESPACE: namespace,
287 EXPORTS: metadata.exportName,
288 VERIFY_NAME_LIST: metadata.exportNameListName ? (0, _template.default)`
289 if (Object.prototype.hasOwnProperty.call(EXPORTS_LIST, key)) return;
290 `({
291 EXPORTS_LIST: metadata.exportNameListName
292 }) : null
293 });
294}
295
296function buildExportNameListDeclaration(programPath, metadata) {
297 const exportedVars = Object.create(null);
298
299 for (const data of metadata.local.values()) {
300 for (const name of data.names) {
301 exportedVars[name] = true;
302 }
303 }
304
305 let hasReexport = false;
306
307 for (const data of metadata.source.values()) {
308 for (const exportName of data.reexports.keys()) {
309 exportedVars[exportName] = true;
310 }
311
312 for (const exportName of data.reexportNamespace) {
313 exportedVars[exportName] = true;
314 }
315
316 hasReexport = hasReexport || !!data.reexportAll;
317 }
318
319 if (!hasReexport || Object.keys(exportedVars).length === 0) return null;
320 const name = programPath.scope.generateUidIdentifier("exportNames");
321 delete exportedVars.default;
322 return {
323 name: name.name,
324 statement: variableDeclaration("var", [variableDeclarator(name, valueToNode(exportedVars))])
325 };
326}
327
328function buildExportInitializationStatements(programPath, metadata, constantReexports = false, noIncompleteNsImportDetection = false) {
329 const initStatements = [];
330
331 for (const [localName, data] of metadata.local) {
332 if (data.kind === "import") {} else if (data.kind === "hoisted") {
333 initStatements.push([data.names[0], buildInitStatement(metadata, data.names, identifier(localName))]);
334 } else if (!noIncompleteNsImportDetection) {
335 for (const exportName of data.names) {
336 initStatements.push([exportName, null]);
337 }
338 }
339 }
340
341 for (const data of metadata.source.values()) {
342 if (!constantReexports) {
343 const reexportsStatements = buildReexportsFromMeta(metadata, data, false);
344 const reexports = [...data.reexports.keys()];
345
346 for (let i = 0; i < reexportsStatements.length; i++) {
347 initStatements.push([reexports[i], reexportsStatements[i]]);
348 }
349 }
350
351 if (!noIncompleteNsImportDetection) {
352 for (const exportName of data.reexportNamespace) {
353 initStatements.push([exportName, null]);
354 }
355 }
356 }
357
358 initStatements.sort((a, b) => a[0] > b[0] ? 1 : -1);
359 const results = [];
360
361 if (noIncompleteNsImportDetection) {
362 for (const [, initStatement] of initStatements) {
363 results.push(initStatement);
364 }
365 } else {
366 const chunkSize = 100;
367
368 for (let i = 0, uninitializedExportNames = []; i < initStatements.length; i += chunkSize) {
369 for (let j = 0; j < chunkSize && i + j < initStatements.length; j++) {
370 const [exportName, initStatement] = initStatements[i + j];
371
372 if (initStatement !== null) {
373 if (uninitializedExportNames.length > 0) {
374 results.push(buildInitStatement(metadata, uninitializedExportNames, programPath.scope.buildUndefinedNode()));
375 uninitializedExportNames = [];
376 }
377
378 results.push(initStatement);
379 } else {
380 uninitializedExportNames.push(exportName);
381 }
382 }
383
384 if (uninitializedExportNames.length > 0) {
385 results.push(buildInitStatement(metadata, uninitializedExportNames, programPath.scope.buildUndefinedNode()));
386 }
387 }
388 }
389
390 return results;
391}
392
393const InitTemplate = {
394 computed: _template.default.expression`EXPORTS["NAME"] = VALUE`,
395 default: _template.default.expression`EXPORTS.NAME = VALUE`
396};
397
398function buildInitStatement(metadata, exportNames, initExpr) {
399 const {
400 stringSpecifiers,
401 exportName: EXPORTS
402 } = metadata;
403 return expressionStatement(exportNames.reduce((acc, exportName) => {
404 const params = {
405 EXPORTS,
406 NAME: exportName,
407 VALUE: acc
408 };
409
410 if (stringSpecifiers.has(exportName)) {
411 return InitTemplate.computed(params);
412 } else {
413 return InitTemplate.default(params);
414 }
415 }, initExpr));
416}
Note: See TracBrowser for help on using the repository browser.