Skip to content

Commit

Permalink
fix: rollup
Browse files Browse the repository at this point in the history
  • Loading branch information
magne4000 committed Jul 16, 2024
1 parent 20cc163 commit ace5ca3
Show file tree
Hide file tree
Showing 4 changed files with 331 additions and 80 deletions.
9 changes: 5 additions & 4 deletions packages/universal-middleware/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,14 @@
"test": "vitest run"
},
"dependencies": {
"esbuild": "^0.23.0",
"@universal-middleware/express": "^0.1.0",
"@universal-middleware/hattip": "^0.1.0",
"@universal-middleware/hono": "^0.1.1"
"@universal-middleware/hono": "^0.1.1",
"esbuild": "^0.23.0"
},
"devDependencies": {
"@rollup/plugin-node-resolve": "^15.2.3",
"@rollup/plugin-typescript": "^11.1.6",
"@universal-middleware/core": "^0.1.1",
"rimraf": "^6.0.1",
"rollup": "^4.18.1",
Expand All @@ -105,11 +106,11 @@
"vitest": "^2.0.3"
},
"peerDependencies": {
"@rollup/plugin-node-resolve": "^15",
"esbuild": "*",
"rollup": "^4",
"vite": ">=5",
"webpack": "^4 || ^5",
"@rollup/plugin-node-resolve": "^15"
"webpack": "^4 || ^5"
},
"peerDependenciesMeta": {
"webpack": {
Expand Down
16 changes: 12 additions & 4 deletions packages/universal-middleware/src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,12 @@ function appendVirtualInputs(
});
}

function load(id: string) {
function load(id: string, resolve?: (handler: string, type: string) => string) {
const [, , server, type, handler] = id.split(":");

const fn = type === "handler" ? "createHandler" : "createMiddleware";
const code = `import { ${fn} } from "@universal-middleware/${server}";
import ${type} from "${handler}";
import ${type} from "${resolve ? resolve(handler, type) : handler}";
export default ${fn}(${type});
`;
return { code };
Expand All @@ -120,13 +120,19 @@ const universalMiddleware = createUnplugin((options?: Options) => {
name: namespace,
enforce: "post",
rollup: {
async options(opts) {
options(opts) {
const normalizedInput = normalizeInput(opts.input);
if (normalizedInput) {
opts.input = normalizedInput;
appendVirtualInputs(opts.input, options?.servers);
}
},
// outputOptions(opts) {
// opts.entryFileNames = (chunkInfo) => {
// console.log(chunkInfo);
// return chunkInfo.name + '-[hash]';
// };
// },
},

esbuild: {
Expand Down Expand Up @@ -224,7 +230,9 @@ const universalMiddleware = createUnplugin((options?: Options) => {
return id.startsWith(namespace);
},

load,
load(id) {
return load(id, (handler, type) => `${handler}?${type}`);
},
};
});

Expand Down
190 changes: 125 additions & 65 deletions packages/universal-middleware/test/rollup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,124 +3,184 @@
import { describe, expect, it } from "vitest";
import { type OutputChunk, rollup, type RollupOutput } from "rollup";
import { nodeResolve } from "@rollup/plugin-node-resolve";
import typescript from "@rollup/plugin-typescript";
import unplugin from "../src/build";
import { parse } from "node:path";

describe("rollup", () => {
it("generates all server files (string input)", async () => {
const entry = "test/files/folder1/handler.ts";
const result = await rollup({
input: "test/handler.ts?handler",
plugins: [unplugin.rollup(), nodeResolve()],
input: entry + "?handler",
plugins: [
unplugin.rollup(),
nodeResolve(),
typescript({
sourceMap: false,
}),
],
onwarn(warning) {
throw new Error(warning.message);
},
});

const gen = await result.generate({});

const handler = gen.output.find(
(f: any) => f.facadeModuleId === "test/handler.ts",
) as OutputChunk | undefined;
expect(handler?.name).toEqual("handler");
expect(
gen.output.filter((f) => f.type === "chunk" && f.isEntry),
).toHaveLength(expectNbOutput(1));

const handler = gen.output.find((f: any) => f.facadeModuleId === entry) as
| OutputChunk
| undefined;
expect(handler?.name).toEqual("test/files/folder1/handler");

testRollupHandlers(gen);
testRollupOutput(gen, "handler", entry);
});

it("generates all server files (object input)", async () => {
const entry1 = "test/files/folder1/handler.ts";
const entry2 = "test/files/middleware.ts";
const result = await rollup({
input: {
h: "test/handler.ts?handler",
m: "test/middleware.ts?middleware",
h: entry1 + "?handler",
m: entry2 + "?middleware",
},
plugins: [unplugin.rollup(), nodeResolve()],
plugins: [
unplugin.rollup(),
nodeResolve(),
typescript({
sourceMap: false,
}),
],
onwarn(warning) {
throw new Error(warning.message);
},
});

const gen = await result.generate({});

const handler = gen.output.find(
(f: any) => f.facadeModuleId === "test/handler.ts",
) as OutputChunk | undefined;
expect(
gen.output.filter((f) => f.type === "chunk" && f.isEntry),
).toHaveLength(expectNbOutput(2));

const handler = gen.output.find((f: any) => f.facadeModuleId === entry1) as
| OutputChunk
| undefined;
expect(handler?.name).toEqual("h");

const middleware = gen.output.find(
(f: any) => f.facadeModuleId === "test/middleware.ts",
(f: any) => f.facadeModuleId === entry2,
) as OutputChunk | undefined;
expect(middleware?.name).toEqual("m");

testRollupHandlers(gen);
testRollupMiddlewares(gen);
testRollupOutput(gen, "handler", entry1);
testRollupOutput(gen, "middleware", entry2);
});

it("generates all server files (array input)", async () => {
const entry1 = "test/files/folder1/handler.ts";
const entry2 = "test/files/middleware.ts";
const result = await rollup({
input: ["test/handler.ts?handler", "test/middleware.ts?middleware"],
plugins: [unplugin.rollup(), nodeResolve()],
input: [entry1 + "?handler", entry2 + "?middleware"],
plugins: [
unplugin.rollup(),
nodeResolve(),
typescript({
sourceMap: false,
}),
],
onwarn(warning) {
throw new Error(warning.message);
},
});

const gen = await result.generate({});

const handler = gen.output.find(
(f: any) => f.facadeModuleId === "test/handler.ts",
) as OutputChunk | undefined;
expect(handler?.name).toEqual("handler");
expect(
gen.output.filter((f) => f.type === "chunk" && f.isEntry),
).toHaveLength(expectNbOutput(2));

const handler = gen.output.find((f: any) => f.facadeModuleId === entry1) as
| OutputChunk
| undefined;
expect(handler?.name).toEqual("test/files/folder1/handler");

const middleware = gen.output.find(
(f: any) => f.facadeModuleId === "test/middleware.ts",
(f: any) => f.facadeModuleId === entry2,
) as OutputChunk | undefined;
expect(middleware?.name).toEqual("middleware");
expect(middleware?.name).toEqual("test/files/middleware");

testRollupHandlers(gen);
testRollupMiddlewares(gen);
testRollupOutput(gen, "handler", entry1);
testRollupOutput(gen, "middleware", entry2);
});
});

function testRollupHandlers(gen: RollupOutput) {
const hattip = gen.output.find(
(f: any) =>
f.facadeModuleId ===
"virtual:universal-middleware:hattip:handler:test/handler.ts",
) as OutputChunk | undefined;
expect(hattip?.name).toEqual("universal-hattip-handler");
it("generates all server files (multiple handlers)", async () => {
const entry1 = "test/files/folder1/handler.ts";
const entry2 = "test/files/folder2/handler.ts";
const result = await rollup({
input: [entry1 + "?handler", entry2 + "?handler"],
plugins: [
unplugin.rollup(),
nodeResolve(),
typescript({
sourceMap: false,
}),
],
onwarn(warning) {
throw new Error(warning.message);
},
});

const express = gen.output.find(
(f: any) =>
f.facadeModuleId ===
"virtual:universal-middleware:express:handler:test/handler.ts",
) as OutputChunk | undefined;
expect(express?.name).toEqual("universal-express-handler");
const gen = await result.generate({});

const hono = gen.output.find(
(f: any) =>
f.facadeModuleId ===
"virtual:universal-middleware:hono:handler:test/handler.ts",
) as OutputChunk | undefined;
expect(hono?.name).toEqual("universal-hono-handler");
}
expect(
gen.output.filter((f) => f.type === "chunk" && f.isEntry),
).toHaveLength(expectNbOutput(2));

function testRollupMiddlewares(gen: RollupOutput) {
const hattip = gen.output.find(
(f: any) =>
f.facadeModuleId ===
"virtual:universal-middleware:hattip:middleware:test/middleware.ts",
) as OutputChunk | undefined;
expect(hattip?.name).toEqual("universal-hattip-middleware");
const handler1 = gen.output.find(
(f: any) => f.facadeModuleId === entry1,
) as OutputChunk | undefined;
expect(handler1?.name).toEqual("test/files/folder1/handler");

const express = gen.output.find(
(f: any) =>
f.facadeModuleId ===
"virtual:universal-middleware:express:middleware:test/middleware.ts",
) as OutputChunk | undefined;
expect(express?.name).toEqual("universal-express-middleware");
const handler2 = gen.output.find(
(f: any) => f.facadeModuleId === entry2,
) as OutputChunk | undefined;
expect(handler2?.name).toEqual("test/files/folder2/handler");

const hono = gen.output.find(
(f: any) =>
f.facadeModuleId ===
"virtual:universal-middleware:hono:middleware:test/middleware.ts",
testRollupOutput(gen, "handler", entry1);
testRollupOutput(gen, "handler", entry2);
});
});

function testRollupHandler(
gen: RollupOutput,
type: "handler" | "middleware",
server: string,
f: string,
) {
const parsed = parse(f);
const res = gen.output.find(
(file: any) =>
file.facadeModuleId ===
`virtual:universal-middleware:${server}:${type}:${f}`,
) as OutputChunk | undefined;
expect(hono?.name).toEqual("universal-hono-middleware");
expect(res?.name).toEqual(
`${parsed.dir}/universal-${server}-${type}-${parsed.name}`,
);
}

function testRollupOutput(
gen: RollupOutput,
type: "handler" | "middleware",
f: string,
) {
// FIXME hash
testRollupHandler(gen, type, "express", f);
testRollupHandler(gen, type, "hono", f);
testRollupHandler(gen, type, "hattip", f);
}

function expectNbOutput(i: number) {
return i * 4;
}
Loading

0 comments on commit ace5ca3

Please sign in to comment.