aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorGravatar Jarred Sumner <jarred@jarredsumner.com> 2023-09-29 16:34:20 -0700
committerGravatar GitHub <noreply@github.com> 2023-09-29 16:34:20 -0700
commita97847a49475e774695c38cff07a71eadf608c05 (patch)
tree26867f9be2eddaa0b752189a27810ed4db6ed902 /test
parenteddb0078b5c9ff49bf67c0f1b1c2c623f0480b77 (diff)
downloadbun-a97847a49475e774695c38cff07a71eadf608c05.tar.gz
bun-a97847a49475e774695c38cff07a71eadf608c05.tar.zst
bun-a97847a49475e774695c38cff07a71eadf608c05.zip
Implement virtual module support in `Bun.plugin` (#6167)
* Add support for `build.module` in `Bun.plugin` * Another test * Update docs * Update isBuiltinModule.cpp --------- Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> Co-authored-by: Dylan Conway <dylan.conway567@gmail.com>
Diffstat (limited to 'test')
-rw-r--r--test/bundler/bun-build-api.test.ts23
-rw-r--r--test/js/bun/plugin/module-plugins.ts38
-rw-r--r--test/js/bun/plugin/plugins.test.ts78
3 files changed, 139 insertions, 0 deletions
diff --git a/test/bundler/bun-build-api.test.ts b/test/bundler/bun-build-api.test.ts
index 3c2308d94..d1f1a10b8 100644
--- a/test/bundler/bun-build-api.test.ts
+++ b/test/bundler/bun-build-api.test.ts
@@ -290,4 +290,27 @@ describe("Bun.build", () => {
// depends on the ws package in the test/node_modules.
expect(content).toContain("var websocket = __toESM(require_websocket(), 1);");
});
+
+ test("module() throws error", async () => {
+ expect(() =>
+ Bun.build({
+ entrypoints: [join(import.meta.dir, "./fixtures/trivial/bundle-ws.ts")],
+ plugins: [
+ {
+ name: "test",
+ setup: b => {
+ b.module("ad", () => {
+ return {
+ exports: {
+ hello: "world",
+ },
+ loader: "object",
+ };
+ });
+ },
+ },
+ ],
+ }),
+ ).toThrow();
+ });
});
diff --git a/test/js/bun/plugin/module-plugins.ts b/test/js/bun/plugin/module-plugins.ts
new file mode 100644
index 000000000..d6034c5df
--- /dev/null
+++ b/test/js/bun/plugin/module-plugins.ts
@@ -0,0 +1,38 @@
+import { plugin } from "bun";
+plugin({
+ name: "i am virtual!",
+ setup(builder) {
+ builder.module("my-virtual-module-async", async () => {
+ // check
+ await Bun.sleep(1);
+ return {
+ exports: {
+ hello: "world",
+ },
+ loader: "object",
+ };
+ });
+
+ builder.module("my-virtual-module-sync", () => {
+ return {
+ exports: {
+ hello: "world",
+ },
+ loader: "object",
+ };
+ });
+
+ builder.onLoad({ filter: /.*/, namespace: "rejected-promise" }, async ({ path }) => {
+ throw new Error("Rejected Promise");
+ });
+
+ builder.onResolve({ filter: /.*/, namespace: "rejected-promise2" }, ({ path }) => ({
+ namespace: "rejected-promise2",
+ path,
+ }));
+
+ builder.onLoad({ filter: /.*/, namespace: "rejected-promise2" }, ({ path }) => {
+ return Promise.reject(new Error("Rejected Promise"));
+ });
+ },
+});
diff --git a/test/js/bun/plugin/plugins.test.ts b/test/js/bun/plugin/plugins.test.ts
index c2827f600..2d3cfa1fa 100644
--- a/test/js/bun/plugin/plugins.test.ts
+++ b/test/js/bun/plugin/plugins.test.ts
@@ -185,6 +185,7 @@ plugin({
// This is to test that it works when imported from a separate file
import "../../third_party/svelte";
+import "./module-plugins";
describe("require", () => {
it("SSRs `<h1>Hello world!</h1>` with Svelte", () => {
@@ -210,6 +211,83 @@ describe("require", () => {
});
});
+describe("module", () => {
+ it("throws with require()", () => {
+ expect(() => require("my-virtual-module-async")).toThrow();
+ });
+
+ it("async module works with async import", async () => {
+ // @ts-expect-error
+ const { hello } = await import("my-virtual-module-async");
+
+ expect(hello).toBe("world");
+ delete require.cache["my-virtual-module-async"];
+ });
+
+ it("sync module module works with require()", async () => {
+ const { hello } = require("my-virtual-module-sync");
+
+ expect(hello).toBe("world");
+ delete require.cache["my-virtual-module-sync"];
+ });
+
+ it("sync module module works with require.resolve()", async () => {
+ expect(require.resolve("my-virtual-module-sync")).toBe("my-virtual-module-sync");
+ delete require.cache["my-virtual-module-sync"];
+ });
+
+ it("sync module module works with import", async () => {
+ // @ts-expect-error
+ const { hello } = await import("my-virtual-module-sync");
+
+ expect(hello).toBe("world");
+ delete require.cache["my-virtual-module-sync"];
+ });
+
+ it("modules are overridable", async () => {
+ // @ts-expect-error
+ let { hello, there } = await import("my-virtual-module-sync");
+ expect(there).toBeUndefined();
+ expect(hello).toBe("world");
+
+ Bun.plugin({
+ setup(builder) {
+ builder.module("my-virtual-module-sync", () => ({
+ exports: {
+ there: true,
+ },
+ loader: "object",
+ }));
+ },
+ });
+
+ {
+ const { there, hello } = require("my-virtual-module-sync");
+ expect(there).toBe(true);
+ expect(hello).toBeUndefined();
+ }
+
+ Bun.plugin({
+ setup(builder) {
+ builder.module("my-virtual-module-sync", () => ({
+ exports: {
+ yo: true,
+ },
+ loader: "object",
+ }));
+ },
+ });
+
+ {
+ // @ts-expect-error
+ const { there, hello, yo } = await import("my-virtual-module-sync");
+ expect(yo).toBe(true);
+ expect(hello).toBeUndefined();
+ expect(there).toBeUndefined();
+ }
+ });
+});
+
describe("dynamic import", () => {
it("SSRs `<h1>Hello world!</h1>` with Svelte", async () => {
const { default: App }: any = await import("./hello.svelte");