|
| 1 | +import * as fs from 'fs'; |
| 2 | +import * as os from 'os'; |
| 3 | +import * as path from 'path'; |
| 4 | +import { rollup } from 'rollup'; |
| 5 | +import { rolldown } from 'rolldown'; |
| 6 | +import { describe, it, expect, beforeEach, afterEach } from 'vitest'; |
| 7 | +import { sentryRollupPlugin } from '../../src/rollup'; |
| 8 | +import { sentryVitePlugin } from '../../src/vite'; |
| 9 | + |
| 10 | +const RELEASE_SNIPPET = /SENTRY_RELEASE\s*=\s*\{\s*id:\s*"1\.2\.3"\s*\}/; |
| 11 | +const DEBUG_ID_MARKER = /sentry-dbid-[0-9a-f-]{36}/; |
| 12 | + |
| 13 | +function readChunks(outDir: string): Record<string, string> { |
| 14 | + const chunks: Record<string, string> = {}; |
| 15 | + for (const fileName of fs.readdirSync(outDir)) { |
| 16 | + if (fileName.endsWith('.js')) { |
| 17 | + chunks[fileName] = fs.readFileSync(path.join(outDir, fileName), 'utf8'); |
| 18 | + } |
| 19 | + } |
| 20 | + return chunks; |
| 21 | +} |
| 22 | + |
| 23 | +function findChunk(chunks: Record<string, string>, content: string): string { |
| 24 | + const match = Object.entries(chunks).find(([, code]) => code.includes(content)); |
| 25 | + expect(match, `no chunk contains ${content}`).toBeDefined(); |
| 26 | + return match?.[1] ?? ''; |
| 27 | +} |
| 28 | + |
| 29 | +describe('release injection', () => { |
| 30 | + let tmpDir: string; |
| 31 | + let entries: string[]; |
| 32 | + |
| 33 | + beforeEach(() => { |
| 34 | + tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'sentry-rollup-release-injection-')); |
| 35 | + entries = [path.join(tmpDir, 'entry.js'), path.join(tmpDir, 'second.js')]; |
| 36 | + fs.writeFileSync( |
| 37 | + entries[0] ?? '', |
| 38 | + 'import { shared } from "./shared.js";\nconsole.log("entry", shared);\nimport("./route.js");\n', |
| 39 | + ); |
| 40 | + fs.writeFileSync(entries[1] ?? '', 'import { shared } from "./shared.js";\nconsole.log("second", shared);\n'); |
| 41 | + fs.writeFileSync(path.join(tmpDir, 'shared.js'), 'export const shared = "shared";\nconsole.log(shared);\n'); |
| 42 | + fs.writeFileSync( |
| 43 | + path.join(tmpDir, 'route.js'), |
| 44 | + 'import { shared } from "./shared.js";\nconsole.log("route", shared);\n', |
| 45 | + ); |
| 46 | + }); |
| 47 | + |
| 48 | + afterEach(() => { |
| 49 | + fs.rmSync(tmpDir, { recursive: true, force: true }); |
| 50 | + }); |
| 51 | + |
| 52 | + it('injects the release only into chunks that load at startup (rollup)', async () => { |
| 53 | + const outDir = path.join(tmpDir, 'dist'); |
| 54 | + const build = await rollup({ |
| 55 | + input: entries, |
| 56 | + plugins: [sentryRollupPlugin({ telemetry: false, release: { name: '1.2.3', create: false } })], |
| 57 | + }); |
| 58 | + await build.write({ dir: outDir, chunkFileNames: '[name].js' }); |
| 59 | + |
| 60 | + const chunks = readChunks(outDir); |
| 61 | + expect(Object.keys(chunks)).toHaveLength(4); |
| 62 | + |
| 63 | + const entryChunk = findChunk(chunks, '"entry"'); |
| 64 | + const secondChunk = findChunk(chunks, '"second"'); |
| 65 | + const sharedChunk = findChunk(chunks, '"shared"'); |
| 66 | + const routeChunk = findChunk(chunks, '"route"'); |
| 67 | + |
| 68 | + expect(entryChunk).toMatch(RELEASE_SNIPPET); |
| 69 | + expect(secondChunk).toMatch(RELEASE_SNIPPET); |
| 70 | + expect(sharedChunk).toMatch(RELEASE_SNIPPET); |
| 71 | + expect(routeChunk).not.toMatch(RELEASE_SNIPPET); |
| 72 | + |
| 73 | + expect(entryChunk).toMatch(DEBUG_ID_MARKER); |
| 74 | + expect(sharedChunk).toMatch(DEBUG_ID_MARKER); |
| 75 | + expect(routeChunk).toMatch(DEBUG_ID_MARKER); |
| 76 | + }); |
| 77 | + |
| 78 | + it('injects the release only into chunks that load at startup (rolldown / vite)', async () => { |
| 79 | + const outDir = path.join(tmpDir, 'dist'); |
| 80 | + const build = await rolldown({ |
| 81 | + input: entries, |
| 82 | + plugins: sentryVitePlugin({ telemetry: false, release: { name: '1.2.3', create: false } }), |
| 83 | + }); |
| 84 | + await build.write({ dir: outDir, chunkFileNames: '[name].js' }); |
| 85 | + |
| 86 | + const chunks = readChunks(outDir); |
| 87 | + expect(Object.keys(chunks)).toHaveLength(4); |
| 88 | + |
| 89 | + const entryChunk = findChunk(chunks, '"entry"'); |
| 90 | + const secondChunk = findChunk(chunks, '"second"'); |
| 91 | + const sharedChunk = findChunk(chunks, '"shared"'); |
| 92 | + const routeChunk = findChunk(chunks, '"route"'); |
| 93 | + |
| 94 | + expect(entryChunk).toMatch(RELEASE_SNIPPET); |
| 95 | + expect(secondChunk).toMatch(RELEASE_SNIPPET); |
| 96 | + expect(sharedChunk).toMatch(RELEASE_SNIPPET); |
| 97 | + expect(routeChunk).not.toMatch(RELEASE_SNIPPET); |
| 98 | + |
| 99 | + expect(entryChunk).toMatch(DEBUG_ID_MARKER); |
| 100 | + expect(sharedChunk).toMatch(DEBUG_ID_MARKER); |
| 101 | + expect(routeChunk).toMatch(DEBUG_ID_MARKER); |
| 102 | + }); |
| 103 | +}); |
0 commit comments