|
| 1 | +/** |
| 2 | + * Test for GitHub Issue #829: |
| 3 | + * sentryVitePlugin creates unnecessary JS modules for each index page |
| 4 | + * |
| 5 | + * In a Vite multi-page app (MPA), sentryVitePlugin causes "vite build" to emit |
| 6 | + * a separate, unique, unexpected JS module for each index page. |
| 7 | + * |
| 8 | + * Expected: The plugin should add metadata to generated modules but should NOT |
| 9 | + * rework the import graph, create new modules, or add script tags to pages |
| 10 | + * that didn't have them in the first place. |
| 11 | + * |
| 12 | + * Actual: A unique module is generated for every HTML page in rollup.options.input. |
| 13 | + * Pages that had been sharing modules will instead load a page-specific module |
| 14 | + * that imports the shared code. Pages that didn't contain ANY scripts will now have one. |
| 15 | + */ |
| 16 | +import childProcess from "child_process"; |
| 17 | +import fs from "fs"; |
| 18 | +import path from "path"; |
| 19 | + |
| 20 | +function getAssetFiles(outDir: string): string[] { |
| 21 | + const assetsDir = path.join(outDir, "assets"); |
| 22 | + if (!fs.existsSync(assetsDir)) { |
| 23 | + return []; |
| 24 | + } |
| 25 | + return fs.readdirSync(assetsDir).filter((file) => file.endsWith(".js")); |
| 26 | +} |
| 27 | + |
| 28 | +function getScriptTagsFromHtml(htmlPath: string): string[] { |
| 29 | + if (!fs.existsSync(htmlPath)) { |
| 30 | + return []; |
| 31 | + } |
| 32 | + const content = fs.readFileSync(htmlPath, "utf-8"); |
| 33 | + const scriptMatches = content.match(/<script[^>]*>/g) || []; |
| 34 | + return scriptMatches; |
| 35 | +} |
| 36 | + |
| 37 | +describe("Vite MPA Extra Modules Issue (#829)", () => { |
| 38 | + const outWithoutPlugin = path.join(__dirname, "out", "without-plugin"); |
| 39 | + const outWithPlugin = path.join(__dirname, "out", "with-plugin"); |
| 40 | + |
| 41 | + beforeAll(() => { |
| 42 | + // Clean output directories |
| 43 | + if (fs.existsSync(outWithoutPlugin)) { |
| 44 | + fs.rmSync(outWithoutPlugin, { recursive: true }); |
| 45 | + } |
| 46 | + if (fs.existsSync(outWithPlugin)) { |
| 47 | + fs.rmSync(outWithPlugin, { recursive: true }); |
| 48 | + } |
| 49 | + |
| 50 | + // Build without plugin |
| 51 | + childProcess.execSync( |
| 52 | + `yarn ts-node ${path.join(__dirname, "build-vite-without-plugin.ts")}`, |
| 53 | + { encoding: "utf-8", stdio: "inherit" } |
| 54 | + ); |
| 55 | + |
| 56 | + // Build with plugin |
| 57 | + childProcess.execSync( |
| 58 | + `yarn ts-node ${path.join(__dirname, "build-vite-with-plugin.ts")}`, |
| 59 | + { encoding: "utf-8", stdio: "inherit" } |
| 60 | + ); |
| 61 | + }, 60_000); |
| 62 | + |
| 63 | + it("should not create extra JS modules when sentry plugin is enabled", () => { |
| 64 | + const assetsWithoutPlugin = getAssetFiles(outWithoutPlugin); |
| 65 | + const assetsWithPlugin = getAssetFiles(outWithPlugin); |
| 66 | + |
| 67 | + console.log("Assets without plugin:", assetsWithoutPlugin); |
| 68 | + console.log("Assets with plugin:", assetsWithPlugin); |
| 69 | + |
| 70 | + // The number of JS files should be the same (or very close) |
| 71 | + // With the bug, the plugin creates extra modules like index.js, page1.js, page2.js |
| 72 | + // for each HTML entry point |
| 73 | + expect(assetsWithPlugin.length).toBeLessThanOrEqual(assetsWithoutPlugin.length + 1); |
| 74 | + }); |
| 75 | + |
| 76 | + it("should not add script tags to HTML pages that had none", () => { |
| 77 | + // index.html originally has no scripts |
| 78 | + const indexWithoutPlugin = getScriptTagsFromHtml( |
| 79 | + path.join(outWithoutPlugin, "index.html") |
| 80 | + ); |
| 81 | + const indexWithPlugin = getScriptTagsFromHtml( |
| 82 | + path.join(outWithPlugin, "index.html") |
| 83 | + ); |
| 84 | + |
| 85 | + console.log("index.html scripts without plugin:", indexWithoutPlugin); |
| 86 | + console.log("index.html scripts with plugin:", indexWithPlugin); |
| 87 | + |
| 88 | + // The number of script tags should be the same |
| 89 | + // With the bug, index.html gets a script tag added even though it had none |
| 90 | + expect(indexWithPlugin.length).toBe(indexWithoutPlugin.length); |
| 91 | + }); |
| 92 | + |
| 93 | + it("should preserve shared module imports without creating page-specific wrappers", () => { |
| 94 | + // page1.html and page2.html should both reference the same shared module |
| 95 | + // not page-specific modules |
| 96 | + const page1WithPlugin = fs.readFileSync( |
| 97 | + path.join(outWithPlugin, "page1.html"), |
| 98 | + "utf-8" |
| 99 | + ); |
| 100 | + const page2WithPlugin = fs.readFileSync( |
| 101 | + path.join(outWithPlugin, "page2.html"), |
| 102 | + "utf-8" |
| 103 | + ); |
| 104 | + |
| 105 | + // Extract the JS file references |
| 106 | + const page1ScriptMatch = page1WithPlugin.match(/src="([^"]+\.js)"/); |
| 107 | + const page2ScriptMatch = page2WithPlugin.match(/src="([^"]+\.js)"/); |
| 108 | + |
| 109 | + console.log("page1 script src:", page1ScriptMatch?.[1]); |
| 110 | + console.log("page2 script src:", page2ScriptMatch?.[1]); |
| 111 | + |
| 112 | + if (page1ScriptMatch && page2ScriptMatch) { |
| 113 | + // Both pages should reference the same shared module, not page-specific ones |
| 114 | + // With the bug, page1.html references page1-xxx.js and page2.html references page2-xxx.js |
| 115 | + // instead of both referencing shared-module-xxx.js |
| 116 | + const page1Script = page1ScriptMatch[1]; |
| 117 | + const page2Script = page2ScriptMatch[1]; |
| 118 | + |
| 119 | + // They should NOT be page-specific (named after the page) |
| 120 | + expect(page1Script).not.toMatch(/page1/i); |
| 121 | + expect(page2Script).not.toMatch(/page2/i); |
| 122 | + } |
| 123 | + }); |
| 124 | +}); |
| 125 | + |
0 commit comments