normalize-and-load-metadata.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.hasExports = hasExports;
  6. exports.isSideEffectImport = isSideEffectImport;
  7. exports.validateImportInteropOption = validateImportInteropOption;
  8. exports.default = normalizeModuleAndLoadMetadata;
  9. var _path = require("path");
  10. var _helperValidatorIdentifier = require("@babel/helper-validator-identifier");
  11. var _helperSplitExportDeclaration = require("@babel/helper-split-export-declaration");
  12. function hasExports(metadata) {
  13. return metadata.hasExports;
  14. }
  15. function isSideEffectImport(source) {
  16. return source.imports.size === 0 && source.importsNamespace.size === 0 && source.reexports.size === 0 && source.reexportNamespace.size === 0 && !source.reexportAll;
  17. }
  18. function validateImportInteropOption(importInterop) {
  19. if (typeof importInterop !== "function" && importInterop !== "none" && importInterop !== "babel" && importInterop !== "node") {
  20. throw new Error(`.importInterop must be one of "none", "babel", "node", or a function returning one of those values (received ${importInterop}).`);
  21. }
  22. return importInterop;
  23. }
  24. function resolveImportInterop(importInterop, source) {
  25. if (typeof importInterop === "function") {
  26. return validateImportInteropOption(importInterop(source));
  27. }
  28. return importInterop;
  29. }
  30. function normalizeModuleAndLoadMetadata(programPath, exportName, {
  31. importInterop,
  32. initializeReexports = false,
  33. lazy = false,
  34. esNamespaceOnly = false
  35. }) {
  36. if (!exportName) {
  37. exportName = programPath.scope.generateUidIdentifier("exports").name;
  38. }
  39. const stringSpecifiers = new Set();
  40. nameAnonymousExports(programPath);
  41. const {
  42. local,
  43. source,
  44. hasExports
  45. } = getModuleMetadata(programPath, {
  46. initializeReexports,
  47. lazy
  48. }, stringSpecifiers);
  49. removeModuleDeclarations(programPath);
  50. for (const [, metadata] of source) {
  51. if (metadata.importsNamespace.size > 0) {
  52. metadata.name = metadata.importsNamespace.values().next().value;
  53. }
  54. const resolvedInterop = resolveImportInterop(importInterop, metadata.source);
  55. if (resolvedInterop === "none") {
  56. metadata.interop = "none";
  57. } else if (resolvedInterop === "node" && metadata.interop === "namespace") {
  58. metadata.interop = "node-namespace";
  59. } else if (resolvedInterop === "node" && metadata.interop === "default") {
  60. metadata.interop = "node-default";
  61. } else if (esNamespaceOnly && metadata.interop === "namespace") {
  62. metadata.interop = "default";
  63. }
  64. }
  65. return {
  66. exportName,
  67. exportNameListName: null,
  68. hasExports,
  69. local,
  70. source,
  71. stringSpecifiers
  72. };
  73. }
  74. function getExportSpecifierName(path, stringSpecifiers) {
  75. if (path.isIdentifier()) {
  76. return path.node.name;
  77. } else if (path.isStringLiteral()) {
  78. const stringValue = path.node.value;
  79. if (!(0, _helperValidatorIdentifier.isIdentifierName)(stringValue)) {
  80. stringSpecifiers.add(stringValue);
  81. }
  82. return stringValue;
  83. } else {
  84. throw new Error(`Expected export specifier to be either Identifier or StringLiteral, got ${path.node.type}`);
  85. }
  86. }
  87. function assertExportSpecifier(path) {
  88. if (path.isExportSpecifier()) {
  89. return;
  90. } else if (path.isExportNamespaceSpecifier()) {
  91. throw path.buildCodeFrameError("Export namespace should be first transformed by `@babel/plugin-proposal-export-namespace-from`.");
  92. } else {
  93. throw path.buildCodeFrameError("Unexpected export specifier type");
  94. }
  95. }
  96. function getModuleMetadata(programPath, {
  97. lazy,
  98. initializeReexports
  99. }, stringSpecifiers) {
  100. const localData = getLocalExportMetadata(programPath, initializeReexports, stringSpecifiers);
  101. const sourceData = new Map();
  102. const getData = sourceNode => {
  103. const source = sourceNode.value;
  104. let data = sourceData.get(source);
  105. if (!data) {
  106. data = {
  107. name: programPath.scope.generateUidIdentifier((0, _path.basename)(source, (0, _path.extname)(source))).name,
  108. interop: "none",
  109. loc: null,
  110. imports: new Map(),
  111. importsNamespace: new Set(),
  112. reexports: new Map(),
  113. reexportNamespace: new Set(),
  114. reexportAll: null,
  115. lazy: false,
  116. source
  117. };
  118. sourceData.set(source, data);
  119. }
  120. return data;
  121. };
  122. let hasExports = false;
  123. programPath.get("body").forEach(child => {
  124. if (child.isImportDeclaration()) {
  125. const data = getData(child.node.source);
  126. if (!data.loc) data.loc = child.node.loc;
  127. child.get("specifiers").forEach(spec => {
  128. if (spec.isImportDefaultSpecifier()) {
  129. const localName = spec.get("local").node.name;
  130. data.imports.set(localName, "default");
  131. const reexport = localData.get(localName);
  132. if (reexport) {
  133. localData.delete(localName);
  134. reexport.names.forEach(name => {
  135. data.reexports.set(name, "default");
  136. });
  137. }
  138. } else if (spec.isImportNamespaceSpecifier()) {
  139. const localName = spec.get("local").node.name;
  140. data.importsNamespace.add(localName);
  141. const reexport = localData.get(localName);
  142. if (reexport) {
  143. localData.delete(localName);
  144. reexport.names.forEach(name => {
  145. data.reexportNamespace.add(name);
  146. });
  147. }
  148. } else if (spec.isImportSpecifier()) {
  149. const importName = getExportSpecifierName(spec.get("imported"), stringSpecifiers);
  150. const localName = spec.get("local").node.name;
  151. data.imports.set(localName, importName);
  152. const reexport = localData.get(localName);
  153. if (reexport) {
  154. localData.delete(localName);
  155. reexport.names.forEach(name => {
  156. data.reexports.set(name, importName);
  157. });
  158. }
  159. }
  160. });
  161. } else if (child.isExportAllDeclaration()) {
  162. hasExports = true;
  163. const data = getData(child.node.source);
  164. if (!data.loc) data.loc = child.node.loc;
  165. data.reexportAll = {
  166. loc: child.node.loc
  167. };
  168. } else if (child.isExportNamedDeclaration() && child.node.source) {
  169. hasExports = true;
  170. const data = getData(child.node.source);
  171. if (!data.loc) data.loc = child.node.loc;
  172. child.get("specifiers").forEach(spec => {
  173. assertExportSpecifier(spec);
  174. const importName = getExportSpecifierName(spec.get("local"), stringSpecifiers);
  175. const exportName = getExportSpecifierName(spec.get("exported"), stringSpecifiers);
  176. data.reexports.set(exportName, importName);
  177. if (exportName === "__esModule") {
  178. throw spec.get("exported").buildCodeFrameError('Illegal export "__esModule".');
  179. }
  180. });
  181. } else if (child.isExportNamedDeclaration() || child.isExportDefaultDeclaration()) {
  182. hasExports = true;
  183. }
  184. });
  185. for (const metadata of sourceData.values()) {
  186. let needsDefault = false;
  187. let needsNamed = false;
  188. if (metadata.importsNamespace.size > 0) {
  189. needsDefault = true;
  190. needsNamed = true;
  191. }
  192. if (metadata.reexportAll) {
  193. needsNamed = true;
  194. }
  195. for (const importName of metadata.imports.values()) {
  196. if (importName === "default") needsDefault = true;else needsNamed = true;
  197. }
  198. for (const importName of metadata.reexports.values()) {
  199. if (importName === "default") needsDefault = true;else needsNamed = true;
  200. }
  201. if (needsDefault && needsNamed) {
  202. metadata.interop = "namespace";
  203. } else if (needsDefault) {
  204. metadata.interop = "default";
  205. }
  206. }
  207. for (const [source, metadata] of sourceData) {
  208. if (lazy !== false && !(isSideEffectImport(metadata) || metadata.reexportAll)) {
  209. if (lazy === true) {
  210. metadata.lazy = !/\./.test(source);
  211. } else if (Array.isArray(lazy)) {
  212. metadata.lazy = lazy.indexOf(source) !== -1;
  213. } else if (typeof lazy === "function") {
  214. metadata.lazy = lazy(source);
  215. } else {
  216. throw new Error(`.lazy must be a boolean, string array, or function`);
  217. }
  218. }
  219. }
  220. return {
  221. hasExports,
  222. local: localData,
  223. source: sourceData
  224. };
  225. }
  226. function getLocalExportMetadata(programPath, initializeReexports, stringSpecifiers) {
  227. const bindingKindLookup = new Map();
  228. programPath.get("body").forEach(child => {
  229. let kind;
  230. if (child.isImportDeclaration()) {
  231. kind = "import";
  232. } else {
  233. if (child.isExportDefaultDeclaration()) child = child.get("declaration");
  234. if (child.isExportNamedDeclaration()) {
  235. if (child.node.declaration) {
  236. child = child.get("declaration");
  237. } else if (initializeReexports && child.node.source && child.get("source").isStringLiteral()) {
  238. child.get("specifiers").forEach(spec => {
  239. assertExportSpecifier(spec);
  240. bindingKindLookup.set(spec.get("local").node.name, "block");
  241. });
  242. return;
  243. }
  244. }
  245. if (child.isFunctionDeclaration()) {
  246. kind = "hoisted";
  247. } else if (child.isClassDeclaration()) {
  248. kind = "block";
  249. } else if (child.isVariableDeclaration({
  250. kind: "var"
  251. })) {
  252. kind = "var";
  253. } else if (child.isVariableDeclaration()) {
  254. kind = "block";
  255. } else {
  256. return;
  257. }
  258. }
  259. Object.keys(child.getOuterBindingIdentifiers()).forEach(name => {
  260. bindingKindLookup.set(name, kind);
  261. });
  262. });
  263. const localMetadata = new Map();
  264. const getLocalMetadata = idPath => {
  265. const localName = idPath.node.name;
  266. let metadata = localMetadata.get(localName);
  267. if (!metadata) {
  268. const kind = bindingKindLookup.get(localName);
  269. if (kind === undefined) {
  270. throw idPath.buildCodeFrameError(`Exporting local "${localName}", which is not declared.`);
  271. }
  272. metadata = {
  273. names: [],
  274. kind
  275. };
  276. localMetadata.set(localName, metadata);
  277. }
  278. return metadata;
  279. };
  280. programPath.get("body").forEach(child => {
  281. if (child.isExportNamedDeclaration() && (initializeReexports || !child.node.source)) {
  282. if (child.node.declaration) {
  283. const declaration = child.get("declaration");
  284. const ids = declaration.getOuterBindingIdentifierPaths();
  285. Object.keys(ids).forEach(name => {
  286. if (name === "__esModule") {
  287. throw declaration.buildCodeFrameError('Illegal export "__esModule".');
  288. }
  289. getLocalMetadata(ids[name]).names.push(name);
  290. });
  291. } else {
  292. child.get("specifiers").forEach(spec => {
  293. const local = spec.get("local");
  294. const exported = spec.get("exported");
  295. const localMetadata = getLocalMetadata(local);
  296. const exportName = getExportSpecifierName(exported, stringSpecifiers);
  297. if (exportName === "__esModule") {
  298. throw exported.buildCodeFrameError('Illegal export "__esModule".');
  299. }
  300. localMetadata.names.push(exportName);
  301. });
  302. }
  303. } else if (child.isExportDefaultDeclaration()) {
  304. const declaration = child.get("declaration");
  305. if (declaration.isFunctionDeclaration() || declaration.isClassDeclaration()) {
  306. getLocalMetadata(declaration.get("id")).names.push("default");
  307. } else {
  308. throw declaration.buildCodeFrameError("Unexpected default expression export.");
  309. }
  310. }
  311. });
  312. return localMetadata;
  313. }
  314. function nameAnonymousExports(programPath) {
  315. programPath.get("body").forEach(child => {
  316. if (!child.isExportDefaultDeclaration()) return;
  317. (0, _helperSplitExportDeclaration.default)(child);
  318. });
  319. }
  320. function removeModuleDeclarations(programPath) {
  321. programPath.get("body").forEach(child => {
  322. if (child.isImportDeclaration()) {
  323. child.remove();
  324. } else if (child.isExportNamedDeclaration()) {
  325. if (child.node.declaration) {
  326. child.node.declaration._blockHoist = child.node._blockHoist;
  327. child.replaceWith(child.node.declaration);
  328. } else {
  329. child.remove();
  330. }
  331. } else if (child.isExportDefaultDeclaration()) {
  332. const declaration = child.get("declaration");
  333. if (declaration.isFunctionDeclaration() || declaration.isClassDeclaration()) {
  334. declaration._blockHoist = child.node._blockHoist;
  335. child.replaceWith(declaration);
  336. } else {
  337. throw declaration.buildCodeFrameError("Unexpected default expression export.");
  338. }
  339. } else if (child.isExportAllDeclaration()) {
  340. child.remove();
  341. }
  342. });
  343. }