版博士V2.0程序
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import { pathToFileURL, fileURLToPath } from 'node:url';
  2. import { readFile } from 'node:fs/promises';
  3. import { i as isNodeBuiltin, h as hasCJSSyntax } from './vendor-index.bdee400f.js';
  4. import { normalizeModuleId } from 'vite-node/utils';
  5. import './chunk-utils-env.6b856dbf.js';
  6. import { g as getWorkerState } from './chunk-utils-global.fd174983.js';
  7. import 'acorn';
  8. import 'node:module';
  9. import 'node:fs';
  10. import 'pathe';
  11. import 'node:assert';
  12. import 'node:process';
  13. import 'node:path';
  14. import 'node:v8';
  15. import 'node:util';
  16. import 'std-env';
  17. import '@vitest/runner/utils';
  18. import '@vitest/utils';
  19. var ModuleFormat = /* @__PURE__ */ ((ModuleFormat2) => {
  20. ModuleFormat2["Builtin"] = "builtin";
  21. ModuleFormat2["Commonjs"] = "commonjs";
  22. ModuleFormat2["Json"] = "json";
  23. ModuleFormat2["Module"] = "module";
  24. ModuleFormat2["Wasm"] = "wasm";
  25. return ModuleFormat2;
  26. })(ModuleFormat || {});
  27. 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;
  28. function hasESMSyntax(code) {
  29. return ESM_RE.test(code);
  30. }
  31. const cache = /* @__PURE__ */ new Map();
  32. const getPotentialSource = async (filepath, result) => {
  33. var _a;
  34. if (!result.url.startsWith("file://") || result.format === "module")
  35. return null;
  36. let source = (_a = cache.get(result.url)) == null ? void 0 : _a.source;
  37. if (source == null)
  38. source = await readFile(filepath, "utf8");
  39. return source;
  40. };
  41. const detectESM = (url, source) => {
  42. const cached = cache.get(url);
  43. if (cached)
  44. return cached.isPseudoESM;
  45. if (!source)
  46. return false;
  47. return hasESMSyntax(source) && !hasCJSSyntax(source);
  48. };
  49. const resolve = async (url, context, next) => {
  50. const { parentURL } = context;
  51. const state = getWorkerState();
  52. const resolver = state == null ? void 0 : state.rpc.resolveId;
  53. if (!parentURL || isNodeBuiltin(url) || !resolver)
  54. return next(url, context, next);
  55. const id = normalizeModuleId(url);
  56. const importer = normalizeModuleId(parentURL);
  57. const resolved = await resolver(id, importer, state.ctx.environment.name);
  58. let result;
  59. let filepath;
  60. if (resolved) {
  61. const resolvedUrl = pathToFileURL(resolved.id).toString();
  62. filepath = resolved.id;
  63. result = {
  64. url: resolvedUrl,
  65. shortCircuit: true
  66. };
  67. } else {
  68. const { url: resolvedUrl, format } = await next(url, context, next);
  69. filepath = fileURLToPath(resolvedUrl);
  70. result = {
  71. url: resolvedUrl,
  72. format,
  73. shortCircuit: true
  74. };
  75. }
  76. const source = await getPotentialSource(filepath, result);
  77. const isPseudoESM = detectESM(result.url, source);
  78. if (typeof source === "string")
  79. cache.set(result.url, { isPseudoESM, source });
  80. if (isPseudoESM)
  81. result.format = ModuleFormat.Module;
  82. return result;
  83. };
  84. const load = async (url, context, next) => {
  85. const result = await next(url, context, next);
  86. const cached = cache.get(url);
  87. if ((cached == null ? void 0 : cached.isPseudoESM) && result.format !== "module") {
  88. return {
  89. source: cached.source,
  90. format: ModuleFormat.Module
  91. };
  92. }
  93. return result;
  94. };
  95. export { load, resolve };