Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bun plugin: plugin build.onLoad loads other plugins when it shouldn't (?) and the rest of build.onLoad plugins are not called. #9373

Open
hambergerpls opened this issue Mar 12, 2024 · 1 comment
Labels
bug Something isn't working bundler Something to do with the bundler

Comments

@hambergerpls
Copy link

hambergerpls commented Mar 12, 2024

What version of Bun is running?

1.0.30+1424a196f

What platform is your computer?

Linux 5.15.146.1-microsoft-standard-WSL2 x86_64 x86_64

What steps can reproduce the bug?

// plugin-1.ts
import { plugin } from "bun";
plugin({
	name: "plugin-1",
	setup(build) {
		console.log("hello world #plugin-1 from setup");
		build.onLoad({ filter: /\.(ts|tsx)$/ }, async (args) => {
			console.log("hello world #plugin-1 from onLoad");
                        console.log(args.path);
			return {
				contents: await Bun.file(args.path).text(),
			};
		});
	},
});

// plugin-2.ts
import { plugin } from "bun";
plugin({
	name: "plugin-2",
	setup(build) {
		console.log("hello world #plugin-2 from setup");
		build.onLoad({ filter: /\.(ts|tsx)$/ }, async (args) => {
			console.log("hello world #plugin-2 from onLoad");
                        console.log(args.path);
			return {
				contents: await Bun.file(args.path).text(),
			};
		});
	},
});

// plugin-3.ts
import { plugin } from "bun";
plugin({
	name: "plugin-3",
	setup(build) {
		console.log("hello world #plugin-3 from setup");
		build.onLoad({ filter: /\.(ts|tsx)$/ }, async (args) => {
			console.log("hello world #plugin-3 from onLoad");
                        console.log(args.path);
			return {
				contents: await Bun.file(args.path).text(),
			};
		});
	},
});

// bunfig.toml
preload = ["./plugin-1.ts", "./plugin-2.ts", "./plugin-3.ts"]


// preload-test-file1.ts
import { foo } from "preload-test-file2"
console.log("Hello, world!");
foo();

// preload-test-file2.ts
export const foo = () => console.log("Foo!");

What is the expected behavior?

Not sure if it should either be (1)

//run: bun preload-test-file1.ts
hello world #plugin-1 from setup
hello world #plugin-2 from setup
hello world #plugin-3 from setup
hello world #plugin-1 from onLoad
/home/user/repo/bun-plugin-test/preload-test-file1.ts
hello world #plugin-2 from onLoad
/home/user/repo/bun-plugin-test/preload-test-file1.ts
hello world #plugin-3 from onLoad
/home/user/repo/bun-plugin-test/preload-test-file1.ts
hello world #plugin-1 from onLoad
/home/user/repo/bun-plugin-test/preload-test-file2.ts
hello world #plugin-2 from onLoad
/home/user/repo/bun-plugin-test/preload-test-file2.ts
hello world #plugin-3 from onLoad
/home/user/repo/bun-plugin-test/preload-test-file2.ts
Hello, world!
Foo!

or (2)

//run: bun preload-test-file1.ts
hello world #plugin-1 from setup
hello world #plugin-1 from onLoad
/home/user/repo/bun-plugin-test/preload-test-file1.ts
hello world #plugin-1 from onLoad
/home/user/repo/bun-plugin-test/preload-test-file2.ts
hello world #plugin-2 from setup
hello world #plugin-2 from onLoad
/home/user/repo/bun-plugin-test/preload-test-file1.ts
hello world #plugin-2 from onLoad
/home/user/repo/bun-plugin-test/preload-test-file2.ts
hello world #plugin-3 from setup
hello world #plugin-3 from onLoad
/home/user/repo/bun-plugin-test/preload-test-file1.ts
hello world #plugin-3 from onLoad
/home/user/repo/bun-plugin-test/preload-test-file2.ts
Hello, world!
Foo!

What do you see instead?

//run: bun preload-test-file1.ts
hello world #plugin-1 from setup
hello world #plugin-1 from onLoad
/home/user/repo/bun-plugin-test/plugin2.ts
hello world #plugin-2 from setup
hello world #plugin-1 from onLoad
/home/user/repo/bun-plugin-test/preload-test-file1.ts
hello world #plugin-1 from onLoad
/home/user/repo/bun-plugin-test/preload-test-file2.ts
Hello, world!
Foo!

Additional information

Not sure what is supposed to be the intended behaviour. Is it supposed to call all plugin setup() first before executing the onLoad() for each loading, or call setup() after every file is loaded by the plugin subsequently.

The build.onLoad is also called for plugin-2 when it shouldn't?

@hambergerpls hambergerpls added the bug Something isn't working label Mar 12, 2024
@hambergerpls
Copy link
Author

Also noticed that this test is commented:

// itBundled("plugin/ManyPlugins", ({ root }) => {
// const pluginCount = 4000;
// let resolveCount = 0;
// let loadCount = 0;
// return {
// files: {
// "index.ts": /* ts */ `
// import { foo as foo1 } from "plugin1:file";
// import { foo as foo2 } from "plugin4000:file";
// console.log(foo1, foo2);
// `,
// },
// plugins: Array.from({ length: pluginCount }).map((_, i) => ({
// name: `${i}`,
// setup(builder) {
// builder.onResolve({ filter: new RegExp(`^plugin${i}:file$`) }, args => {
// resolveCount++;
// return {
// path: `plugin${i}:file`,
// namespace: `plugin${i}`,
// };
// });
// builder.onLoad({ filter: new RegExp(`^plugin${i}:file$`), namespace: `plugin${i}` }, args => {
// loadCount++;
// return {
// contents: `export const foo = ${i};`,
// loader: "js",
// };
// });
// },
// })),
// run: {
// stdout: `${pluginCount - 1} ${pluginCount - 1}`,
// },
// onAfterBundle(api) {
// expect(resolveCount).toBe(pluginCount * 2);
// expect(loadCount).toBe(pluginCount);
// },
// };
// });

Any particular reasons?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working bundler Something to do with the bundler
Projects
None yet
Development

No branches or pull requests

2 participants