loader.mjs 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import { pathToFileURL } from 'url';
  2. import { readFile } from 'fs/promises';
  3. import { i as isNodeBuiltin, d as normalizeModuleId, h as hasCJSSyntax } from './chunk-vite-node-utils.473cd0b2.mjs';
  4. import { g as getWorkerState } from './chunk-mock-date.2917be60.mjs';
  5. import './chunk-constants.71e8a211.mjs';
  6. import 'tty';
  7. import 'path';
  8. import 'module';
  9. import 'fs';
  10. import 'assert';
  11. import 'util';
  12. import 'local-pkg';
  13. var ModuleFormat = /* @__PURE__ */ ((ModuleFormat2) => {
  14. ModuleFormat2["Builtin"] = "builtin";
  15. ModuleFormat2["Commonjs"] = "commonjs";
  16. ModuleFormat2["Json"] = "json";
  17. ModuleFormat2["Module"] = "module";
  18. ModuleFormat2["Wasm"] = "wasm";
  19. return ModuleFormat2;
  20. })(ModuleFormat || {});
  21. const ESM_RE = /([\s;}]|^)(import[\w,{}\s*]*from|import\s*['"*{]|export\b\s*(?:[*{]|default|class|type|function|const|var|let|async function)|import\.meta\b)/m;
  22. function hasESMSyntax(code) {
  23. return ESM_RE.test(code);
  24. }
  25. const cache = /* @__PURE__ */ new Map();
  26. const getPotentialSource = async (filepath, result) => {
  27. var _a;
  28. if (!result.url.startsWith("file://") || result.format === "module")
  29. return null;
  30. let source = (_a = cache.get(result.url)) == null ? void 0 : _a.source;
  31. if (source == null)
  32. source = await readFile(filepath, "utf8");
  33. return source;
  34. };
  35. const detectESM = (url, source) => {
  36. const cached = cache.get(url);
  37. if (cached)
  38. return cached.isPseudoESM;
  39. if (!source)
  40. return false;
  41. return hasESMSyntax(source) && !hasCJSSyntax(source);
  42. };
  43. const resolve = async (url, context, next) => {
  44. const { parentURL } = context;
  45. const state = getWorkerState();
  46. const resolver = state == null ? void 0 : state.rpc.resolveId;
  47. if (!parentURL || isNodeBuiltin(url) || !resolver)
  48. return next(url, context, next);
  49. const id = normalizeModuleId(url);
  50. const importer = normalizeModuleId(parentURL);
  51. const resolved = await resolver(id, importer);
  52. let result;
  53. let filepath;
  54. if (resolved) {
  55. const resolvedUrl = pathToFileURL(resolved.id).toString();
  56. filepath = resolved.id;
  57. result = {
  58. url: resolvedUrl,
  59. shortCircuit: true
  60. };
  61. } else {
  62. const { url: resolvedUrl, format } = await next(url, context, next);
  63. filepath = new URL(resolvedUrl).pathname;
  64. result = {
  65. url: resolvedUrl,
  66. format,
  67. shortCircuit: true
  68. };
  69. }
  70. const source = await getPotentialSource(filepath, result);
  71. const isPseudoESM = detectESM(result.url, source);
  72. if (typeof source === "string")
  73. cache.set(result.url, { isPseudoESM, source });
  74. if (isPseudoESM)
  75. result.format = ModuleFormat.Module;
  76. return result;
  77. };
  78. const load = async (url, context, next) => {
  79. const result = await next(url, context, next);
  80. const cached = cache.get(url);
  81. if ((cached == null ? void 0 : cached.isPseudoESM) && result.format !== "module") {
  82. return {
  83. source: cached.source,
  84. format: ModuleFormat.Module
  85. };
  86. }
  87. return result;
  88. };
  89. export { load, resolve };