source: trip-planner-front/node_modules/@babel/plugin-transform-modules-commonjs/lib/index.js@ eed0bf8

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

initial commit

  • Property mode set to 100644
File size: 6.9 KB
Line 
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = void 0;
7
8var _helperPluginUtils = require("@babel/helper-plugin-utils");
9
10var _helperModuleTransforms = require("@babel/helper-module-transforms");
11
12var _helperSimpleAccess = require("@babel/helper-simple-access");
13
14var _core = require("@babel/core");
15
16var _utils = require("babel-plugin-dynamic-import-node/utils");
17
18var _default = (0, _helperPluginUtils.declare)((api, options) => {
19 var _api$assumption, _api$assumption2, _api$assumption3;
20
21 api.assertVersion(7);
22 const transformImportCall = (0, _utils.createDynamicImportTransform)(api);
23 const {
24 strictNamespace = false,
25 mjsStrictNamespace = true,
26 allowTopLevelThis,
27 strict,
28 strictMode,
29 noInterop,
30 importInterop,
31 lazy = false,
32 allowCommonJSExports = true
33 } = options;
34 const constantReexports = (_api$assumption = api.assumption("constantReexports")) != null ? _api$assumption : options.loose;
35 const enumerableModuleMeta = (_api$assumption2 = api.assumption("enumerableModuleMeta")) != null ? _api$assumption2 : options.loose;
36 const noIncompleteNsImportDetection = (_api$assumption3 = api.assumption("noIncompleteNsImportDetection")) != null ? _api$assumption3 : false;
37
38 if (typeof lazy !== "boolean" && typeof lazy !== "function" && (!Array.isArray(lazy) || !lazy.every(item => typeof item === "string"))) {
39 throw new Error(`.lazy must be a boolean, array of strings, or a function`);
40 }
41
42 if (typeof strictNamespace !== "boolean") {
43 throw new Error(`.strictNamespace must be a boolean, or undefined`);
44 }
45
46 if (typeof mjsStrictNamespace !== "boolean") {
47 throw new Error(`.mjsStrictNamespace must be a boolean, or undefined`);
48 }
49
50 const getAssertion = localName => _core.template.expression.ast`
51 (function(){
52 throw new Error(
53 "The CommonJS '" + "${localName}" + "' variable is not available in ES6 modules." +
54 "Consider setting setting sourceType:script or sourceType:unambiguous in your " +
55 "Babel config for this file.");
56 })()
57 `;
58
59 const moduleExportsVisitor = {
60 ReferencedIdentifier(path) {
61 const localName = path.node.name;
62 if (localName !== "module" && localName !== "exports") return;
63 const localBinding = path.scope.getBinding(localName);
64 const rootBinding = this.scope.getBinding(localName);
65
66 if (rootBinding !== localBinding || path.parentPath.isObjectProperty({
67 value: path.node
68 }) && path.parentPath.parentPath.isObjectPattern() || path.parentPath.isAssignmentExpression({
69 left: path.node
70 }) || path.isAssignmentExpression({
71 left: path.node
72 })) {
73 return;
74 }
75
76 path.replaceWith(getAssertion(localName));
77 },
78
79 AssignmentExpression(path) {
80 const left = path.get("left");
81
82 if (left.isIdentifier()) {
83 const localName = path.node.name;
84 if (localName !== "module" && localName !== "exports") return;
85 const localBinding = path.scope.getBinding(localName);
86 const rootBinding = this.scope.getBinding(localName);
87 if (rootBinding !== localBinding) return;
88 const right = path.get("right");
89 right.replaceWith(_core.types.sequenceExpression([right.node, getAssertion(localName)]));
90 } else if (left.isPattern()) {
91 const ids = left.getOuterBindingIdentifiers();
92 const localName = Object.keys(ids).filter(localName => {
93 if (localName !== "module" && localName !== "exports") return false;
94 return this.scope.getBinding(localName) === path.scope.getBinding(localName);
95 })[0];
96
97 if (localName) {
98 const right = path.get("right");
99 right.replaceWith(_core.types.sequenceExpression([right.node, getAssertion(localName)]));
100 }
101 }
102 }
103
104 };
105 return {
106 name: "transform-modules-commonjs",
107
108 pre() {
109 this.file.set("@babel/plugin-transform-modules-*", "commonjs");
110 },
111
112 visitor: {
113 CallExpression(path) {
114 if (!this.file.has("@babel/plugin-proposal-dynamic-import")) return;
115 if (!path.get("callee").isImport()) return;
116 let {
117 scope
118 } = path;
119
120 do {
121 scope.rename("require");
122 } while (scope = scope.parent);
123
124 transformImportCall(this, path.get("callee"));
125 },
126
127 Program: {
128 exit(path, state) {
129 if (!(0, _helperModuleTransforms.isModule)(path)) return;
130 path.scope.rename("exports");
131 path.scope.rename("module");
132 path.scope.rename("require");
133 path.scope.rename("__filename");
134 path.scope.rename("__dirname");
135
136 if (!allowCommonJSExports) {
137 (0, _helperSimpleAccess.default)(path, new Set(["module", "exports"]));
138 path.traverse(moduleExportsVisitor, {
139 scope: path.scope
140 });
141 }
142
143 let moduleName = (0, _helperModuleTransforms.getModuleName)(this.file.opts, options);
144 if (moduleName) moduleName = _core.types.stringLiteral(moduleName);
145 const {
146 meta,
147 headers
148 } = (0, _helperModuleTransforms.rewriteModuleStatementsAndPrepareHeader)(path, {
149 exportName: "exports",
150 constantReexports,
151 enumerableModuleMeta,
152 strict,
153 strictMode,
154 allowTopLevelThis,
155 noInterop,
156 importInterop,
157 lazy,
158 esNamespaceOnly: typeof state.filename === "string" && /\.mjs$/.test(state.filename) ? mjsStrictNamespace : strictNamespace,
159 noIncompleteNsImportDetection
160 });
161
162 for (const [source, metadata] of meta.source) {
163 const loadExpr = _core.types.callExpression(_core.types.identifier("require"), [_core.types.stringLiteral(source)]);
164
165 let header;
166
167 if ((0, _helperModuleTransforms.isSideEffectImport)(metadata)) {
168 if (metadata.lazy) throw new Error("Assertion failure");
169 header = _core.types.expressionStatement(loadExpr);
170 } else {
171 const init = (0, _helperModuleTransforms.wrapInterop)(path, loadExpr, metadata.interop) || loadExpr;
172
173 if (metadata.lazy) {
174 header = _core.template.ast`
175 function ${metadata.name}() {
176 const data = ${init};
177 ${metadata.name} = function(){ return data; };
178 return data;
179 }
180 `;
181 } else {
182 header = _core.template.ast`
183 var ${metadata.name} = ${init};
184 `;
185 }
186 }
187
188 header.loc = metadata.loc;
189 headers.push(header);
190 headers.push(...(0, _helperModuleTransforms.buildNamespaceInitStatements)(meta, metadata, constantReexports));
191 }
192
193 (0, _helperModuleTransforms.ensureStatementsHoisted)(headers);
194 path.unshiftContainer("body", headers);
195 }
196
197 }
198 }
199 };
200});
201
202exports.default = _default;
Note: See TracBrowser for help on using the repository browser.