|
| 1 | +import * as fs from 'fs'; |
| 2 | +import * as os from 'os'; |
| 3 | +import * as path from 'path'; |
| 4 | +import type { PluginContext } from 'rollup'; |
| 5 | +import { afterEach, beforeEach, describe, expect, it } from 'vitest'; |
| 6 | +import { serviceSourceMapsPlugin } from '../../src/vite/serviceSourceMaps'; |
| 7 | + |
| 8 | +const CODE = 'export default 1;\n'; |
| 9 | +const MAP = '{"version":3,"sources":["../../../../app/pages/index.vue"],"mappings":"AAAA"}'; |
| 10 | + |
| 11 | +let buildDir: string; |
| 12 | + |
| 13 | +function writeFile(file: string, content: string): string { |
| 14 | + fs.mkdirSync(path.dirname(file), { recursive: true }); |
| 15 | + fs.writeFileSync(file, content); |
| 16 | + return file; |
| 17 | +} |
| 18 | + |
| 19 | +function load(id: string): unknown { |
| 20 | + const { load } = serviceSourceMapsPlugin(buildDir); |
| 21 | + if (typeof load !== 'function') { |
| 22 | + throw new Error('Expected `load` to be a function'); |
| 23 | + } |
| 24 | + return load.call({} as PluginContext, id); |
| 25 | +} |
| 26 | + |
| 27 | +describe('serviceSourceMapsPlugin', () => { |
| 28 | + beforeEach(() => { |
| 29 | + buildDir = fs.mkdtempSync(path.join(os.tmpdir(), 'sentry-nuxt-service-source-maps-')); |
| 30 | + }); |
| 31 | + |
| 32 | + afterEach(() => { |
| 33 | + fs.rmSync(buildDir, { recursive: true, force: true }); |
| 34 | + }); |
| 35 | + |
| 36 | + it('loads a service chunk together with its source map', async () => { |
| 37 | + const chunk = writeFile(path.join(buildDir, 'vite', 'services', 'ssr', '_nuxt', 'pages.js'), CODE); |
| 38 | + writeFile(`${chunk}.map`, MAP); |
| 39 | + |
| 40 | + expect(await load(chunk)).toEqual({ code: CODE, map: MAP }); |
| 41 | + }); |
| 42 | + |
| 43 | + it('leaves a service chunk without a source map to the default loader', async () => { |
| 44 | + const chunk = writeFile(path.join(buildDir, 'vite', 'services', 'ssr', '_nuxt', 'export-helper.js'), CODE); |
| 45 | + |
| 46 | + expect(await load(chunk)).toBeNull(); |
| 47 | + }); |
| 48 | + |
| 49 | + it('leaves files outside the service output to the default loader', async () => { |
| 50 | + const file = writeFile(path.join(buildDir, 'sentry-runtime-flags.mjs'), CODE); |
| 51 | + writeFile(`${file}.map`, MAP); |
| 52 | + |
| 53 | + expect(await load(file)).toBeNull(); |
| 54 | + }); |
| 55 | +}); |
0 commit comments