|
1 | 1 | import { loadModule } from '@sentry/core'; |
2 | | -import { beforeEach, describe, expect, it, vi } from 'vitest'; |
3 | | -import { handleRunAfterProductionCompile } from '../../src/config/handleRunAfterProductionCompile'; |
| 2 | +import * as fs from 'fs'; |
| 3 | +import * as os from 'os'; |
| 4 | +import * as path from 'path'; |
| 5 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 6 | +import { |
| 7 | + handleRunAfterProductionCompile, |
| 8 | + stripSourceMappingURLComments, |
| 9 | +} from '../../src/config/handleRunAfterProductionCompile'; |
4 | 10 | import type { SentryBuildOptions } from '../../src/config/types'; |
5 | 11 |
|
6 | 12 | vi.mock('@sentry/core', () => ({ |
@@ -305,6 +311,85 @@ describe('handleRunAfterProductionCompile', () => { |
305 | 311 | }); |
306 | 312 | }); |
307 | 313 |
|
| 314 | + describe('sourceMappingURL stripping', () => { |
| 315 | + let readdirSpy: ReturnType<typeof vi.spyOn>; |
| 316 | + |
| 317 | + beforeEach(() => { |
| 318 | + // Spy on fs.promises.readdir to detect whether stripping was attempted. |
| 319 | + // The actual readdir will fail (dir doesn't exist), which is fine — we just |
| 320 | + // need to know if it was called. |
| 321 | + readdirSpy = vi.spyOn(fs.promises, 'readdir').mockRejectedValue(new Error('ENOENT')); |
| 322 | + }); |
| 323 | + |
| 324 | + afterEach(() => { |
| 325 | + readdirSpy.mockRestore(); |
| 326 | + }); |
| 327 | + |
| 328 | + it('strips sourceMappingURL comments for turbopack builds with deleteSourcemapsAfterUpload', async () => { |
| 329 | + await handleRunAfterProductionCompile( |
| 330 | + { |
| 331 | + releaseName: 'test-release', |
| 332 | + distDir: '/path/to/.next', |
| 333 | + buildTool: 'turbopack', |
| 334 | + }, |
| 335 | + { |
| 336 | + ...mockSentryBuildOptions, |
| 337 | + sourcemaps: { deleteSourcemapsAfterUpload: true }, |
| 338 | + }, |
| 339 | + ); |
| 340 | + |
| 341 | + expect(readdirSpy).toHaveBeenCalledWith( |
| 342 | + path.join('/path/to/.next', 'static'), |
| 343 | + expect.objectContaining({ recursive: true }), |
| 344 | + ); |
| 345 | + }); |
| 346 | + |
| 347 | + it('does NOT strip sourceMappingURL comments for webpack builds even with deleteSourcemapsAfterUpload', async () => { |
| 348 | + await handleRunAfterProductionCompile( |
| 349 | + { |
| 350 | + releaseName: 'test-release', |
| 351 | + distDir: '/path/to/.next', |
| 352 | + buildTool: 'webpack', |
| 353 | + }, |
| 354 | + { |
| 355 | + ...mockSentryBuildOptions, |
| 356 | + sourcemaps: { deleteSourcemapsAfterUpload: true }, |
| 357 | + }, |
| 358 | + ); |
| 359 | + |
| 360 | + expect(readdirSpy).not.toHaveBeenCalled(); |
| 361 | + }); |
| 362 | + |
| 363 | + it('does NOT strip sourceMappingURL comments when deleteSourcemapsAfterUpload is false', async () => { |
| 364 | + await handleRunAfterProductionCompile( |
| 365 | + { |
| 366 | + releaseName: 'test-release', |
| 367 | + distDir: '/path/to/.next', |
| 368 | + buildTool: 'turbopack', |
| 369 | + }, |
| 370 | + { |
| 371 | + ...mockSentryBuildOptions, |
| 372 | + sourcemaps: { deleteSourcemapsAfterUpload: false }, |
| 373 | + }, |
| 374 | + ); |
| 375 | + |
| 376 | + expect(readdirSpy).not.toHaveBeenCalled(); |
| 377 | + }); |
| 378 | + |
| 379 | + it('does NOT strip sourceMappingURL comments when deleteSourcemapsAfterUpload is undefined', async () => { |
| 380 | + await handleRunAfterProductionCompile( |
| 381 | + { |
| 382 | + releaseName: 'test-release', |
| 383 | + distDir: '/path/to/.next', |
| 384 | + buildTool: 'turbopack', |
| 385 | + }, |
| 386 | + mockSentryBuildOptions, |
| 387 | + ); |
| 388 | + |
| 389 | + expect(readdirSpy).not.toHaveBeenCalled(); |
| 390 | + }); |
| 391 | + }); |
| 392 | + |
308 | 393 | describe('path handling', () => { |
309 | 394 | it('correctly passes distDir to debug ID injection', async () => { |
310 | 395 | const customDistDir = '/custom/dist/path'; |
@@ -343,3 +428,112 @@ describe('handleRunAfterProductionCompile', () => { |
343 | 428 | }); |
344 | 429 | }); |
345 | 430 | }); |
| 431 | + |
| 432 | +describe('stripSourceMappingURLComments', () => { |
| 433 | + let tmpDir: string; |
| 434 | + |
| 435 | + beforeEach(async () => { |
| 436 | + tmpDir = await fs.promises.mkdtemp(path.join(os.tmpdir(), 'sentry-test-')); |
| 437 | + await fs.promises.mkdir(path.join(tmpDir, 'chunks'), { recursive: true }); |
| 438 | + }); |
| 439 | + |
| 440 | + afterEach(async () => { |
| 441 | + await fs.promises.rm(tmpDir, { recursive: true, force: true }); |
| 442 | + }); |
| 443 | + |
| 444 | + it('strips sourceMappingURL comment from JS files', async () => { |
| 445 | + const filePath = path.join(tmpDir, 'chunks', 'abc123.js'); |
| 446 | + await fs.promises.writeFile(filePath, 'console.log("hello");\n//# sourceMappingURL=abc123.js.map'); |
| 447 | + |
| 448 | + await stripSourceMappingURLComments(tmpDir); |
| 449 | + |
| 450 | + const content = await fs.promises.readFile(filePath, 'utf-8'); |
| 451 | + expect(content).toBe('console.log("hello");'); |
| 452 | + expect(content).not.toContain('sourceMappingURL'); |
| 453 | + }); |
| 454 | + |
| 455 | + it('strips sourceMappingURL comment from MJS files', async () => { |
| 456 | + const filePath = path.join(tmpDir, 'chunks', 'module.mjs'); |
| 457 | + await fs.promises.writeFile(filePath, 'export default 42;\n//# sourceMappingURL=module.mjs.map'); |
| 458 | + |
| 459 | + await stripSourceMappingURLComments(tmpDir); |
| 460 | + |
| 461 | + const content = await fs.promises.readFile(filePath, 'utf-8'); |
| 462 | + expect(content).toBe('export default 42;'); |
| 463 | + }); |
| 464 | + |
| 465 | + it('strips sourceMappingURL comment from CSS files', async () => { |
| 466 | + const filePath = path.join(tmpDir, 'chunks', 'styles.css'); |
| 467 | + await fs.promises.writeFile(filePath, '.foo { color: red; }\n/*# sourceMappingURL=styles.css.map */'); |
| 468 | + |
| 469 | + await stripSourceMappingURLComments(tmpDir); |
| 470 | + |
| 471 | + const content = await fs.promises.readFile(filePath, 'utf-8'); |
| 472 | + expect(content).toBe('.foo { color: red; }'); |
| 473 | + }); |
| 474 | + |
| 475 | + it('does not modify files without sourceMappingURL comments', async () => { |
| 476 | + const filePath = path.join(tmpDir, 'chunks', 'clean.js'); |
| 477 | + const originalContent = 'console.log("no source map ref");'; |
| 478 | + await fs.promises.writeFile(filePath, originalContent); |
| 479 | + |
| 480 | + await stripSourceMappingURLComments(tmpDir); |
| 481 | + |
| 482 | + const content = await fs.promises.readFile(filePath, 'utf-8'); |
| 483 | + expect(content).toBe(originalContent); |
| 484 | + }); |
| 485 | + |
| 486 | + it('handles files in nested subdirectories', async () => { |
| 487 | + const nestedDir = path.join(tmpDir, 'chunks', 'app', 'page'); |
| 488 | + await fs.promises.mkdir(nestedDir, { recursive: true }); |
| 489 | + const filePath = path.join(nestedDir, 'layout.js'); |
| 490 | + await fs.promises.writeFile(filePath, 'var x = 1;\n//# sourceMappingURL=layout.js.map'); |
| 491 | + |
| 492 | + await stripSourceMappingURLComments(tmpDir); |
| 493 | + |
| 494 | + const content = await fs.promises.readFile(filePath, 'utf-8'); |
| 495 | + expect(content).toBe('var x = 1;'); |
| 496 | + }); |
| 497 | + |
| 498 | + it('handles non-existent directory gracefully', async () => { |
| 499 | + await expect(stripSourceMappingURLComments('/nonexistent/path')).resolves.toBeUndefined(); |
| 500 | + }); |
| 501 | + |
| 502 | + it('handles sourceMappingURL with @-style comment', async () => { |
| 503 | + const filePath = path.join(tmpDir, 'chunks', 'legacy.js'); |
| 504 | + await fs.promises.writeFile(filePath, 'var y = 2;\n//@ sourceMappingURL=legacy.js.map'); |
| 505 | + |
| 506 | + await stripSourceMappingURLComments(tmpDir); |
| 507 | + |
| 508 | + const content = await fs.promises.readFile(filePath, 'utf-8'); |
| 509 | + expect(content).toBe('var y = 2;'); |
| 510 | + }); |
| 511 | + |
| 512 | + it('ignores non-JS/CSS files', async () => { |
| 513 | + const filePath = path.join(tmpDir, 'chunks', 'data.json'); |
| 514 | + const originalContent = '{"key": "value"}\n//# sourceMappingURL=data.json.map'; |
| 515 | + await fs.promises.writeFile(filePath, originalContent); |
| 516 | + |
| 517 | + await stripSourceMappingURLComments(tmpDir); |
| 518 | + |
| 519 | + const content = await fs.promises.readFile(filePath, 'utf-8'); |
| 520 | + expect(content).toBe(originalContent); |
| 521 | + }); |
| 522 | + |
| 523 | + it('processes multiple files concurrently', async () => { |
| 524 | + const files = ['a.js', 'b.mjs', 'c.cjs', 'd.css']; |
| 525 | + for (const file of files) { |
| 526 | + const ext = path.extname(file); |
| 527 | + const comment = ext === '.css' ? `/*# sourceMappingURL=${file}.map */` : `//# sourceMappingURL=${file}.map`; |
| 528 | + await fs.promises.writeFile(path.join(tmpDir, file), `content_${file}\n${comment}`); |
| 529 | + } |
| 530 | + |
| 531 | + await stripSourceMappingURLComments(tmpDir); |
| 532 | + |
| 533 | + for (const file of files) { |
| 534 | + const content = await fs.promises.readFile(path.join(tmpDir, file), 'utf-8'); |
| 535 | + expect(content).toBe(`content_${file}`); |
| 536 | + expect(content).not.toContain('sourceMappingURL'); |
| 537 | + } |
| 538 | + }); |
| 539 | +}); |
0 commit comments