You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

301 lines
9.5 KiB

4 years ago
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.rewriteModuleStatementsAndPrepareHeader = rewriteModuleStatementsAndPrepareHeader;
  6. exports.ensureStatementsHoisted = ensureStatementsHoisted;
  7. exports.wrapInterop = wrapInterop;
  8. exports.buildNamespaceInitStatements = buildNamespaceInitStatements;
  9. Object.defineProperty(exports, "isModule", {
  10. enumerable: true,
  11. get: function () {
  12. return _helperModuleImports.isModule;
  13. }
  14. });
  15. Object.defineProperty(exports, "hasExports", {
  16. enumerable: true,
  17. get: function () {
  18. return _normalizeAndLoadMetadata.hasExports;
  19. }
  20. });
  21. Object.defineProperty(exports, "isSideEffectImport", {
  22. enumerable: true,
  23. get: function () {
  24. return _normalizeAndLoadMetadata.isSideEffectImport;
  25. }
  26. });
  27. var _assert = _interopRequireDefault(require("assert"));
  28. var t = _interopRequireWildcard(require("@babel/types"));
  29. var _template = _interopRequireDefault(require("@babel/template"));
  30. var _chunk = _interopRequireDefault(require("lodash/chunk"));
  31. var _helperModuleImports = require("@babel/helper-module-imports");
  32. var _rewriteThis = _interopRequireDefault(require("./rewrite-this"));
  33. var _rewriteLiveReferences = _interopRequireDefault(require("./rewrite-live-references"));
  34. var _normalizeAndLoadMetadata = _interopRequireWildcard(require("./normalize-and-load-metadata"));
  35. function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
  36. function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
  37. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  38. function rewriteModuleStatementsAndPrepareHeader(path, {
  39. exportName,
  40. strict,
  41. allowTopLevelThis,
  42. strictMode,
  43. loose,
  44. noInterop,
  45. lazy,
  46. esNamespaceOnly
  47. }) {
  48. (0, _assert.default)((0, _helperModuleImports.isModule)(path), "Cannot process module statements in a script");
  49. path.node.sourceType = "script";
  50. const meta = (0, _normalizeAndLoadMetadata.default)(path, exportName, {
  51. noInterop,
  52. loose,
  53. lazy,
  54. esNamespaceOnly
  55. });
  56. if (!allowTopLevelThis) {
  57. (0, _rewriteThis.default)(path);
  58. }
  59. (0, _rewriteLiveReferences.default)(path, meta);
  60. if (strictMode !== false) {
  61. const hasStrict = path.node.directives.some(directive => {
  62. return directive.value.value === "use strict";
  63. });
  64. if (!hasStrict) {
  65. path.unshiftContainer("directives", t.directive(t.directiveLiteral("use strict")));
  66. }
  67. }
  68. const headers = [];
  69. if ((0, _normalizeAndLoadMetadata.hasExports)(meta) && !strict) {
  70. headers.push(buildESModuleHeader(meta, loose));
  71. }
  72. const nameList = buildExportNameListDeclaration(path, meta);
  73. if (nameList) {
  74. meta.exportNameListName = nameList.name;
  75. headers.push(nameList.statement);
  76. }
  77. headers.push(...buildExportInitializationStatements(path, meta, loose));
  78. return {
  79. meta,
  80. headers
  81. };
  82. }
  83. function ensureStatementsHoisted(statements) {
  84. statements.forEach(header => {
  85. header._blockHoist = 3;
  86. });
  87. }
  88. function wrapInterop(programPath, expr, type) {
  89. if (type === "none") {
  90. return null;
  91. }
  92. let helper;
  93. if (type === "default") {
  94. helper = "interopRequireDefault";
  95. } else if (type === "namespace") {
  96. helper = "interopRequireWildcard";
  97. } else {
  98. throw new Error(`Unknown interop: ${type}`);
  99. }
  100. return t.callExpression(programPath.hub.addHelper(helper), [expr]);
  101. }
  102. function buildNamespaceInitStatements(metadata, sourceMetadata, loose = false) {
  103. const statements = [];
  104. let srcNamespace = t.identifier(sourceMetadata.name);
  105. if (sourceMetadata.lazy) srcNamespace = t.callExpression(srcNamespace, []);
  106. for (const localName of sourceMetadata.importsNamespace) {
  107. if (localName === sourceMetadata.name) continue;
  108. statements.push(_template.default.statement`var NAME = SOURCE;`({
  109. NAME: localName,
  110. SOURCE: t.cloneNode(srcNamespace)
  111. }));
  112. }
  113. if (loose) {
  114. statements.push(...buildReexportsFromMeta(metadata, sourceMetadata, loose));
  115. }
  116. for (const exportName of sourceMetadata.reexportNamespace) {
  117. statements.push((sourceMetadata.lazy ? _template.default.statement`
  118. Object.defineProperty(EXPORTS, "NAME", {
  119. enumerable: true,
  120. get: function() {
  121. return NAMESPACE;
  122. }
  123. });
  124. ` : _template.default.statement`EXPORTS.NAME = NAMESPACE;`)({
  125. EXPORTS: metadata.exportName,
  126. NAME: exportName,
  127. NAMESPACE: t.cloneNode(srcNamespace)
  128. }));
  129. }
  130. if (sourceMetadata.reexportAll) {
  131. const statement = buildNamespaceReexport(metadata, t.cloneNode(srcNamespace), loose);
  132. statement.loc = sourceMetadata.reexportAll.loc;
  133. statements.push(statement);
  134. }
  135. return statements;
  136. }
  137. const getTemplateForReexport = loose => {
  138. return loose ? _template.default.statement`EXPORTS.EXPORT_NAME = NAMESPACE.IMPORT_NAME;` : _template.default`
  139. Object.defineProperty(EXPORTS, "EXPORT_NAME", {
  140. enumerable: true,
  141. get: function() {
  142. return NAMESPACE.IMPORT_NAME;
  143. },
  144. });
  145. `;
  146. };
  147. const buildReexportsFromMeta = (meta, metadata, loose) => {
  148. const namespace = metadata.lazy ? t.callExpression(t.identifier(metadata.name), []) : t.identifier(metadata.name);
  149. const templateForCurrentMode = getTemplateForReexport(loose);
  150. return Array.from(metadata.reexports, ([exportName, importName]) => templateForCurrentMode({
  151. EXPORTS: meta.exportName,
  152. EXPORT_NAME: exportName,
  153. NAMESPACE: t.cloneNode(namespace),
  154. IMPORT_NAME: importName
  155. }));
  156. };
  157. function buildESModuleHeader(metadata, enumerable = false) {
  158. return (enumerable ? _template.default.statement`
  159. EXPORTS.__esModule = true;
  160. ` : _template.default.statement`
  161. Object.defineProperty(EXPORTS, "__esModule", {
  162. value: true,
  163. });
  164. `)({
  165. EXPORTS: metadata.exportName
  166. });
  167. }
  168. function buildNamespaceReexport(metadata, namespace, loose) {
  169. return (loose ? _template.default.statement`
  170. Object.keys(NAMESPACE).forEach(function(key) {
  171. if (key === "default" || key === "__esModule") return;
  172. VERIFY_NAME_LIST;
  173. EXPORTS[key] = NAMESPACE[key];
  174. });
  175. ` : _template.default.statement`
  176. Object.keys(NAMESPACE).forEach(function(key) {
  177. if (key === "default" || key === "__esModule") return;
  178. VERIFY_NAME_LIST;
  179. Object.defineProperty(EXPORTS, key, {
  180. enumerable: true,
  181. get: function() {
  182. return NAMESPACE[key];
  183. },
  184. });
  185. });
  186. `)({
  187. NAMESPACE: namespace,
  188. EXPORTS: metadata.exportName,
  189. VERIFY_NAME_LIST: metadata.exportNameListName ? _template.default`
  190. if (Object.prototype.hasOwnProperty.call(EXPORTS_LIST, key)) return;
  191. `({
  192. EXPORTS_LIST: metadata.exportNameListName
  193. }) : null
  194. });
  195. }
  196. function buildExportNameListDeclaration(programPath, metadata) {
  197. const exportedVars = Object.create(null);
  198. for (const data of metadata.local.values()) {
  199. for (const name of data.names) {
  200. exportedVars[name] = true;
  201. }
  202. }
  203. let hasReexport = false;
  204. for (const data of metadata.source.values()) {
  205. for (const exportName of data.reexports.keys()) {
  206. exportedVars[exportName] = true;
  207. }
  208. for (const exportName of data.reexportNamespace) {
  209. exportedVars[exportName] = true;
  210. }
  211. hasReexport = hasReexport || data.reexportAll;
  212. }
  213. if (!hasReexport || Object.keys(exportedVars).length === 0) return null;
  214. const name = programPath.scope.generateUidIdentifier("exportNames");
  215. delete exportedVars.default;
  216. return {
  217. name: name.name,
  218. statement: t.variableDeclaration("var", [t.variableDeclarator(name, t.valueToNode(exportedVars))])
  219. };
  220. }
  221. function buildExportInitializationStatements(programPath, metadata, loose = false) {
  222. const initStatements = [];
  223. const exportNames = [];
  224. for (const [localName, data] of metadata.local) {
  225. if (data.kind === "import") {} else if (data.kind === "hoisted") {
  226. initStatements.push(buildInitStatement(metadata, data.names, t.identifier(localName)));
  227. } else {
  228. exportNames.push(...data.names);
  229. }
  230. }
  231. for (const data of metadata.source.values()) {
  232. if (!loose) {
  233. initStatements.push(...buildReexportsFromMeta(metadata, data, loose));
  234. }
  235. for (const exportName of data.reexportNamespace) {
  236. exportNames.push(exportName);
  237. }
  238. }
  239. initStatements.push(...(0, _chunk.default)(exportNames, 100).map(members => {
  240. return buildInitStatement(metadata, members, programPath.scope.buildUndefinedNode());
  241. }));
  242. return initStatements;
  243. }
  244. function buildInitStatement(metadata, exportNames, initExpr) {
  245. return t.expressionStatement(exportNames.reduce((acc, exportName) => _template.default.expression`EXPORTS.NAME = VALUE`({
  246. EXPORTS: metadata.exportName,
  247. NAME: exportName,
  248. VALUE: acc
  249. }), initExpr));
  250. }