Files
bun.sh/test/regression/issue/bundler-plugin-onresolve-entrypoint.test.ts
Alistair Smith 54b90213eb fix: support virtual entrypoints in onResolve() (#22144)
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
2025-08-26 16:51:41 -07:00

130 lines
4.0 KiB
TypeScript

import { describe } from "bun:test";
import path from "node:path";
import { itBundled } from "../../bundler/expectBundled";
describe("bundler plugin onResolve entry point", () => {
itBundled("onResolve-entrypoint-modification", {
files: {
"entry.js": `console.log("original entry");`,
},
plugins(build) {
const resolvedPaths = new Map();
build.onResolve({ filter: /.*/ }, args => {
if (args.kind === "entry-point-build" || args.kind === "entry-point-run") {
const modifiedPath = args.path + ".modified";
resolvedPaths.set(modifiedPath, args.path);
console.log(`onResolve: ${args.path} -> ${modifiedPath} (${args.kind})`);
return { path: modifiedPath };
}
});
build.onLoad({ filter: /.*/ }, args => {
console.log(`onLoad: ${args.path}`);
if (args.path.endsWith(".modified")) {
return {
contents: 'console.log("SUCCESS: Modified entry loaded");',
loader: "js",
};
}
for (const [modified, original] of resolvedPaths) {
if (args.path === original) {
return {
contents: 'console.log("BUG: Original entry loaded");',
loader: "js",
};
}
}
return {
contents: 'console.log("Other file loaded");',
loader: "js",
};
});
},
run: {
stdout: "SUCCESS: Modified entry loaded",
},
});
itBundled("onResolve-import-modification", {
files: {
"entry.js": `import "./foo.magic";`,
"foo.js": `console.log("foo loaded");`,
},
plugins(build) {
build.onResolve({ filter: /\.magic$/ }, args => {
const newPath = args.path.replace(/\.magic$/, ".js");
const resolvedPath = path.join(path.dirname(args.importer), newPath);
console.log(`onResolve: ${args.path} -> ${resolvedPath} (${args.kind})`);
return { path: resolvedPath };
});
build.onLoad({ filter: /foo\.js$/ }, args => {
console.log(`onLoad: ${args.path}`);
if (args.path.endsWith("foo.js")) {
return {
contents: 'console.log("SUCCESS: foo.js loaded via onResolve");',
loader: "js",
};
}
});
},
run: {
stdout: "SUCCESS: foo.js loaded via onResolve",
},
});
itBundled("onResolve-multiple-entrypoints", {
files: {
"entry1.js": `console.log("entry1");`,
"entry2.js": `console.log("entry2");`,
"entry3.js": `console.log("entry3");`,
},
entryPoints: ["entry1.js", "entry2.js", "entry3.js"],
plugins(build) {
const entryModifications = new Map();
build.onResolve({ filter: /.*/ }, args => {
if (args.kind?.includes("entry-point")) {
const modified = args.path + ".modified";
entryModifications.set(args.path, modified);
console.log(`onResolve: ${args.path} -> ${modified} (${args.kind})`);
return { path: modified };
}
});
build.onLoad({ filter: /.*/ }, args => {
console.log(`onLoad: ${args.path}`);
if (args.path.endsWith(".modified")) {
const baseName = path.basename(args.path, ".js.modified");
return {
contents: `console.log("SUCCESS: ${baseName} modified");`,
loader: "js",
};
}
for (const [original] of entryModifications) {
if (args.path === original) {
const entryName = path.basename(args.path, ".js");
return {
contents: `console.log("BUG: ${entryName} original loaded");`,
loader: "js",
};
}
}
});
},
outputPaths: ["out/entry1.js", "out/entry2.js", "out/entry3.js"],
run: [
{ file: "out/entry1.js", stdout: "SUCCESS: entry1 modified" },
{ file: "out/entry2.js", stdout: "SUCCESS: entry2 modified" },
{ file: "out/entry3.js", stdout: "SUCCESS: entry3 modified" },
],
});
});