From 902fb3d3408c2ad5ac944d3d6fa5ea24ee0ee384 Mon Sep 17 00:00:00 2001 From: Ian Yong Date: Mon, 25 Sep 2023 22:41:09 +0800 Subject: [PATCH] fix(typescript): fix sourcemap sourcecontent referencing non-existent files (#1571) * test(typescript): add test case for incorrect `sourceContent` * fix(typescript): fix .js.map files being treated as declarations --- packages/typescript/src/index.ts | 8 ++++---- packages/typescript/src/outputFile.ts | 7 +++++++ packages/typescript/test/test.js | 18 ++++++++++++++++++ 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/packages/typescript/src/index.ts b/packages/typescript/src/index.ts index 293e341d9..bea3e736d 100644 --- a/packages/typescript/src/index.ts +++ b/packages/typescript/src/index.ts @@ -17,7 +17,7 @@ import findTypescriptOutput, { normalizePath, emitFile, isDeclarationOutputFile, - isMapOutputFile + isTypeScriptMapOutputFile } from './outputFile'; import { preflight } from './preflight'; import createWatchProgram, { WatchProgramHelper } from './watchProgram'; @@ -156,11 +156,11 @@ export default function typescript(options: RollupTypescriptOptions = {}): Plugi }, async generateBundle(outputOptions) { - const declarationAndMapFiles = [...emittedFiles.keys()].filter( - (fileName) => isDeclarationOutputFile(fileName) || isMapOutputFile(fileName) + const declarationAndTypeScriptMapFiles = [...emittedFiles.keys()].filter( + (fileName) => isDeclarationOutputFile(fileName) || isTypeScriptMapOutputFile(fileName) ); - declarationAndMapFiles.forEach((id) => { + declarationAndTypeScriptMapFiles.forEach((id) => { const code = getEmittedFile(id, emittedFiles, tsCache); if (!code || !parsedOptions.options.declaration) { return; diff --git a/packages/typescript/src/outputFile.ts b/packages/typescript/src/outputFile.ts index 4dc0de04d..d7b080ab1 100644 --- a/packages/typescript/src/outputFile.ts +++ b/packages/typescript/src/outputFile.ts @@ -24,6 +24,13 @@ export function isCodeOutputFile(name: string): boolean { * Checks if the given OutputFile represents some source map */ export function isMapOutputFile(name: string): boolean { + return name.endsWith('.map'); +} + +/** + * Checks if the given OutputFile represents some TypeScript source map + */ +export function isTypeScriptMapOutputFile(name: string): boolean { return name.endsWith('ts.map'); } diff --git a/packages/typescript/test/test.js b/packages/typescript/test/test.js index 27cb33a10..e746680c1 100644 --- a/packages/typescript/test/test.js +++ b/packages/typescript/test/test.js @@ -612,6 +612,24 @@ test.serial('should not emit null sourceContent', async (t) => { t.false(sourcemap.sourcesContent.includes(undefined)); }); +test.serial('should not emit sourceContent that references a non-existent file', async (t) => { + const bundle = await rollup({ + input: 'fixtures/basic/main.ts', + output: { + sourcemap: true + }, + plugins: [ + typescript({ + tsconfig: 'fixtures/basic/tsconfig.json' + }) + ], + onwarn + }); + const output = await getCode(bundle, { format: 'es', sourcemap: true }, true); + const sourcemap = output[0].map; + t.false(sourcemap.sourcesContent.includes('//# sourceMappingURL=main.js.map')); +}); + test.serial('should not fail if source maps are off', async (t) => { await t.notThrowsAsync( rollup({