source: imaps-frontend/node_modules/@babel/helper-module-transforms/lib/index.js@ 0c6b92a

main
Last change on this file since 0c6b92a was d565449, checked in by stefan toskovski <stefantoska84@…>, 3 months ago

Update repo after prototype presentation

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