@lifo-sh/node-runner 0.1.19 → 0.1.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -217,11 +217,20 @@ function resolveExportsCondition(value) {
217
217
  if (typeof value === "string") return value;
218
218
  if (value && typeof value === "object" && !Array.isArray(value)) {
219
219
  const cond = value;
220
- if (typeof cond.require === "string") return cond.require;
221
- if (typeof cond.default === "string") return cond.default;
222
- if (typeof cond.import === "string") return cond.import;
220
+ if (cond.require != null) {
221
+ const r = resolveExportsCondition(cond.require);
222
+ if (r) return r;
223
+ }
224
+ if (cond.default != null) {
225
+ const r = resolveExportsCondition(cond.default);
226
+ if (r) return r;
227
+ }
228
+ if (cond.import != null) {
229
+ const r = resolveExportsCondition(cond.import);
230
+ if (r) return r;
231
+ }
223
232
  for (const key of Object.keys(cond)) {
224
- if (key === "types") continue;
233
+ if (key === "types" || key === "require" || key === "default" || key === "import") continue;
225
234
  const nested = resolveExportsCondition(cond[key]);
226
235
  if (nested) return nested;
227
236
  }
@@ -779,7 +788,7 @@ function transformEsmToCjs(source) {
779
788
  }).filter((s) => s).join(", ");
780
789
  return `
781
790
  ${indent}${cjsDecl(tmp)} ${tmp} = require(${mod});
782
- ${indent}${cjsDecl(defaultName)} ${defaultName} = ${tmp}.default || ${tmp};
791
+ ${indent}${cjsDecl(defaultName)} ${defaultName} = ${tmp} && ${tmp}.__esModule && 'default' in ${tmp} ? ${tmp}.default : ${tmp};
783
792
  ${indent}const { ${mapped} } = ${tmp};`;
784
793
  }
785
794
  );
@@ -788,7 +797,7 @@ ${indent}const { ${mapped} } = ${tmp};`;
788
797
  (_match, indent, defaultName, nsName, mod) => {
789
798
  return `
790
799
  ${indent}${cjsDecl(nsName)} ${nsName} = require(${mod});
791
- ${indent}${cjsDecl(defaultName)} ${defaultName} = ${nsName}.default || ${nsName};`;
800
+ ${indent}${cjsDecl(defaultName)} ${defaultName} = ${nsName} && ${nsName}.__esModule && 'default' in ${nsName} ? ${nsName}.default : ${nsName};`;
792
801
  }
793
802
  );
794
803
  result = result.replace(
@@ -816,8 +825,12 @@ ${indent}${cjsDecl(name)} ${name} = require(${mod});`
816
825
  );
817
826
  result = result.replace(
818
827
  /(?:^|\n)([ \t]*)import\s+([\w$]+)\s+from\s*(['"][^'"]+['"])[ \t]*;?/g,
819
- (_match, indent, name, mod) => `
820
- ${indent}${cjsDecl(name)} ${name} = require(${mod});`
828
+ (_match, indent, name, mod) => {
829
+ const tmp = "__imp_" + Math.random().toString(36).slice(2, 8);
830
+ return `
831
+ ${indent}const ${tmp} = require(${mod});
832
+ ${indent}${cjsDecl(name)} ${name} = ${tmp} && ${tmp}.__esModule && 'default' in ${tmp} ? ${tmp}.default : ${tmp};`;
833
+ }
821
834
  );
822
835
  result = result.replace(
823
836
  /(?:^|\n)([ \t]*)import\s*(['"][^'"]+['"])[ \t]*;?/g,
@@ -963,19 +976,16 @@ ${indent}${decl}`;
963
976
  result += "\n" + trailingExports.join("\n");
964
977
  }
965
978
  result = unmaskStringLiterals(result, literals);
966
- return result;
979
+ return 'Object.defineProperty(exports, "__esModule", { value: true });\n' + result;
967
980
  }
968
981
  function transformToCjs(source, filename, vfs) {
969
982
  if (shouldTreatAsEsm(source, filename, vfs)) {
970
- const code2 = stripViteHmr(safetyNetImportMeta(transformEsmToCjs(source)));
983
+ const code2 = safetyNetImportMeta(transformEsmToCjs(source));
971
984
  return { code: code2, wasEsm: true };
972
985
  }
973
- const code = stripViteHmr(safetyNetImportMeta(replaceImportMetaInCode(source)));
986
+ const code = safetyNetImportMeta(replaceImportMetaInCode(source));
974
987
  return { code, wasEsm: false };
975
988
  }
976
- function stripViteHmr(code) {
977
- return code.replace(/^[ \t]*__importMeta\.hot\b.*$/gm, "").replace(/^[ \t]*import\.meta\.hot\b.*$/gm, "");
978
- }
979
989
  function safetyNetImportMeta(code) {
980
990
  if (!code.includes("import.meta")) return code;
981
991
  return code.replace(/\bimport\.meta\.url\b/g, "__importMetaUrl").replace(/\bimport\.meta\.dirname\b/g, "__dirname").replace(/\bimport\.meta\.filename\b/g, "__filename").replace(/\bimport\.meta\.require\b/g, "require").replace(/\bimport\.meta\.resolve\b/g, "__importMetaResolve").replace(/\bimport\.meta\b/g, "__importMeta");
@@ -983,6 +993,20 @@ function safetyNetImportMeta(code) {
983
993
 
984
994
  // src/runner.ts
985
995
  var textEncoder = new TextEncoder();
996
+ function createHotStub() {
997
+ const noop = () => {
998
+ };
999
+ return {
1000
+ accept: noop,
1001
+ prune: noop,
1002
+ dispose: noop,
1003
+ decline: noop,
1004
+ invalidate: noop,
1005
+ on: noop,
1006
+ send: noop,
1007
+ data: {}
1008
+ };
1009
+ }
986
1010
  var JS_EXTENSIONS = /* @__PURE__ */ new Set([
987
1011
  ".js",
988
1012
  ".ts",
@@ -1207,9 +1231,8 @@ var NodeRunner = class {
1207
1231
  }
1208
1232
  const ext = resolved.path.slice(resolved.path.lastIndexOf("."));
1209
1233
  if (ext && !JS_EXTENSIONS.has(ext)) {
1210
- const clean = resolved.source.replace(/^[ \t]*import\.meta\.hot\b.*$/gm, "").replace(/^[ \t]*__importMeta\.hot\b.*$/gm, "");
1211
- self.moduleCache.set(resolved.path, clean);
1212
- return clean;
1234
+ self.moduleCache.set(resolved.path, resolved.source);
1235
+ return resolved.source;
1213
1236
  }
1214
1237
  const moduleExports2 = {};
1215
1238
  const moduleObj2 = { exports: moduleExports2 };
@@ -1221,7 +1244,7 @@ var NodeRunner = class {
1221
1244
  cleanSource = cjsSource;
1222
1245
  const factory = compileModule(cleanSource, resolved.path, self.stderr);
1223
1246
  const importMetaUrl = "file://" + resolved.path;
1224
- const importMeta = { url: importMetaUrl, dirname: moduleDir, filename: resolved.path };
1247
+ const importMeta = { url: importMetaUrl, dirname: moduleDir, filename: resolved.path, hot: createHotStub() };
1225
1248
  const importMetaResolve = (specifier) => {
1226
1249
  throw new Error(`import.meta.resolve('${specifier}') is not supported`);
1227
1250
  };
@@ -1324,7 +1347,7 @@ ${cleanMainSource}
1324
1347
  const moduleObj = { exports: moduleExports };
1325
1348
  const global = { process: processObj, Buffer: BufferClass, console: scriptConsole };
1326
1349
  const mainImportMetaUrl = "file://" + absPath;
1327
- const mainImportMeta = { url: mainImportMetaUrl, dirname: scriptDir, filename: absPath };
1350
+ const mainImportMeta = { url: mainImportMetaUrl, dirname: scriptDir, filename: absPath, hot: createHotStub() };
1328
1351
  const mainImportMetaResolve = (specifier) => {
1329
1352
  throw new Error(`import.meta.resolve('${specifier}') is not supported`);
1330
1353
  };