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. sources,
  45. hasExports
  46. } = getModuleMetadata(programPath, {
  47. initializeReexports,
  48. lazy
  49. }, stringSpecifiers);
  50. removeImportExportDeclarations(programPath);
  51. for (const [source, metadata] of sources) {
  52. if (metadata.importsNamespace.size > 0) {
  53. metadata.name = metadata.importsNamespace.values().next().value;
  54. }
  55. const resolvedInterop = resolveImportInterop(importInterop, 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: sources,
  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. referenced: false
  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. data.referenced = true;
  139. }
  140. } else if (spec.isImportNamespaceSpecifier()) {
  141. const localName = spec.get("local").node.name;
  142. data.importsNamespace.add(localName);
  143. const reexport = localData.get(localName);
  144. if (reexport) {
  145. localData.delete(localName);
  146. reexport.names.forEach(name => {
  147. data.reexportNamespace.add(name);
  148. });
  149. data.referenced = true;
  150. }
  151. } else if (spec.isImportSpecifier()) {
  152. const importName = getExportSpecifierName(spec.get("imported"), stringSpecifiers);
  153. const localName = spec.get("local").node.name;
  154. data.imports.set(localName, importName);
  155. const reexport = localData.get(localName);
  156. if (reexport) {
  157. localData.delete(localName);
  158. reexport.names.forEach(name => {
  159. data.reexports.set(name, importName);
  160. });
  161. data.referenced = true;
  162. }
  163. }
  164. });
  165. } else if (child.isExportAllDeclaration()) {
  166. hasExports = true;
  167. const data = getData(child.node.source);
  168. if (!data.loc) data.loc = child.node.loc;
  169. data.reexportAll = {
  170. loc: child.node.loc
  171. };
  172. data.referenced = true;
  173. } else if (child.isExportNamedDeclaration() && child.node.source) {
  174. hasExports = true;
  175. const data = getData(child.node.source);
  176. if (!data.loc) data.loc = child.node.loc;
  177. child.get("specifiers").forEach(spec => {
  178. assertExportSpecifier(spec);
  179. const importName = getExportSpecifierName(spec.get("local"), stringSpecifiers);
  180. const exportName = getExportSpecifierName(spec.get("exported"), stringSpecifiers);
  181. data.reexports.set(exportName, importName);
  182. data.referenced = true;
  183. if (exportName === "__esModule") {
  184. throw spec.get("exported").buildCodeFrameError('Illegal export "__esModule".');
  185. }
  186. });
  187. } else if (child.isExportNamedDeclaration() || child.isExportDefaultDeclaration()) {
  188. hasExports = true;
  189. }
  190. });
  191. for (const metadata of sourceData.values()) {
  192. let needsDefault = false;
  193. let needsNamed = false;
  194. if (metadata.importsNamespace.size > 0) {
  195. needsDefault = true;
  196. needsNamed = true;
  197. }
  198. if (metadata.reexportAll) {
  199. needsNamed = true;
  200. }
  201. for (const importName of metadata.imports.values()) {
  202. if (importName === "default") needsDefault = true;else needsNamed = true;
  203. }
  204. for (const importName of metadata.reexports.values()) {
  205. if (importName === "default") needsDefault = true;else needsNamed = true;
  206. }
  207. if (needsDefault && needsNamed) {
  208. metadata.interop = "namespace";
  209. } else if (needsDefault) {
  210. metadata.interop = "default";
  211. }
  212. }
  213. for (const [source, metadata] of sourceData) {
  214. if (lazy !== false && !(isSideEffectImport(metadata) || metadata.reexportAll)) {
  215. if (lazy === true) {
  216. metadata.lazy = !/\./.test(source);
  217. } else if (Array.isArray(lazy)) {
  218. metadata.lazy = lazy.indexOf(source) !== -1;
  219. } else if (typeof lazy === "function") {
  220. metadata.lazy = lazy(source);
  221. } else {
  222. throw new Error(`.lazy must be a boolean, string array, or function`);
  223. }
  224. }
  225. }
  226. return {
  227. hasExports,
  228. local: localData,
  229. sources: sourceData
  230. };
  231. }
  232. function getLocalExportMetadata(programPath, initializeReexports, stringSpecifiers) {
  233. const bindingKindLookup = new Map();
  234. programPath.get("body").forEach(child => {
  235. let kind;
  236. if (child.isImportDeclaration()) {
  237. kind = "import";
  238. } else {
  239. if (child.isExportDefaultDeclaration()) {
  240. child = child.get("declaration");
  241. }
  242. if (child.isExportNamedDeclaration()) {
  243. if (child.node.declaration) {
  244. child = child.get("declaration");
  245. } else if (initializeReexports && child.node.source && child.get("source").isStringLiteral()) {
  246. child.get("specifiers").forEach(spec => {
  247. assertExportSpecifier(spec);
  248. bindingKindLookup.set(spec.get("local").node.name, "block");
  249. });
  250. return;
  251. }
  252. }
  253. if (child.isFunctionDeclaration()) {
  254. kind = "hoisted";
  255. } else if (child.isClassDeclaration()) {
  256. kind = "block";
  257. } else if (child.isVariableDeclaration({
  258. kind: "var"
  259. })) {
  260. kind = "var";
  261. } else if (child.isVariableDeclaration()) {
  262. kind = "block";
  263. } else {
  264. return;
  265. }
  266. }
  267. Object.keys(child.getOuterBindingIdentifiers()).forEach(name => {
  268. bindingKindLookup.set(name, kind);
  269. });
  270. });
  271. const localMetadata = new Map();
  272. const getLocalMetadata = idPath => {
  273. const localName = idPath.node.name;
  274. let metadata = localMetadata.get(localName);
  275. if (!metadata) {
  276. const kind = bindingKindLookup.get(localName);
  277. if (kind === undefined) {
  278. throw idPath.buildCodeFrameError(`Exporting local "${localName}", which is not declared.`);
  279. }
  280. metadata = {
  281. names: [],
  282. kind
  283. };
  284. localMetadata.set(localName, metadata);
  285. }
  286. return metadata;
  287. };
  288. programPath.get("body").forEach(child => {
  289. if (child.isExportNamedDeclaration() && (initializeReexports || !child.node.source)) {
  290. if (child.node.declaration) {
  291. const declaration = child.get("declaration");
  292. const ids = declaration.getOuterBindingIdentifierPaths();
  293. Object.keys(ids).forEach(name => {
  294. if (name === "__esModule") {
  295. throw declaration.buildCodeFrameError('Illegal export "__esModule".');
  296. }
  297. getLocalMetadata(ids[name]).names.push(name);
  298. });
  299. } else {
  300. child.get("specifiers").forEach(spec => {
  301. const local = spec.get("local");
  302. const exported = spec.get("exported");
  303. const localMetadata = getLocalMetadata(local);
  304. const exportName = getExportSpecifierName(exported, stringSpecifiers);
  305. if (exportName === "__esModule") {
  306. throw exported.buildCodeFrameError('Illegal export "__esModule".');
  307. }
  308. localMetadata.names.push(exportName);
  309. });
  310. }
  311. } else if (child.isExportDefaultDeclaration()) {
  312. const declaration = child.get("declaration");
  313. if (declaration.isFunctionDeclaration() || declaration.isClassDeclaration()) {
  314. getLocalMetadata(declaration.get("id")).names.push("default");
  315. } else {
  316. throw declaration.buildCodeFrameError("Unexpected default expression export.");
  317. }
  318. }
  319. });
  320. return localMetadata;
  321. }
  322. function nameAnonymousExports(programPath) {
  323. programPath.get("body").forEach(child => {
  324. if (!child.isExportDefaultDeclaration()) return;
  325. (0, _helperSplitExportDeclaration.default)(child);
  326. });
  327. }
  328. function removeImportExportDeclarations(programPath) {
  329. programPath.get("body").forEach(child => {
  330. if (child.isImportDeclaration()) {
  331. child.remove();
  332. } else if (child.isExportNamedDeclaration()) {
  333. if (child.node.declaration) {
  334. child.node.declaration._blockHoist = child.node._blockHoist;
  335. child.replaceWith(child.node.declaration);
  336. } else {
  337. child.remove();
  338. }
  339. } else if (child.isExportDefaultDeclaration()) {
  340. const declaration = child.get("declaration");
  341. if (declaration.isFunctionDeclaration() || declaration.isClassDeclaration()) {
  342. declaration._blockHoist = child.node._blockHoist;
  343. child.replaceWith(declaration);
  344. } else {
  345. throw declaration.buildCodeFrameError("Unexpected default expression export.");
  346. }
  347. } else if (child.isExportAllDeclaration()) {
  348. child.remove();
  349. }
  350. });
  351. }
  352. //# sourceMappingURL=normalize-and-load-metadata.js.map