index.js 12 KB

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