on-load.js 862 B

123456789101112131415161718192021222324
  1. const createCjsContents = (variableName) => `module.exports = ${variableName};`;
  2. const a = 0;
  3. const createEsmContents = (variableName, namedExports, defaultExport) => {
  4. const codeElements = defaultExport ? [`export default ${variableName};`] : [];
  5. if (namedExports && namedExports.length) {
  6. const exportNames = [...new Set(namedExports)].join(", ");
  7. codeElements.push(`const { ${exportNames} } = ${variableName};`);
  8. codeElements.push(`export { ${exportNames} };`);
  9. }
  10. return codeElements.join("\n");
  11. };
  12. /**
  13. * Creates value for `OnLoadResult.contents`.
  14. */
  15. export const createContents = (moduleInfo) => {
  16. const { type, varName, namedExports, defaultExport } = moduleInfo;
  17. switch (type) {
  18. case "esm":
  19. return createEsmContents(varName, namedExports, defaultExport);
  20. case "cjs":
  21. return createCjsContents(varName);
  22. }
  23. };