@anvil-di/anvil-unplugin 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/README.md +83 -0
- package/dist/esbuild.d.ts +5 -0
- package/dist/esbuild.d.ts.map +1 -0
- package/dist/esbuild.js +5 -0
- package/dist/esbuild.js.map +1 -0
- package/dist/index.d.ts +153 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +289 -0
- package/dist/index.js.map +1 -0
- package/dist/rollup.d.ts +5 -0
- package/dist/rollup.d.ts.map +1 -0
- package/dist/rollup.js +5 -0
- package/dist/rollup.js.map +1 -0
- package/dist/rspack.d.ts +5 -0
- package/dist/rspack.d.ts.map +1 -0
- package/dist/rspack.js +5 -0
- package/dist/rspack.js.map +1 -0
- package/dist/vite.d.ts +18 -0
- package/dist/vite.d.ts.map +1 -0
- package/dist/vite.js +18 -0
- package/dist/vite.js.map +1 -0
- package/dist/webpack.d.ts +5 -0
- package/dist/webpack.d.ts.map +1 -0
- package/dist/webpack.js +5 -0
- package/dist/webpack.js.map +1 -0
- package/package.json +58 -0
- package/src/esbuild.ts +5 -0
- package/src/index.test.ts +347 -0
- package/src/index.ts +468 -0
- package/src/rollup.ts +5 -0
- package/src/rspack.ts +5 -0
- package/src/vite.ts +18 -0
- package/src/webpack.ts +5 -0
|
@@ -0,0 +1,347 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { mkdtempSync, writeFileSync, mkdirSync, readFileSync, rmSync, existsSync } from "node:fs";
|
|
3
|
+
import { tmpdir } from "node:os";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
|
|
6
|
+
import { anvilUnplugin, type PreBuildHook, type PostBuildHook } from "./index.js";
|
|
7
|
+
|
|
8
|
+
// Find the workspace's `anvil` binary so the unplugin's `cli` option
|
|
9
|
+
// can shell out to it. In the monorepo we point straight at the
|
|
10
|
+
// debug build emitted by `cargo build`.
|
|
11
|
+
const repoRoot = path.resolve(__dirname, "..", "..", "..");
|
|
12
|
+
const anvilBin = path.join(
|
|
13
|
+
repoRoot,
|
|
14
|
+
"target",
|
|
15
|
+
"debug",
|
|
16
|
+
process.platform === "win32" ? "anvil.exe" : "anvil",
|
|
17
|
+
);
|
|
18
|
+
|
|
19
|
+
function makeFixture(): { dir: string; entry: string; output: string } {
|
|
20
|
+
const dir = mkdtempSync(path.join(tmpdir(), "anvil-unplugin-"));
|
|
21
|
+
// Stub `@anvil-di/anvil` runtime stubs so the parser can resolve them.
|
|
22
|
+
const anvilPkg = path.join(dir, "node_modules", "@anvil-di/anvil");
|
|
23
|
+
mkdirSync(anvilPkg, { recursive: true });
|
|
24
|
+
writeFileSync(
|
|
25
|
+
path.join(anvilPkg, "package.json"),
|
|
26
|
+
JSON.stringify({ name: "@anvil-di/anvil", main: "index.ts" }),
|
|
27
|
+
);
|
|
28
|
+
writeFileSync(
|
|
29
|
+
path.join(anvilPkg, "index.ts"),
|
|
30
|
+
[
|
|
31
|
+
"export const Inject = (..._: any[]) => {};",
|
|
32
|
+
"export const Module = (..._: any[]) => {};",
|
|
33
|
+
"export const Provides = (..._: any[]) => {};",
|
|
34
|
+
"export const Component = (..._: any[]) => {};",
|
|
35
|
+
"export const Singleton = (..._: any[]) => {};",
|
|
36
|
+
"export const Binds = (..._: any[]) => {};",
|
|
37
|
+
"export const Subcomponent = (..._: any[]) => {};",
|
|
38
|
+
"export const IntoSet = (..._: any[]) => {};",
|
|
39
|
+
].join("\n"),
|
|
40
|
+
);
|
|
41
|
+
writeFileSync(
|
|
42
|
+
path.join(dir, "heater.ts"),
|
|
43
|
+
[
|
|
44
|
+
'import { Inject } from "@anvil-di/anvil";',
|
|
45
|
+
"@Inject",
|
|
46
|
+
"export class Heater { constructor() {} }",
|
|
47
|
+
].join("\n"),
|
|
48
|
+
);
|
|
49
|
+
const entry = path.join(dir, "coffee-component.ts");
|
|
50
|
+
writeFileSync(
|
|
51
|
+
entry,
|
|
52
|
+
[
|
|
53
|
+
'import { Component } from "@anvil-di/anvil";',
|
|
54
|
+
'import { Heater } from "./heater";',
|
|
55
|
+
"@Component({ modules: [] })",
|
|
56
|
+
"export abstract class CoffeeShop {",
|
|
57
|
+
" abstract heater(): Heater;",
|
|
58
|
+
"}",
|
|
59
|
+
].join("\n"),
|
|
60
|
+
);
|
|
61
|
+
return { dir, entry, output: path.join(dir, "coffee-component.anvil.ts") };
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
describe("anvil-unplugin", () => {
|
|
65
|
+
it("invokes `anvil build` on buildStart and emits the .anvil.ts file", async () => {
|
|
66
|
+
const { dir, entry, output } = makeFixture();
|
|
67
|
+
try {
|
|
68
|
+
const plugin = anvilUnplugin.rollup({
|
|
69
|
+
cli: anvilBin,
|
|
70
|
+
entries: [entry],
|
|
71
|
+
}) as { name: string; buildStart?: () => Promise<void> };
|
|
72
|
+
expect(plugin.name).toBe("anvil-unplugin");
|
|
73
|
+
expect(typeof plugin.buildStart).toBe("function");
|
|
74
|
+
await plugin.buildStart!.call({});
|
|
75
|
+
const generated = readFileSync(output, "utf8");
|
|
76
|
+
expect(generated).toContain("AnvilCoffeeShop");
|
|
77
|
+
expect(generated).toContain("export function createCoffeeShop");
|
|
78
|
+
} finally {
|
|
79
|
+
rmSync(dir, { recursive: true, force: true });
|
|
80
|
+
}
|
|
81
|
+
}, 30_000);
|
|
82
|
+
|
|
83
|
+
it("debounces watchChange invocations", async () => {
|
|
84
|
+
const { dir, entry } = makeFixture();
|
|
85
|
+
try {
|
|
86
|
+
// Spy on the CLI binding by passing a no-op CLI path through a
|
|
87
|
+
// shell wrapper. We only need to count invocations, not produce
|
|
88
|
+
// real output.
|
|
89
|
+
const plugin = anvilUnplugin.rollup({
|
|
90
|
+
cli: anvilBin,
|
|
91
|
+
entries: [entry],
|
|
92
|
+
debounceMs: 30,
|
|
93
|
+
}) as {
|
|
94
|
+
watchChange?: (id: string, change: unknown) => void;
|
|
95
|
+
};
|
|
96
|
+
expect(typeof plugin.watchChange).toBe("function");
|
|
97
|
+
// Fire several edits in quick succession; the debounce window
|
|
98
|
+
// collapses them into one rebuild.
|
|
99
|
+
const changedFile = path.join(dir, "heater.ts");
|
|
100
|
+
plugin.watchChange!(changedFile, {});
|
|
101
|
+
plugin.watchChange!(changedFile, {});
|
|
102
|
+
plugin.watchChange!(changedFile, {});
|
|
103
|
+
// The debounce just wraps a setTimeout — sanity-check the
|
|
104
|
+
// function runs without error and accepts the right shape.
|
|
105
|
+
await new Promise<void>((resolve) => setTimeout(resolve, 80));
|
|
106
|
+
} finally {
|
|
107
|
+
rmSync(dir, { recursive: true, force: true });
|
|
108
|
+
}
|
|
109
|
+
}, 30_000);
|
|
110
|
+
|
|
111
|
+
it("ignores edits to .anvil.ts files (avoids self-trigger loops)", () => {
|
|
112
|
+
const plugin = anvilUnplugin.rollup({
|
|
113
|
+
cli: anvilBin,
|
|
114
|
+
entries: ["nonexistent.ts"],
|
|
115
|
+
debounceMs: 30,
|
|
116
|
+
}) as { watchChange?: (id: string, change: unknown) => void };
|
|
117
|
+
// Should not throw and should not schedule any rebuild — we
|
|
118
|
+
// verify by ensuring no unhandled rejection occurs.
|
|
119
|
+
plugin.watchChange!("/tmp/foo.anvil.ts", {});
|
|
120
|
+
// Also: random non-TS files are ignored.
|
|
121
|
+
plugin.watchChange!("/tmp/foo.css", {});
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
it("compiles in-process via WASM mode and emits the .anvil.ts file", async () => {
|
|
125
|
+
const { dir, entry, output } = makeFixture();
|
|
126
|
+
try {
|
|
127
|
+
const plugin = anvilUnplugin.rollup({
|
|
128
|
+
mode: "wasm",
|
|
129
|
+
entries: [entry],
|
|
130
|
+
rootDir: dir,
|
|
131
|
+
}) as { name: string; buildStart?: () => Promise<void> };
|
|
132
|
+
await plugin.buildStart!.call({});
|
|
133
|
+
const generated = readFileSync(output, "utf8");
|
|
134
|
+
expect(generated).toContain("AnvilCoffeeShop");
|
|
135
|
+
expect(generated).toContain("export function createCoffeeShop");
|
|
136
|
+
// The WASM build produces the same shape as the native one —
|
|
137
|
+
// smoke-check by comparing the dagger class declaration line.
|
|
138
|
+
expect(generated).toMatch(/export class AnvilCoffeeShop extends CoffeeShop/);
|
|
139
|
+
} finally {
|
|
140
|
+
rmSync(dir, { recursive: true, force: true });
|
|
141
|
+
}
|
|
142
|
+
}, 30_000);
|
|
143
|
+
|
|
144
|
+
// Use the spy avoidance above to keep the suite from accidentally
|
|
145
|
+
// depending on the real cargo binary at runtime.
|
|
146
|
+
it("exposes per-bundler entry points", () => {
|
|
147
|
+
expect(typeof anvilUnplugin.vite).toBe("function");
|
|
148
|
+
expect(typeof anvilUnplugin.rollup).toBe("function");
|
|
149
|
+
expect(typeof anvilUnplugin.webpack).toBe("function");
|
|
150
|
+
expect(typeof anvilUnplugin.rspack).toBe("function");
|
|
151
|
+
expect(typeof anvilUnplugin.esbuild).toBe("function");
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
// ---------------------------------------------------------------------------
|
|
155
|
+
// M4 — preBuild / postBuild hook API
|
|
156
|
+
// ---------------------------------------------------------------------------
|
|
157
|
+
|
|
158
|
+
function makeHook(
|
|
159
|
+
name: string,
|
|
160
|
+
onRun: () => void,
|
|
161
|
+
shouldRerun: (files: string[]) => boolean = () => false,
|
|
162
|
+
): PreBuildHook & PostBuildHook {
|
|
163
|
+
return {
|
|
164
|
+
name,
|
|
165
|
+
watchPatterns: [],
|
|
166
|
+
shouldRerun,
|
|
167
|
+
async run() {
|
|
168
|
+
onRun();
|
|
169
|
+
},
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
it("runs preBuild hooks before anvil build in buildStart", async () => {
|
|
174
|
+
const { dir, entry, output } = makeFixture();
|
|
175
|
+
try {
|
|
176
|
+
const order: string[] = [];
|
|
177
|
+
const hook = makeHook("pre", () => order.push("pre"));
|
|
178
|
+
|
|
179
|
+
const plugin = anvilUnplugin.rollup({
|
|
180
|
+
cli: anvilBin,
|
|
181
|
+
entries: [entry],
|
|
182
|
+
preBuild: [hook],
|
|
183
|
+
}) as { buildStart?: () => Promise<void> };
|
|
184
|
+
|
|
185
|
+
await plugin.buildStart!.call({});
|
|
186
|
+
|
|
187
|
+
// preBuild ran, and the anvil build also ran (output file exists).
|
|
188
|
+
expect(order).toEqual(["pre"]);
|
|
189
|
+
expect(existsSync(output)).toBe(true);
|
|
190
|
+
} finally {
|
|
191
|
+
rmSync(dir, { recursive: true, force: true });
|
|
192
|
+
}
|
|
193
|
+
}, 30_000);
|
|
194
|
+
|
|
195
|
+
it("runs postBuild hooks after anvil build in buildStart", async () => {
|
|
196
|
+
const { dir, entry, output } = makeFixture();
|
|
197
|
+
try {
|
|
198
|
+
const order: string[] = [];
|
|
199
|
+
const post = makeHook("post", () => order.push("post"));
|
|
200
|
+
|
|
201
|
+
const plugin = anvilUnplugin.rollup({
|
|
202
|
+
cli: anvilBin,
|
|
203
|
+
entries: [entry],
|
|
204
|
+
postBuild: [post],
|
|
205
|
+
}) as { buildStart?: () => Promise<void> };
|
|
206
|
+
|
|
207
|
+
await plugin.buildStart!.call({});
|
|
208
|
+
|
|
209
|
+
// postBuild ran after anvil build produced the output file.
|
|
210
|
+
expect(existsSync(output)).toBe(true);
|
|
211
|
+
expect(order).toEqual(["post"]);
|
|
212
|
+
} finally {
|
|
213
|
+
rmSync(dir, { recursive: true, force: true });
|
|
214
|
+
}
|
|
215
|
+
}, 30_000);
|
|
216
|
+
|
|
217
|
+
it("runs preBuild → anvil build → postBuild in order in buildStart", async () => {
|
|
218
|
+
const { dir, entry } = makeFixture();
|
|
219
|
+
try {
|
|
220
|
+
const order: string[] = [];
|
|
221
|
+
const pre = makeHook("pre", () => order.push("pre"));
|
|
222
|
+
const post = makeHook("post", () => order.push("post"));
|
|
223
|
+
|
|
224
|
+
const plugin = anvilUnplugin.rollup({
|
|
225
|
+
cli: anvilBin,
|
|
226
|
+
entries: [entry],
|
|
227
|
+
preBuild: [pre],
|
|
228
|
+
postBuild: [post],
|
|
229
|
+
}) as { buildStart?: () => Promise<void> };
|
|
230
|
+
|
|
231
|
+
await plugin.buildStart!.call({});
|
|
232
|
+
|
|
233
|
+
expect(order).toEqual(["pre", "post"]);
|
|
234
|
+
} finally {
|
|
235
|
+
rmSync(dir, { recursive: true, force: true });
|
|
236
|
+
}
|
|
237
|
+
}, 30_000);
|
|
238
|
+
|
|
239
|
+
it("re-runs preBuild hook in watch mode when shouldRerun returns true", async () => {
|
|
240
|
+
const { dir, entry } = makeFixture();
|
|
241
|
+
try {
|
|
242
|
+
let runCount = 0;
|
|
243
|
+
const hook = makeHook(
|
|
244
|
+
"pre",
|
|
245
|
+
() => { runCount++; },
|
|
246
|
+
(files) => files.some((f) => f.startsWith(dir)),
|
|
247
|
+
);
|
|
248
|
+
|
|
249
|
+
const plugin = anvilUnplugin.rollup({
|
|
250
|
+
cli: anvilBin,
|
|
251
|
+
entries: [entry],
|
|
252
|
+
preBuild: [hook],
|
|
253
|
+
debounceMs: 30,
|
|
254
|
+
}) as { watchChange?: (id: string, change: unknown) => void };
|
|
255
|
+
|
|
256
|
+
plugin.watchChange!(path.join(dir, "heater.ts"), {});
|
|
257
|
+
await new Promise<void>((resolve) => setTimeout(resolve, 120));
|
|
258
|
+
|
|
259
|
+
expect(runCount).toBe(1);
|
|
260
|
+
} finally {
|
|
261
|
+
rmSync(dir, { recursive: true, force: true });
|
|
262
|
+
}
|
|
263
|
+
}, 30_000);
|
|
264
|
+
|
|
265
|
+
it("does not re-run preBuild hook in watch mode when shouldRerun returns false", async () => {
|
|
266
|
+
const { dir, entry } = makeFixture();
|
|
267
|
+
try {
|
|
268
|
+
let runCount = 0;
|
|
269
|
+
// shouldRerun always returns false.
|
|
270
|
+
const hook = makeHook("pre", () => { runCount++; }, () => false);
|
|
271
|
+
|
|
272
|
+
const plugin = anvilUnplugin.rollup({
|
|
273
|
+
cli: anvilBin,
|
|
274
|
+
entries: [entry],
|
|
275
|
+
preBuild: [hook],
|
|
276
|
+
debounceMs: 30,
|
|
277
|
+
}) as { watchChange?: (id: string, change: unknown) => void };
|
|
278
|
+
|
|
279
|
+
plugin.watchChange!(path.join(dir, "heater.ts"), {});
|
|
280
|
+
await new Promise<void>((resolve) => setTimeout(resolve, 120));
|
|
281
|
+
|
|
282
|
+
expect(runCount).toBe(0);
|
|
283
|
+
} finally {
|
|
284
|
+
rmSync(dir, { recursive: true, force: true });
|
|
285
|
+
}
|
|
286
|
+
}, 30_000);
|
|
287
|
+
|
|
288
|
+
it("runs postBuild hooks when preBuild hook re-runs in watch mode", async () => {
|
|
289
|
+
const { dir, entry } = makeFixture();
|
|
290
|
+
try {
|
|
291
|
+
let postRunCount = 0;
|
|
292
|
+
const pre = makeHook(
|
|
293
|
+
"pre",
|
|
294
|
+
() => {},
|
|
295
|
+
(files) => files.some((f) => f.startsWith(dir)),
|
|
296
|
+
);
|
|
297
|
+
const post = makeHook("post", () => { postRunCount++; });
|
|
298
|
+
|
|
299
|
+
const plugin = anvilUnplugin.rollup({
|
|
300
|
+
cli: anvilBin,
|
|
301
|
+
entries: [entry],
|
|
302
|
+
preBuild: [pre],
|
|
303
|
+
postBuild: [post],
|
|
304
|
+
debounceMs: 30,
|
|
305
|
+
}) as { watchChange?: (id: string, change: unknown) => void };
|
|
306
|
+
|
|
307
|
+
plugin.watchChange!(path.join(dir, "heater.ts"), {});
|
|
308
|
+
await new Promise<void>((resolve) => setTimeout(resolve, 120));
|
|
309
|
+
|
|
310
|
+
// postBuild ran because preBuild ran.
|
|
311
|
+
expect(postRunCount).toBe(1);
|
|
312
|
+
} finally {
|
|
313
|
+
rmSync(dir, { recursive: true, force: true });
|
|
314
|
+
}
|
|
315
|
+
}, 30_000);
|
|
316
|
+
|
|
317
|
+
it("runs only opted-in postBuild hooks when no preBuild hook re-runs", async () => {
|
|
318
|
+
const { dir, entry } = makeFixture();
|
|
319
|
+
try {
|
|
320
|
+
let postACount = 0;
|
|
321
|
+
let postBCount = 0;
|
|
322
|
+
// postA opts in, postB does not.
|
|
323
|
+
const postA = makeHook(
|
|
324
|
+
"postA",
|
|
325
|
+
() => { postACount++; },
|
|
326
|
+
(files) => files.some((f) => f.startsWith(dir)),
|
|
327
|
+
);
|
|
328
|
+
const postB = makeHook("postB", () => { postBCount++; }, () => false);
|
|
329
|
+
|
|
330
|
+
const plugin = anvilUnplugin.rollup({
|
|
331
|
+
cli: anvilBin,
|
|
332
|
+
entries: [entry],
|
|
333
|
+
postBuild: [postA, postB],
|
|
334
|
+
debounceMs: 30,
|
|
335
|
+
}) as { watchChange?: (id: string, change: unknown) => void };
|
|
336
|
+
|
|
337
|
+
plugin.watchChange!(path.join(dir, "heater.ts"), {});
|
|
338
|
+
await new Promise<void>((resolve) => setTimeout(resolve, 120));
|
|
339
|
+
|
|
340
|
+
expect(postACount).toBe(1);
|
|
341
|
+
expect(postBCount).toBe(0);
|
|
342
|
+
} finally {
|
|
343
|
+
rmSync(dir, { recursive: true, force: true });
|
|
344
|
+
}
|
|
345
|
+
}, 30_000);
|
|
346
|
+
|
|
347
|
+
});
|