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