mirror of
https://github.com/cloudflare/vinext.git
synced 2026-09-14 19:04:59 +08:00
88ed49e11d
* perf(build): cache repeated compatibility transforms App Router builds run pure dynamic-request and typeof window transforms repeatedly across RSC, SSR, and analysis passes. Re-parsing identical module input adds build work without changing output. The transform hooks now reuse results for exact module id, source, and environment replacement keys while replacing stale per-id source entries. Focused tests cover cache reuse and key separation. * perf(build): reuse pure compatibility transforms Repeated build environments can parse and rewrite the same module source more than once. The transform result is deterministic once the module id, source, and environment-derived variant are fixed. Share the existing bounded per-module cache across the compatible transform plugins and cover source, module, variant, and null-result boundaries. * fix(build): invalidate cached source identities after junction retargets Repeated transforms can keep emitting import.meta.url and CJS globals for a junction's previous canonical target when the raw ID and source stay unchanged. The import-meta-url cache variant omitted the canonical path even though the rewrite derives its output from that path. Include canonicalId in the variant and cover retargeting between identical source files. * ci: rerun performance benchmarks * perf(build): avoid composite import-meta cache keys Eligible import-meta transforms allocate and hash a composite string containing the canonical root and module path on every invocation, including cache hits. That adds deterministic work to dev cold start. Keep source, canonical root, and canonical id as direct equality fields in a per-module entry. Retain only the two-value environment result map so canonical-path invalidation remains correct without composite key allocation. --------- Co-authored-by: James <james@eli.cx>
35 lines
1.3 KiB
TypeScript
35 lines
1.3 KiB
TypeScript
import { describe, expect, it } from "vite-plus/test";
|
|
import { createRequireContextPlugin } from "../packages/vinext/src/plugins/require-context.js";
|
|
|
|
function unwrapHook(hook: any): Function {
|
|
return typeof hook === "function" ? hook : hook?.handler;
|
|
}
|
|
|
|
function createTransform(): Function {
|
|
const plugin = createRequireContextPlugin();
|
|
return unwrapHook(plugin.transform).bind(plugin);
|
|
}
|
|
|
|
describe("vinext:require-context", () => {
|
|
it("rewrites literal require.context calls into an import.meta.glob map", () => {
|
|
const transform = createTransform();
|
|
const result = transform(
|
|
`const ctx = require.context("./posts", true, /\\.md$/);`,
|
|
"/app/page.tsx",
|
|
);
|
|
|
|
expect(result.code).toContain('import.meta.glob("./posts/**/*", { eager: true })');
|
|
});
|
|
|
|
it("reuses the cached transform result for a repeated id/source pair", () => {
|
|
const transform = createTransform();
|
|
const source = `const ctx = require.context("./posts", true, /\\.md$/);`;
|
|
|
|
const first = transform(source, "/app/page.tsx");
|
|
expect(first).toBeTruthy();
|
|
expect(transform(source, "/app/page.tsx")).toBe(first);
|
|
expect(transform(`${source}\nconsole.log("changed");`, "/app/page.tsx")).not.toBe(first);
|
|
expect(transform(source, "/app/other.tsx")).not.toBe(first);
|
|
});
|
|
});
|