normalize-and-load-metadata.js 12 KB

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