1 | "use strict";
|
---|
2 |
|
---|
3 | Object.defineProperty(exports, "__esModule", {
|
---|
4 | value: true
|
---|
5 | });
|
---|
6 | exports.default = void 0;
|
---|
7 |
|
---|
8 | var _helperPluginUtils = require("@babel/helper-plugin-utils");
|
---|
9 |
|
---|
10 | var _path = require("path");
|
---|
11 |
|
---|
12 | var _helperModuleTransforms = require("@babel/helper-module-transforms");
|
---|
13 |
|
---|
14 | var _core = require("@babel/core");
|
---|
15 |
|
---|
16 | const buildPrerequisiteAssignment = (0, _core.template)(`
|
---|
17 | GLOBAL_REFERENCE = GLOBAL_REFERENCE || {}
|
---|
18 | `);
|
---|
19 | const buildWrapper = (0, _core.template)(`
|
---|
20 | (function (global, factory) {
|
---|
21 | if (typeof define === "function" && define.amd) {
|
---|
22 | define(MODULE_NAME, AMD_ARGUMENTS, factory);
|
---|
23 | } else if (typeof exports !== "undefined") {
|
---|
24 | factory(COMMONJS_ARGUMENTS);
|
---|
25 | } else {
|
---|
26 | var mod = { exports: {} };
|
---|
27 | factory(BROWSER_ARGUMENTS);
|
---|
28 |
|
---|
29 | GLOBAL_TO_ASSIGN;
|
---|
30 | }
|
---|
31 | })(
|
---|
32 | typeof globalThis !== "undefined" ? globalThis
|
---|
33 | : typeof self !== "undefined" ? self
|
---|
34 | : this,
|
---|
35 | function(IMPORT_NAMES) {
|
---|
36 | })
|
---|
37 | `);
|
---|
38 |
|
---|
39 | var _default = (0, _helperPluginUtils.declare)((api, options) => {
|
---|
40 | var _api$assumption, _api$assumption2;
|
---|
41 |
|
---|
42 | api.assertVersion(7);
|
---|
43 | const {
|
---|
44 | globals,
|
---|
45 | exactGlobals,
|
---|
46 | allowTopLevelThis,
|
---|
47 | strict,
|
---|
48 | strictMode,
|
---|
49 | noInterop,
|
---|
50 | importInterop
|
---|
51 | } = options;
|
---|
52 | const constantReexports = (_api$assumption = api.assumption("constantReexports")) != null ? _api$assumption : options.loose;
|
---|
53 | const enumerableModuleMeta = (_api$assumption2 = api.assumption("enumerableModuleMeta")) != null ? _api$assumption2 : options.loose;
|
---|
54 |
|
---|
55 | function buildBrowserInit(browserGlobals, exactGlobals, filename, moduleName) {
|
---|
56 | const moduleNameOrBasename = moduleName ? moduleName.value : (0, _path.basename)(filename, (0, _path.extname)(filename));
|
---|
57 |
|
---|
58 | let globalToAssign = _core.types.memberExpression(_core.types.identifier("global"), _core.types.identifier(_core.types.toIdentifier(moduleNameOrBasename)));
|
---|
59 |
|
---|
60 | let initAssignments = [];
|
---|
61 |
|
---|
62 | if (exactGlobals) {
|
---|
63 | const globalName = browserGlobals[moduleNameOrBasename];
|
---|
64 |
|
---|
65 | if (globalName) {
|
---|
66 | initAssignments = [];
|
---|
67 | const members = globalName.split(".");
|
---|
68 | globalToAssign = members.slice(1).reduce((accum, curr) => {
|
---|
69 | initAssignments.push(buildPrerequisiteAssignment({
|
---|
70 | GLOBAL_REFERENCE: _core.types.cloneNode(accum)
|
---|
71 | }));
|
---|
72 | return _core.types.memberExpression(accum, _core.types.identifier(curr));
|
---|
73 | }, _core.types.memberExpression(_core.types.identifier("global"), _core.types.identifier(members[0])));
|
---|
74 | }
|
---|
75 | }
|
---|
76 |
|
---|
77 | initAssignments.push(_core.types.expressionStatement(_core.types.assignmentExpression("=", globalToAssign, _core.types.memberExpression(_core.types.identifier("mod"), _core.types.identifier("exports")))));
|
---|
78 | return initAssignments;
|
---|
79 | }
|
---|
80 |
|
---|
81 | function buildBrowserArg(browserGlobals, exactGlobals, source) {
|
---|
82 | let memberExpression;
|
---|
83 |
|
---|
84 | if (exactGlobals) {
|
---|
85 | const globalRef = browserGlobals[source];
|
---|
86 |
|
---|
87 | if (globalRef) {
|
---|
88 | memberExpression = globalRef.split(".").reduce((accum, curr) => _core.types.memberExpression(accum, _core.types.identifier(curr)), _core.types.identifier("global"));
|
---|
89 | } else {
|
---|
90 | memberExpression = _core.types.memberExpression(_core.types.identifier("global"), _core.types.identifier(_core.types.toIdentifier(source)));
|
---|
91 | }
|
---|
92 | } else {
|
---|
93 | const requireName = (0, _path.basename)(source, (0, _path.extname)(source));
|
---|
94 | const globalName = browserGlobals[requireName] || requireName;
|
---|
95 | memberExpression = _core.types.memberExpression(_core.types.identifier("global"), _core.types.identifier(_core.types.toIdentifier(globalName)));
|
---|
96 | }
|
---|
97 |
|
---|
98 | return memberExpression;
|
---|
99 | }
|
---|
100 |
|
---|
101 | return {
|
---|
102 | name: "transform-modules-umd",
|
---|
103 | visitor: {
|
---|
104 | Program: {
|
---|
105 | exit(path) {
|
---|
106 | if (!(0, _helperModuleTransforms.isModule)(path)) return;
|
---|
107 | const browserGlobals = globals || {};
|
---|
108 | let moduleName = (0, _helperModuleTransforms.getModuleName)(this.file.opts, options);
|
---|
109 | if (moduleName) moduleName = _core.types.stringLiteral(moduleName);
|
---|
110 | const {
|
---|
111 | meta,
|
---|
112 | headers
|
---|
113 | } = (0, _helperModuleTransforms.rewriteModuleStatementsAndPrepareHeader)(path, {
|
---|
114 | constantReexports,
|
---|
115 | enumerableModuleMeta,
|
---|
116 | strict,
|
---|
117 | strictMode,
|
---|
118 | allowTopLevelThis,
|
---|
119 | noInterop,
|
---|
120 | importInterop
|
---|
121 | });
|
---|
122 | const amdArgs = [];
|
---|
123 | const commonjsArgs = [];
|
---|
124 | const browserArgs = [];
|
---|
125 | const importNames = [];
|
---|
126 |
|
---|
127 | if ((0, _helperModuleTransforms.hasExports)(meta)) {
|
---|
128 | amdArgs.push(_core.types.stringLiteral("exports"));
|
---|
129 | commonjsArgs.push(_core.types.identifier("exports"));
|
---|
130 | browserArgs.push(_core.types.memberExpression(_core.types.identifier("mod"), _core.types.identifier("exports")));
|
---|
131 | importNames.push(_core.types.identifier(meta.exportName));
|
---|
132 | }
|
---|
133 |
|
---|
134 | for (const [source, metadata] of meta.source) {
|
---|
135 | amdArgs.push(_core.types.stringLiteral(source));
|
---|
136 | commonjsArgs.push(_core.types.callExpression(_core.types.identifier("require"), [_core.types.stringLiteral(source)]));
|
---|
137 | browserArgs.push(buildBrowserArg(browserGlobals, exactGlobals, source));
|
---|
138 | importNames.push(_core.types.identifier(metadata.name));
|
---|
139 |
|
---|
140 | if (!(0, _helperModuleTransforms.isSideEffectImport)(metadata)) {
|
---|
141 | const interop = (0, _helperModuleTransforms.wrapInterop)(path, _core.types.identifier(metadata.name), metadata.interop);
|
---|
142 |
|
---|
143 | if (interop) {
|
---|
144 | const header = _core.types.expressionStatement(_core.types.assignmentExpression("=", _core.types.identifier(metadata.name), interop));
|
---|
145 |
|
---|
146 | header.loc = meta.loc;
|
---|
147 | headers.push(header);
|
---|
148 | }
|
---|
149 | }
|
---|
150 |
|
---|
151 | headers.push(...(0, _helperModuleTransforms.buildNamespaceInitStatements)(meta, metadata, constantReexports));
|
---|
152 | }
|
---|
153 |
|
---|
154 | (0, _helperModuleTransforms.ensureStatementsHoisted)(headers);
|
---|
155 | path.unshiftContainer("body", headers);
|
---|
156 | const {
|
---|
157 | body,
|
---|
158 | directives
|
---|
159 | } = path.node;
|
---|
160 | path.node.directives = [];
|
---|
161 | path.node.body = [];
|
---|
162 | const umdWrapper = path.pushContainer("body", [buildWrapper({
|
---|
163 | MODULE_NAME: moduleName,
|
---|
164 | AMD_ARGUMENTS: _core.types.arrayExpression(amdArgs),
|
---|
165 | COMMONJS_ARGUMENTS: commonjsArgs,
|
---|
166 | BROWSER_ARGUMENTS: browserArgs,
|
---|
167 | IMPORT_NAMES: importNames,
|
---|
168 | GLOBAL_TO_ASSIGN: buildBrowserInit(browserGlobals, exactGlobals, this.filename || "unknown", moduleName)
|
---|
169 | })])[0];
|
---|
170 | const umdFactory = umdWrapper.get("expression.arguments")[1].get("body");
|
---|
171 | umdFactory.pushContainer("directives", directives);
|
---|
172 | umdFactory.pushContainer("body", body);
|
---|
173 | }
|
---|
174 |
|
---|
175 | }
|
---|
176 | }
|
---|
177 | };
|
---|
178 | });
|
---|
179 |
|
---|
180 | exports.default = _default; |
---|