mirror of
https://github.com/vercel-labs/scriptc.git
synced 2026-10-25 06:24:27 +08:00
64 lines
2.0 KiB
JavaScript
64 lines
2.0 KiB
JavaScript
// Lazy require() edges, exercised differentially against Node: every
|
|
// require here targets a module that DOES NOT EXIST, so under Node the
|
|
// package loads fine and each require throws MODULE_NOT_FOUND at the
|
|
// CALL — scriptc must build the same graph (no build error) and throw
|
|
// byte-identical errors at the same calls. The catch happens in here (in
|
|
// the package, island-side) so the error's own properties — message with
|
|
// the Require stack, code, requireStack — print without crossing the
|
|
// island boundary.
|
|
"use strict";
|
|
const inner = require("./inner.js");
|
|
|
|
const fmt = (e) =>
|
|
e.name + "@" + e.code + "@" + JSON.stringify(e.message) + "@" + JSON.stringify(e.requireStack);
|
|
|
|
// A bare specifier no node_modules anywhere provides.
|
|
exports.probeMissingPkg = () => {
|
|
try {
|
|
require("left-padz");
|
|
return "loaded";
|
|
} catch (e) {
|
|
return fmt(e);
|
|
}
|
|
};
|
|
|
|
// A relative path that resolves to nothing — Node reports the specifier
|
|
// as written, not a resolved path.
|
|
exports.probeMissingRel = () => {
|
|
try {
|
|
require("./nope");
|
|
return "loaded";
|
|
} catch (e) {
|
|
return fmt(e);
|
|
}
|
|
};
|
|
|
|
// The deep chain: the failing require lives in inner.js — the Require
|
|
// stack lists inner.js, then index.js (first-requirer parent chain).
|
|
exports.probeDeep = () => {
|
|
try {
|
|
inner.go();
|
|
return "loaded";
|
|
} catch (e) {
|
|
return fmt(e);
|
|
}
|
|
};
|
|
|
|
// Present but never called: its mere existence must not break the build
|
|
// or the run (Node never fails it either — nothing evaluates the call).
|
|
exports.neverCalled = () => require("never-there");
|
|
|
|
// The TRANSITIVE half: ./cjs-sub.js itself loads fine as a file, but its
|
|
// TOP-LEVEL require targets a ghost — Node evaluates the sub-module at
|
|
// THIS call and the failure surfaces here, with the Require stack listing
|
|
// the sub-module first (it is the requiring module), then this file.
|
|
// Repeat calls re-evaluate (the failed module leaves no cache entry).
|
|
exports.probeChain = () => {
|
|
try {
|
|
require("./cjs-sub.js");
|
|
return "loaded";
|
|
} catch (e) {
|
|
return fmt(e);
|
|
}
|
|
};
|