oven-sh / bun

Incredibly fast JavaScript runtime, bundler, test runner, and package manager – all in one
https://bun.sh
Other
73.48k stars 2.71k forks source link

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 6 months ago

hambergerpls commented 6 months ago

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 commented 6 months ago

Also noticed that this test is commented: https://github.com/oven-sh/bun/blob/840f1b400639793c4935a5f41903a93b17d53a13/test/bundler/bundler_plugin.test.ts#L694-L733

Any particular reasons?