import type { Plugin, PluginContext } from "rollup"; import type { Dirent } from "fs"; import type { IsomorphicRollupFs, Stats } from "../../src/fs-iface"; import sass from "../../src/sass"; // large enough compiled CSS to cross the 14KB extraction threshold used below const bigScss = Array.from( { length: 2000 }, (_, i) => `.a${i}{color:red}`, ).join("\n"); type FakeDirent = { name: string; isSymbolicLink: () => boolean }; const createFakeFs = ( readdirEntries: Record, realpathTargets: Record = {}, ): IsomorphicRollupFs & { readdirCalls: string[] } => { const readdirCalls: string[] = []; return { readdirCalls, access: async () => true, readFile: async () => "", realpath: async (p: string) => p, realpathSync: (p: string) => realpathTargets[p] ?? p, stat: async () => ({}) as Stats, mkdir: async () => {}, writeFile: async () => {}, promises: { readdir: async (dir: string) => { readdirCalls.push(dir); const entries = readdirEntries[dir]; if (!entries) { const err: NodeJS.ErrnoException = new Error( `ENOENT: no such file or directory, scandir '${dir}'`, ); err.code = "ENOENT"; throw err; } return entries as unknown as Dirent[]; }, }, } as unknown as IsomorphicRollupFs & { readdirCalls: string[] }; }; const createCtx = () => ({ addWatchFile: jest.fn(), emitFile: jest.fn(), warn: jest.fn(), }) as unknown as PluginContext; describe("sass plugin", () => { describe("rootDir / node_modules symlink resolution", () => { test("does not rewrite filePath when rootDir is omitted (default behaviour is unchanged)", async () => { const getExtractNameAndUrl = jest.fn(async (filePath: string) => ({ name: filePath, url: filePath, })); const fs = createFakeFs({}); const plugin: Plugin = sass({ fs, insert: true, shouldExtract: (_, content) => content.length > 14 * 1024, getExtractNameAndUrl, }); const ctx = createCtx(); const filePath = "/packages/react/dist/Foo.scss"; await (plugin.transform as any).call(ctx, bigScss, filePath); await (plugin.generateBundle as any).call(ctx, {}, {}, true); expect(getExtractNameAndUrl).toHaveBeenCalledWith( filePath, expect.any(String), fs, ); // No rootDir was given, so the plugin never scans node_modules. expect(fs.readdirCalls).toHaveLength(0); }); test("rewrites filePath through its node_modules symlink when rootDir is given", async () => { const rootDir = "/project"; const nodeModulesDir = "/project/node_modules"; const linkPath = "/project/node_modules/@pkg"; const fs = createFakeFs( { [nodeModulesDir]: [{ name: "@pkg", isSymbolicLink: () => true }] }, { [linkPath]: "/packages/react" }, ); const getExtractNameAndUrl = jest.fn(async (filePath: string) => ({ name: filePath, url: filePath, })); const plugin: Plugin = sass({ fs, insert: true, rootDir, shouldExtract: (_, content) => content.length > 14 * 1024, getExtractNameAndUrl, }); const ctx = createCtx(); // No node_modules ancestor, mirroring what pnpm workspaces hand // rollup after resolving the package symlink. const filePath = "/packages/react/dist/Foo.scss"; await (plugin.transform as any).call(ctx, bigScss, filePath); await (plugin.generateBundle as any).call(ctx, {}, {}, true); expect(getExtractNameAndUrl).toHaveBeenCalledWith( "/project/node_modules/@pkg/dist/Foo.scss", expect.any(String), fs, ); }); test("embeds the resolved path in the transform-time href, not just the generateBundle emitFile name", async () => { const rootDir = "/project"; const nodeModulesDir = "/project/node_modules"; const linkPath = "/project/node_modules/@pkg"; const fs = createFakeFs( { [nodeModulesDir]: [{ name: "@pkg", isSymbolicLink: () => true }] }, { [linkPath]: "/packages/react" }, ); const getExtractNameAndUrl = jest.fn(async (filePath: string) => ({ name: filePath, url: filePath, })); const plugin: Plugin = sass({ fs, insert: true, rootDir, shouldExtract: (_, content) => content.length > 14 * 1024, getExtractNameAndUrl, }); const ctx = createCtx(); // No node_modules ancestor, mirroring what pnpm workspaces hand // rollup after resolving the package symlink. const filePath = "/packages/react/dist/Foo.scss"; const result = await (plugin.transform as any).call( ctx, bigScss, filePath, ); await (plugin.generateBundle as any).call(ctx, {}, {}, true); // Every call, including the transform-time one that produces the // embedded href, must use the resolved path — not just the // later generateBundle call that emits the asset file. expect(getExtractNameAndUrl).not.toHaveBeenCalledWith( filePath, expect.any(String), fs, ); expect(result.code).toContain("/project/node_modules/@pkg/dist/Foo.scss"); }); test("leaves filePath unchanged when rootDir is given but no symlink matches", async () => { const fs = createFakeFs({ "/project/node_modules": [] }); const getExtractNameAndUrl = jest.fn(async (filePath: string) => ({ name: filePath, url: filePath, })); const plugin: Plugin = sass({ fs, insert: true, rootDir: "/project", shouldExtract: (_, content) => content.length > 14 * 1024, getExtractNameAndUrl, }); const ctx = createCtx(); const filePath = "/packages/react/dist/Foo.scss"; await (plugin.transform as any).call(ctx, bigScss, filePath); await (plugin.generateBundle as any).call(ctx, {}, {}, true); expect(getExtractNameAndUrl).toHaveBeenCalledWith( filePath, expect.any(String), fs, ); }); test("buildStart resets the memoized node_modules scan", async () => { const fs = createFakeFs({ "/project/node_modules": [] }); const plugin: Plugin = sass({ fs, insert: true, rootDir: "/project", shouldExtract: (_, content) => content.length > 14 * 1024, }); const ctx = createCtx(); await (plugin.transform as any).call( ctx, bigScss, "/packages/a/dist/A.scss", ); await (plugin.generateBundle as any).call(ctx, {}, {}, true); await (plugin.transform as any).call( ctx, bigScss, "/packages/b/dist/B.scss", ); await (plugin.generateBundle as any).call(ctx, {}, {}, true); expect(fs.readdirCalls).toHaveLength(1); await (plugin.buildStart as any).call(ctx); await (plugin.transform as any).call( ctx, bigScss, "/packages/c/dist/C.scss", ); await (plugin.generateBundle as any).call(ctx, {}, {}, true); expect(fs.readdirCalls).toHaveLength(2); }); }); });