import type { Plugin, TransformPluginContext, TransformResult, EmittedAsset, SourceDescription, } from "rollup"; import { cssUrl } from "../../src/css-url"; interface MockContext extends TransformPluginContext { getEmittedFiles: () => Array<{ type: string; fileName: string; source: Uint8Array; }>; getWarnings: () => string[]; getWatchedFiles: () => string[]; } describe("CSS URL plugin", () => { const createMockContext = ( files: Record = {}, ): MockContext => { const emittedFiles: Array<{ type: string; fileName: string; source: Uint8Array; }> = []; const warnings: string[] = []; const watchedFiles: string[] = []; return { fs: { readFile: async (path: string) => { const content = files[path]; if (content === undefined) { throw new Error( `ENOENT: no such file or directory, open '${path}'`, ); } return content; }, }, addWatchFile: (file: string) => { watchedFiles.push(file); }, emitFile: (file: EmittedAsset) => { emittedFiles.push( file as { type: string; fileName: string; source: Uint8Array }, ); return file.fileName!; }, warn: (message: string) => { warnings.push(message); }, getEmittedFiles: () => emittedFiles, getWarnings: () => warnings, getWatchedFiles: () => watchedFiles, } as unknown as MockContext; }; const callTransformHook = async ( plugin: Plugin, context: TransformPluginContext, code: string, id: string, ): Promise => { let result: TransformResult; if (typeof plugin.transform === "function") { result = await plugin.transform.call(context, code, id); } else if ( plugin.transform && typeof plugin.transform === "object" && "handler" in plugin.transform ) { const transformHook = plugin.transform as { handler: (code: string, id: string) => TransformResult; }; result = await transformHook.handler.call(context, code, id); } else { return null; } if (typeof result === "string") { return { code: result }; } return result as SourceDescription | null; }; describe("basic functionality", () => { test("should inline small images as base64", async () => { const plugin = cssUrl() as Plugin; const smallImage = new Uint8Array([137, 80, 78, 71, 13, 10, 26, 10]); // PNG header const files = { "/project/styles/icon.png": smallImage, }; const context = createMockContext(files); const code = `.icon { background: url('./icon.png'); }`; const id = "/project/styles/main.css"; const result = await callTransformHook(plugin, context, code, id); expect(result).not.toBeNull(); expect(result!.code).toContain("data:image/png;base64,"); expect(result!.code).toContain("iVBORw0KGgo="); // Base64 of PNG header expect(context.getEmittedFiles()).toHaveLength(0); }); test("should extract large files as assets", async () => { const plugin = cssUrl({ getExtractNameAndUrl: (url) => ({ name: url.replace("./", ""), url: url.replace("./", ""), }), }) as Plugin; // Create a large file > 14KB const largeImage = new Uint8Array(15 * 1024); largeImage[0] = 137; // PNG header largeImage[1] = 80; const files = { "/project/styles/large-image.png": largeImage, }; const context = createMockContext(files); const code = `.bg { background: url('./large-image.png'); }`; const id = "/project/styles/main.css"; const result = await callTransformHook(plugin, context, code, id); expect(result).not.toBeNull(); expect(result!.code).toContain("url('large-image.png')"); expect(result!.code).not.toContain("data:"); const emittedFiles = context.getEmittedFiles(); expect(emittedFiles).toHaveLength(1); expect(emittedFiles[0]).toMatchObject({ type: "asset", fileName: "large-image.png", source: largeImage, }); }); test("should not touch http/https URLs", async () => { const plugin = cssUrl() as Plugin; const context = createMockContext(); const code = `.remote { background: url('https://example.com/image.png'); }`; const id = "/project/styles/main.css"; const result = await callTransformHook(plugin, context, code, id); expect(result).toBeNull(); // No transformation }); test("should not touch data URLs", async () => { const plugin = cssUrl() as Plugin; const context = createMockContext(); const code = `.inlined { background: url('data:image/png;base64,abc123'); }`; const id = "/project/styles/main.css"; const result = await callTransformHook(plugin, context, code, id); expect(result).toBeNull(); // No transformation }); test("should not touch protocol-relative URLs", async () => { const plugin = cssUrl() as Plugin; const context = createMockContext(); const code = `.cdn { background: url('//cdn.example.com/image.png'); }`; const id = "/project/styles/main.css"; const result = await callTransformHook(plugin, context, code, id); expect(result).toBeNull(); // No transformation }); test("should handle multiple URLs in same file", async () => { const plugin = cssUrl() as Plugin; const smallImage1 = new Uint8Array([1, 2, 3]); const smallImage2 = new Uint8Array([4, 5, 6]); const files = { "/project/styles/icon1.png": smallImage1, "/project/styles/icon2.png": smallImage2, }; const context = createMockContext(files); const code = ` .icon1 { background: url('./icon1.png'); } .icon2 { background: url('./icon2.png'); } `; const id = "/project/styles/main.css"; const result = await callTransformHook(plugin, context, code, id); expect(result).not.toBeNull(); expect(result!.code).toContain("data:image/png;base64,AQID"); expect(result!.code).toContain("data:image/png;base64,BAUG"); }); test("should handle URLs without quotes", async () => { const plugin = cssUrl() as Plugin; const smallImage = new Uint8Array([1, 2, 3]); const files = { "/project/styles/icon.png": smallImage, }; const context = createMockContext(files); const code = `.icon { background: url(./icon.png); }`; const id = "/project/styles/main.css"; const result = await callTransformHook(plugin, context, code, id); expect(result).not.toBeNull(); expect(result!.code).toContain("data:image/png;base64,"); }); test("should handle URLs with single quotes", async () => { const plugin = cssUrl() as Plugin; const smallImage = new Uint8Array([1, 2, 3]); const files = { "/project/styles/icon.png": smallImage, }; const context = createMockContext(files); const code = `.icon { background: url('./icon.png'); }`; const id = "/project/styles/main.css"; const result = await callTransformHook(plugin, context, code, id); expect(result).not.toBeNull(); expect(result!.code).toContain("data:image/png;base64,"); }); test("should handle URLs with double quotes", async () => { const plugin = cssUrl() as Plugin; const smallImage = new Uint8Array([1, 2, 3]); const files = { "/project/styles/icon.png": smallImage, }; const context = createMockContext(files); const code = `.icon { background: url("./icon.png"); }`; const id = "/project/styles/main.css"; const result = await callTransformHook(plugin, context, code, id); expect(result).not.toBeNull(); expect(result!.code).toContain("data:image/png;base64,"); }); }); describe("watch file tracking", () => { test("should add resolved files to watch list", async () => { const plugin = cssUrl() as Plugin; const smallImage = new Uint8Array([137, 80, 78, 71, 13, 10, 26, 10]); const files = { "/project/styles/icon.png": smallImage, }; const context = createMockContext(files); const code = `.icon { background: url('./icon.png'); }`; const id = "/project/styles/main.css"; await callTransformHook(plugin, context, code, id); expect(context.getWatchedFiles()).toContain("/project/styles/icon.png"); }); test("should add multiple resolved files to watch list", async () => { const plugin = cssUrl() as Plugin; const image1 = new Uint8Array([1, 2, 3]); const image2 = new Uint8Array([4, 5, 6]); const files = { "/project/styles/icon1.png": image1, "/project/styles/icon2.png": image2, }; const context = createMockContext(files); const code = ` .icon1 { background: url('./icon1.png'); } .icon2 { background: url('./icon2.png'); } `; const id = "/project/styles/main.css"; await callTransformHook(plugin, context, code, id); const watched = context.getWatchedFiles(); expect(watched).toContain("/project/styles/icon1.png"); expect(watched).toContain("/project/styles/icon2.png"); }); test("should not add watch files for http/https URLs", async () => { const plugin = cssUrl() as Plugin; const context = createMockContext(); const code = `.remote { background: url('https://example.com/image.png'); }`; const id = "/project/styles/main.css"; await callTransformHook(plugin, context, code, id); expect(context.getWatchedFiles()).toHaveLength(0); }); test("should not add watch files for missing files", async () => { const plugin = cssUrl() as Plugin; const context = createMockContext(); const code = `.icon { background: url('./missing.png'); }`; const id = "/project/styles/main.css"; await callTransformHook(plugin, context, code, id); expect(context.getWatchedFiles()).toHaveLength(0); }); }); describe("file filtering", () => { test("should process CSS files by default", async () => { const plugin = cssUrl() as Plugin; const smallImage = new Uint8Array([1, 2, 3]); const files = { "/project/styles/icon.png": smallImage, }; const context = createMockContext(files); const code = `.icon { background: url('./icon.png'); }`; const id = "/project/styles/main.css"; const result = await callTransformHook(plugin, context, code, id); expect(result).not.toBeNull(); }); test("should process SCSS files by default", async () => { const plugin = cssUrl() as Plugin; const smallImage = new Uint8Array([1, 2, 3]); const files = { "/project/styles/icon.png": smallImage, }; const context = createMockContext(files); const code = `.icon { background: url('./icon.png'); }`; const id = "/project/styles/main.scss"; const result = await callTransformHook(plugin, context, code, id); expect(result).not.toBeNull(); }); test("should process SASS files by default", async () => { const plugin = cssUrl() as Plugin; const smallImage = new Uint8Array([1, 2, 3]); const files = { "/project/styles/icon.png": smallImage, }; const context = createMockContext(files); const code = `.icon\n background: url('./icon.png')`; const id = "/project/styles/main.sass"; const result = await callTransformHook(plugin, context, code, id); expect(result).not.toBeNull(); }); test("should respect include option", async () => { const plugin = cssUrl({ include: "**/*.css" }) as Plugin; const smallImage = new Uint8Array([1, 2, 3]); const files = { "/project/styles/icon.png": smallImage, }; const context = createMockContext(files); const code = `.icon { background: url('./icon.png'); }`; // Should process .css const resultCss = await callTransformHook( plugin, context, code, "/project/styles/main.css", ); expect(resultCss).not.toBeNull(); // Should not process .scss const resultScss = await callTransformHook( plugin, context, code, "/project/styles/main.scss", ); expect(resultScss).toBeNull(); }); test("should respect exclude option", async () => { const plugin = cssUrl({ exclude: "**/*.scss" }) as Plugin; const smallImage = new Uint8Array([1, 2, 3]); const files = { "/project/styles/icon.png": smallImage, }; const context = createMockContext(files); const code = `.icon { background: url('./icon.png'); }`; // Should process .css const resultCss = await callTransformHook( plugin, context, code, "/project/styles/main.css", ); expect(resultCss).not.toBeNull(); // Should not process .scss const resultScss = await callTransformHook( plugin, context, code, "/project/styles/main.scss", ); expect(resultScss).toBeNull(); }); }); describe("custom options", () => { test("should use default getExtractNameAndUrl that returns url unchanged", async () => { // Test default behavior when getExtractNameAndUrl is not provided const plugin = cssUrl({ shouldExtract: () => true, // Force extraction to test the default getExtractNameAndUrl }) as Plugin; const smallImage = new Uint8Array([1, 2, 3]); const files = { "/project/styles/icon.png": smallImage, }; const context = createMockContext(files); // Use double quotes so the transform changes to single quotes const code = `.icon { background: url("./icon.png"); }`; const id = "/project/styles/main.css"; const result = await callTransformHook(plugin, context, code, id); expect(result).not.toBeNull(); // Default getExtractNameAndUrl returns { name: url, url } unchanged // The plugin normalizes to single quotes expect(result!.code).toContain("url('./icon.png')"); expect(result!.code).not.toContain("data:"); const emittedFiles = context.getEmittedFiles(); expect(emittedFiles).toHaveLength(1); expect(emittedFiles[0].fileName).toBe("./icon.png"); }); test("should use custom shouldExtract function", async () => { // Extract all files regardless of size const plugin = cssUrl({ shouldExtract: () => true, getExtractNameAndUrl: (url) => ({ name: url.replace("./", ""), url: url.replace("./", ""), }), }) as Plugin; const smallImage = new Uint8Array([1, 2, 3]); const files = { "/project/styles/icon.png": smallImage, }; const context = createMockContext(files); const code = `.icon { background: url('./icon.png'); }`; const id = "/project/styles/main.css"; const result = await callTransformHook(plugin, context, code, id); expect(result).not.toBeNull(); expect(result!.code).toContain("url('icon.png')"); expect(result!.code).not.toContain("data:"); const emittedFiles = context.getEmittedFiles(); expect(emittedFiles).toHaveLength(1); expect(emittedFiles[0].fileName).toBe("icon.png"); }); test("should use custom getExtractNameAndUrl function", async () => { const plugin = cssUrl({ shouldExtract: () => true, getExtractNameAndUrl: (url) => ({ name: `assets/${url}`, url: `/static/assets/${url}`, }), }) as Plugin; const smallImage = new Uint8Array([1, 2, 3]); const files = { "/project/styles/icon.png": smallImage, }; const context = createMockContext(files); const code = `.icon { background: url('./icon.png'); }`; const id = "/project/styles/main.css"; const result = await callTransformHook(plugin, context, code, id); expect(result).not.toBeNull(); expect(result!.code).toContain("url('/static/assets/./icon.png')"); // Original URL prefix is preserved const emittedFiles = context.getEmittedFiles(); expect(emittedFiles).toHaveLength(1); expect(emittedFiles[0].fileName).toBe("assets/./icon.png"); }); test("should warn for large inlined files", async () => { const plugin = cssUrl({ warnSize: 5, // Warn for files > 5 bytes }) as Plugin; const largeForInline = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); const files = { "/project/styles/icon.png": largeForInline, }; const context = createMockContext(files); const code = `.icon { background: url('./icon.png'); }`; const id = "/project/styles/main.css"; const result = await callTransformHook(plugin, context, code, id); expect(result).not.toBeNull(); const warnings = context.getWarnings(); expect(warnings).toHaveLength(1); expect(warnings[0]).toContain( "Inlining file ./icon.png with large size 10", ); }); test("should not emit duplicate files", async () => { const plugin = cssUrl({ shouldExtract: () => true, getExtractNameAndUrl: (url) => ({ name: url.replace("./", ""), url: url.replace("./", ""), }), }) as Plugin; const image = new Uint8Array([1, 2, 3]); const files = { "/project/styles/icon.png": image, }; const context = createMockContext(files); const code = ` .icon1 { background: url('./icon.png'); } .icon2 { background: url('./icon.png'); } `; const id = "/project/styles/main.css"; const result = await callTransformHook(plugin, context, code, id); expect(result).not.toBeNull(); expect(result!.code).toContain("url('icon.png')"); const emittedFiles = context.getEmittedFiles(); expect(emittedFiles).toHaveLength(1); // Only emitted once expect(emittedFiles[0].fileName).toBe("icon.png"); }); }); describe("content hash", () => { test("passes a non-empty 8-char hex contentHash as the 4th argument to getExtractNameAndUrl", async () => { let capturedHash: string | undefined; const plugin = cssUrl({ shouldExtract: () => true, getExtractNameAndUrl: (url, _filePath, _mimeType, contentHash) => { capturedHash = contentHash; return { name: url.replace("./", ""), url: url.replace("./", "") }; }, }) as Plugin; const image = new Uint8Array(15 * 1024).fill(1); const files = { "/project/styles/icon.png": image }; const context = createMockContext(files); await callTransformHook( plugin, context, `.bg { background: url('./icon.png'); }`, "/project/styles/main.css", ); expect(capturedHash).toMatch(/^[0-9a-f]{8}$/); }); test("passes the same contentHash for the same file content", async () => { const hashes: string[] = []; const makePlugin = () => cssUrl({ shouldExtract: () => true, getExtractNameAndUrl: (url, _filePath, _mimeType, contentHash) => { hashes.push(contentHash); return { name: url.replace("./", ""), url: url.replace("./", "") }; }, }) as Plugin; const image = new Uint8Array([10, 20, 30, 40, 50]); const files = { "/project/styles/icon.png": image }; await callTransformHook( makePlugin(), createMockContext(files), `.a { background: url('./icon.png'); }`, "/project/styles/main.css", ); await callTransformHook( makePlugin(), createMockContext(files), `.b { background: url('./icon.png'); }`, "/project/styles/other.css", ); expect(hashes).toHaveLength(2); expect(hashes[0]).toBe(hashes[1]); }); test("passes different contentHash values for different file content", async () => { const hashes: string[] = []; const plugin = cssUrl({ shouldExtract: () => true, getExtractNameAndUrl: (url, _filePath, _mimeType, contentHash) => { hashes.push(contentHash); return { name: url.replace("./", ""), url: contentHash }; }, }) as Plugin; const files = { "/project/styles/a.png": new Uint8Array([1, 2, 3]), "/project/styles/b.png": new Uint8Array([4, 5, 6]), }; const context = createMockContext(files); await callTransformHook( plugin, context, `.a { background: url('./a.png'); } .b { background: url('./b.png'); }`, "/project/styles/main.css", ); expect(hashes).toHaveLength(2); expect(hashes[0]).not.toBe(hashes[1]); }); }); describe("error handling", () => { test("should warn and keep original URL when file not found", async () => { const plugin = cssUrl() as Plugin; const context = createMockContext(); const code = `.icon { background: url('./missing.png'); }`; const id = "/project/styles/main.css"; const result = await callTransformHook(plugin, context, code, id); expect(result).toBeNull(); // No transformation when all URLs fail const warnings = context.getWarnings(); expect(warnings).toHaveLength(1); expect(warnings[0]).toContain("Error processing file ./missing.png"); }); test("should warn when MIME type cannot be determined", async () => { const plugin = cssUrl() as Plugin; const unknownFile = new Uint8Array([1, 2, 3]); const files = { "/project/styles/file.unknown": unknownFile, }; const context = createMockContext(files); const code = `.unknown { content: url('./file.unknown'); }`; const id = "/project/styles/main.css"; const result = await callTransformHook(plugin, context, code, id); expect(result).toBeNull(); const warnings = context.getWarnings(); expect(warnings).toHaveLength(1); expect(warnings[0]).toContain("Could not determine MIME type"); }); test("should handle mixed success and failure", async () => { const plugin = cssUrl() as Plugin; const validImage = new Uint8Array([1, 2, 3]); const files = { "/project/styles/valid.png": validImage, }; const context = createMockContext(files); const code = ` .valid { background: url('./valid.png'); } .invalid { background: url('./missing.png'); } `; const id = "/project/styles/main.css"; const result = await callTransformHook(plugin, context, code, id); expect(result).not.toBeNull(); expect(result!.code).toContain("data:image/png;base64,"); // valid inlined expect(result!.code).toContain("url('./missing.png')"); // invalid kept const warnings = context.getWarnings(); expect(warnings).toHaveLength(1); expect(warnings[0]).toContain("Error processing file ./missing.png"); }); }); describe("MIME type handling", () => { test("should handle different image types", async () => { const plugin = cssUrl() as Plugin; const jpgImage = new Uint8Array([255, 216, 255]); // JPEG header const gifImage = new Uint8Array([71, 73, 70]); // GIF header const files = { "/project/styles/photo.jpg": jpgImage, "/project/styles/animation.gif": gifImage, }; const context = createMockContext(files); const code = ` .photo { background: url('./photo.jpg'); } .gif { background: url('./animation.gif'); } `; const id = "/project/styles/main.css"; const result = await callTransformHook(plugin, context, code, id); expect(result).not.toBeNull(); expect(result!.code).toContain("data:image/jpeg;base64,"); expect(result!.code).toContain("data:image/gif;base64,"); }); test("should handle font files", async () => { const plugin = cssUrl() as Plugin; const woffFont = new Uint8Array([1, 2, 3, 4]); const files = { "/project/styles/font.woff": woffFont, }; const context = createMockContext(files); const code = `@font-face { src: url('./font.woff'); }`; const id = "/project/styles/main.css"; const result = await callTransformHook(plugin, context, code, id); expect(result).not.toBeNull(); expect(result!.code).toContain("data:font/woff;base64,"); }); test("should handle SVG files", async () => { const plugin = cssUrl() as Plugin; const svgImage = new Uint8Array(Buffer.from("")); const files = { "/project/styles/icon.svg": svgImage, }; const context = createMockContext(files); const code = `.icon { mask: url('./icon.svg'); }`; const id = "/project/styles/main.css"; const result = await callTransformHook(plugin, context, code, id); expect(result).not.toBeNull(); expect(result!.code).toContain("data:image/svg+xml;base64,"); }); }); describe("module metadata", () => { test("should return resolved asset paths in meta", async () => { const plugin = cssUrl() as Plugin; const image1 = new Uint8Array([1, 2, 3]); const image2 = new Uint8Array([4, 5, 6]); const files = { "/project/styles/icon1.png": image1, "/project/styles/icon2.png": image2, }; const context = createMockContext(files); const code = ` .icon1 { background: url('./icon1.png'); } .icon2 { background: url('./icon2.png'); } `; const id = "/project/styles/main.css"; const result = await callTransformHook(plugin, context, code, id); expect(result).not.toBeNull(); expect(result!.meta).toBeDefined(); expect(result!.meta!["css-url"]).toBeDefined(); expect(result!.meta!["css-url"].assets).toEqual( expect.arrayContaining([ "/project/styles/icon1.png", "/project/styles/icon2.png", ]), ); }); test("should resolve nested and parent-relative paths in meta assets", async () => { const plugin = cssUrl() as Plugin; const image1 = new Uint8Array([1, 2, 3]); const image2 = new Uint8Array([4, 5, 6]); const files = { "/project/assets/images/logo.png": image1, "/project/styles/components/nested/icon.png": image2, }; const context = createMockContext(files); const code = ` .logo { background: url('../assets/images/logo.png'); } .icon { background: url('./components/nested/icon.png'); } `; const id = "/project/styles/main.css"; const result = await callTransformHook(plugin, context, code, id); expect(result).not.toBeNull(); expect(result!.meta!["css-url"].assets).toEqual([ "/project/assets/images/logo.png", "/project/styles/components/nested/icon.png", ]); }); test("should not include failed/skipped URLs in meta assets", async () => { const plugin = cssUrl() as Plugin; const validImage = new Uint8Array([1, 2, 3]); const files = { "/project/styles/valid.png": validImage, }; const context = createMockContext(files); const code = ` .valid { background: url('./valid.png'); } .remote { background: url('https://example.com/image.png'); } .missing { background: url('./missing.png'); } `; const id = "/project/styles/main.css"; const result = await callTransformHook(plugin, context, code, id); expect(result).not.toBeNull(); expect(result!.meta!["css-url"].assets).toEqual([ "/project/styles/valid.png", ]); }); test("should not return meta when no URLs are transformed", async () => { const plugin = cssUrl() as Plugin; const context = createMockContext(); const code = `.remote { background: url('https://example.com/image.png'); }`; const id = "/project/styles/main.css"; const result = await callTransformHook(plugin, context, code, id); // No transformation happened — result is null expect(result).toBeNull(); }); }); describe("source map generation", () => { test("should generate source map", async () => { const plugin = cssUrl() as Plugin; const smallImage = new Uint8Array([1, 2, 3]); const files = { "/project/styles/icon.png": smallImage, }; const context = createMockContext(files); const code = `.icon { background: url('./icon.png'); }`; const id = "/project/styles/main.css"; const result = await callTransformHook(plugin, context, code, id); expect(result).not.toBeNull(); expect(result!.map).toBeDefined(); expect(result!.map).toHaveProperty("mappings"); }); }); describe("path resolution", () => { test("should resolve relative paths correctly", async () => { const plugin = cssUrl() as Plugin; const image = new Uint8Array([1, 2, 3]); const files = { "/project/assets/images/icon.png": image, }; const context = createMockContext(files); const code = `.icon { background: url('../images/icon.png'); }`; const id = "/project/assets/styles/main.css"; const result = await callTransformHook(plugin, context, code, id); expect(result).not.toBeNull(); expect(result!.code).toContain("data:image/png;base64,"); }); test("should handle nested paths", async () => { const plugin = cssUrl() as Plugin; const image = new Uint8Array([1, 2, 3]); const files = { "/project/a/b/c/icon.png": image, }; const context = createMockContext(files); const code = `.icon { background: url('./c/icon.png'); }`; const id = "/project/a/b/main.css"; const result = await callTransformHook(plugin, context, code, id); expect(result).not.toBeNull(); expect(result!.code).toContain("data:image/png;base64,"); }); }); });