import type { NormalizedOutputOptions, OutputBundle, OutputChunk, Plugin, PluginContext, } from "rollup"; import { sourcemapUrl } from "../../src/sourcemap-url"; describe("Source Map URL plugin", () => { const testGenerateBundle = async ( replaceUrl: (url: string) => string, bundle: OutputBundle, ) => { const plugin: Plugin = sourcemapUrl({ replaceUrl }); if (typeof plugin.generateBundle !== "function") { throw new Error("Plugin does not have a generateBundle hook"); } plugin.generateBundle.call( {} as PluginContext, {} as NormalizedOutputOptions, bundle, false, ); }; test("should replace source map URL", async () => { const bundle: OutputBundle = { "example.js": { code: `console.log("Hello, world!"); //# sourceMappingURL=example.js.map `, map: {}, } as OutputChunk, }; testGenerateBundle((url) => `https://example.com/${url}`, bundle); expect((bundle["example.js"] as OutputChunk).code) .toBe(`console.log("Hello, world!"); //# sourceMappingURL=https://example.com/example.js.map `); }); test("should replace source map URLs for multiple files", async () => { const bundle: OutputBundle = { "index.js": { code: `alert("Hello, world!"); //# sourceMappingURL=index.js.map `, map: {}, } as OutputChunk, "example.js": { code: `console.log("Hello, world!"); //# sourceMappingURL=example.js.map `, map: {}, } as OutputChunk, }; testGenerateBundle((url) => `https://example.com/${url}`, bundle); expect((bundle["index.js"] as OutputChunk).code) .toBe(`alert("Hello, world!"); //# sourceMappingURL=https://example.com/index.js.map `); expect((bundle["example.js"] as OutputChunk).code) .toBe(`console.log("Hello, world!"); //# sourceMappingURL=https://example.com/example.js.map `); }); test("should replace source map URL with extra empty lines", async () => { const bundle: OutputBundle = { "example.js": { code: `console.log("Hello, world!"); //# sourceMappingURL=example.js.map `, map: {}, } as OutputChunk, }; testGenerateBundle((url) => `https://example.com/${url}`, bundle); expect((bundle["example.js"] as OutputChunk).code) .toBe(`console.log("Hello, world!"); //# sourceMappingURL=https://example.com/example.js.map `); }); test("should not replace without a source map comment", async () => { const bundle: OutputBundle = { "example.js": { code: `console.log("Hello, world!");`, map: {}, } as OutputChunk, }; testGenerateBundle((url) => `https://example.com/${url}`, bundle); expect((bundle["example.js"] as OutputChunk).code).toBe( `console.log("Hello, world!");`, ); }); test("should not replace with empty content", async () => { const bundle: OutputBundle = { "example.js": { code: "", map: {}, } as OutputChunk, }; testGenerateBundle((url) => `https://example.com/${url}`, bundle); expect((bundle["example.js"] as OutputChunk).code).toBe(""); }); test("should not replace with identity replace URL function", async () => { const bundle: OutputBundle = { "example.js": { code: `console.log("Hello, world!"); //# sourceMappingURL=example.js.map `, map: {}, } as OutputChunk, }; testGenerateBundle((url) => url, bundle); expect((bundle["example.js"] as OutputChunk).code) .toBe(`console.log("Hello, world!"); //# sourceMappingURL=example.js.map `); }); test("should not replace without a source map object", async () => { const bundle: OutputBundle = { "example.js": { code: `console.log("Hello, world!"); //# sourceMappingURL=example.js.map `, map: null, } as OutputChunk, }; testGenerateBundle((url) => `https://example.com/${url}`, bundle); expect((bundle["example.js"] as OutputChunk).code) .toBe(`console.log("Hello, world!"); //# sourceMappingURL=example.js.map `); }); });