|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import { mkdtemp, mkdir, readFile, rm, writeFile } from 'node:fs/promises'; |
| 3 | +import { tmpdir } from 'node:os'; |
| 4 | +import { join, relative, resolve } from 'node:path'; |
| 5 | +import test from 'node:test'; |
| 6 | +import { rewritePublishedSourceMaps } from '../../scripts/support/published-source-maps.mjs'; |
| 7 | + |
| 8 | +test('staged JS and declaration maps resolve from the published tree and preserve mappings', async (context) => { |
| 9 | + const root = await mkdtemp(join(tmpdir(), 'glyph-source-maps-')); |
| 10 | + context.after(() => rm(root, { recursive: true, force: true })); |
| 11 | + const staging = join(root, '.cache/build/staging/internal'); |
| 12 | + const source = join(root, 'src/internal'); |
| 13 | + await mkdir(staging, { recursive: true }); |
| 14 | + await mkdir(source, { recursive: true }); |
| 15 | + const original = { |
| 16 | + version: 3, |
| 17 | + sources: ['module.ts'], |
| 18 | + sourceRoot: relative(staging, source), |
| 19 | + names: ['value'], |
| 20 | + mappings: 'AAAA', |
| 21 | + sourcesContent: ['export const value = 1;'], |
| 22 | + }; |
| 23 | + for (const suffix of ['js', 'd.ts']) await writeFile(join(staging, `module.${suffix}.map`), JSON.stringify(original)); |
| 24 | + await rewritePublishedSourceMaps(join(root, '.cache/build/staging'), join(root, 'dist'), join(root, 'src')); |
| 25 | + for (const suffix of ['js', 'd.ts']) { |
| 26 | + const map = JSON.parse(await readFile(join(staging, `module.${suffix}.map`), 'utf8')); |
| 27 | + assert.equal(resolve(root, 'dist/internal', map.sources[0]), join(source, 'module.ts')); |
| 28 | + const { sourceRoot: _sourceRoot, ...unchanged } = original; |
| 29 | + assert.deepEqual(map, { ...unchanged, sources: ['../../src/internal/module.ts'] }); |
| 30 | + } |
| 31 | +}); |
| 32 | + |
| 33 | +test('source maps cannot escape the packaged source tree', async (context) => { |
| 34 | + const root = await mkdtemp(join(tmpdir(), 'glyph-source-map-escape-')); |
| 35 | + context.after(() => rm(root, { recursive: true, force: true })); |
| 36 | + await mkdir(join(root, 'stage')); |
| 37 | + await writeFile(join(root, 'stage/module.js.map'), JSON.stringify({ version: 3, sources: ['../../outside.ts'] })); |
| 38 | + await assert.rejects( |
| 39 | + rewritePublishedSourceMaps(join(root, 'stage'), join(root, 'dist'), join(root, 'src')), |
| 40 | + /outside the published tree/, |
| 41 | + ); |
| 42 | +}); |
0 commit comments