|
| 1 | +import * as fs from 'fs'; |
| 2 | +import * as os from 'os'; |
| 3 | +import * as path from 'path'; |
| 4 | +import { afterEach, beforeEach, describe, expect, it } from 'vitest'; |
| 5 | +import { deletePublicSourceMaps } from '../../src/vite/sourceMapDeletion'; |
| 6 | + |
| 7 | +const BUILD_FILES = [ |
| 8 | + '.output/public/_nuxt/entry.DxB3kLmA.js', |
| 9 | + '.output/public/_nuxt/entry.DxB3kLmA.js.map', |
| 10 | + '.output/public/_nuxt/pages/products.Cq81zR4t.js.map', |
| 11 | + '.output/public-assets/_nuxt/entry.DxB3kLmA.js.map', |
| 12 | + '.output/server/chunks/nitro/nitro.mjs.map', |
| 13 | + '.nuxt/dist/server/_nuxt/index-B2kR7mXp.mjs.map', |
| 14 | +]; |
| 15 | + |
| 16 | +let rootDir: string; |
| 17 | +let publicDir: string; |
| 18 | + |
| 19 | +/** Glob patterns need forward slashes, also on Windows. */ |
| 20 | +function toGlob(relativePattern: string): string { |
| 21 | + return path.join(rootDir, relativePattern).split(path.sep).join('/'); |
| 22 | +} |
| 23 | + |
| 24 | +function remainingFiles(): string[] { |
| 25 | + return BUILD_FILES.filter(file => fs.existsSync(path.join(rootDir, file))); |
| 26 | +} |
| 27 | + |
| 28 | +describe('deletePublicSourceMaps', () => { |
| 29 | + beforeEach(() => { |
| 30 | + rootDir = fs.mkdtempSync(path.join(os.tmpdir(), 'sentry-nuxt-source-map-deletion-')); |
| 31 | + publicDir = path.join(rootDir, '.output', 'public'); |
| 32 | + |
| 33 | + for (const file of BUILD_FILES) { |
| 34 | + fs.mkdirSync(path.dirname(path.join(rootDir, file)), { recursive: true }); |
| 35 | + fs.writeFileSync(path.join(rootDir, file), ''); |
| 36 | + } |
| 37 | + }); |
| 38 | + |
| 39 | + afterEach(() => { |
| 40 | + fs.rmSync(rootDir, { recursive: true, force: true }); |
| 41 | + }); |
| 42 | + |
| 43 | + it('deletes the matched files in the public dir and keeps the matched files outside of it', async () => { |
| 44 | + await deletePublicSourceMaps( |
| 45 | + { |
| 46 | + sourcemaps: { |
| 47 | + filesToDeleteAfterUpload: [toGlob('.*/**/public*/**/*.map'), toGlob('.*/**/server/**/*.map')], |
| 48 | + }, |
| 49 | + }, |
| 50 | + publicDir, |
| 51 | + ); |
| 52 | + |
| 53 | + expect(remainingFiles()).toEqual([ |
| 54 | + '.output/public/_nuxt/entry.DxB3kLmA.js', |
| 55 | + '.output/public-assets/_nuxt/entry.DxB3kLmA.js.map', |
| 56 | + '.output/server/chunks/nitro/nitro.mjs.map', |
| 57 | + '.nuxt/dist/server/_nuxt/index-B2kR7mXp.mjs.map', |
| 58 | + ]); |
| 59 | + }); |
| 60 | + |
| 61 | + it('keeps the files in the public dir that no pattern matches', async () => { |
| 62 | + await deletePublicSourceMaps( |
| 63 | + { sourcemaps: { filesToDeleteAfterUpload: toGlob('.output/public/_nuxt/pages/*.map') } }, |
| 64 | + publicDir, |
| 65 | + ); |
| 66 | + |
| 67 | + expect(remainingFiles()).toEqual([ |
| 68 | + '.output/public/_nuxt/entry.DxB3kLmA.js', |
| 69 | + '.output/public/_nuxt/entry.DxB3kLmA.js.map', |
| 70 | + '.output/public-assets/_nuxt/entry.DxB3kLmA.js.map', |
| 71 | + '.output/server/chunks/nitro/nitro.mjs.map', |
| 72 | + '.nuxt/dist/server/_nuxt/index-B2kR7mXp.mjs.map', |
| 73 | + ]); |
| 74 | + }); |
| 75 | + |
| 76 | + it('resolves a promised list of patterns', async () => { |
| 77 | + await deletePublicSourceMaps( |
| 78 | + { sourcemaps: { filesToDeleteAfterUpload: Promise.resolve([toGlob('.output/public/**/*.map')]) } }, |
| 79 | + publicDir, |
| 80 | + ); |
| 81 | + |
| 82 | + expect(remainingFiles()).toEqual([ |
| 83 | + '.output/public/_nuxt/entry.DxB3kLmA.js', |
| 84 | + '.output/public-assets/_nuxt/entry.DxB3kLmA.js.map', |
| 85 | + '.output/server/chunks/nitro/nitro.mjs.map', |
| 86 | + '.nuxt/dist/server/_nuxt/index-B2kR7mXp.mjs.map', |
| 87 | + ]); |
| 88 | + }); |
| 89 | + |
| 90 | + it('deletes nothing when no deletion patterns are set', async () => { |
| 91 | + await deletePublicSourceMaps({ sourcemaps: {} }, publicDir); |
| 92 | + |
| 93 | + expect(remainingFiles()).toEqual(BUILD_FILES); |
| 94 | + }); |
| 95 | +}); |
0 commit comments