Skip to content

Commit dcb319e

Browse files
ianyongshellscape
authored andcommitted
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
1 parent a683315 commit dcb319e

File tree

4 files changed

+31
-5
lines changed

4 files changed

+31
-5
lines changed

.github/workflows/release.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ jobs:
2121
uses: actions/checkout@v4
2222
with:
2323
fetch-depth: 2
24+
token: ${{ secrets.GH_TOKEN }}
2425

2526
- name: Update Master
2627
run: |
@@ -75,7 +76,7 @@ jobs:
7576
git config pull.rebase false
7677
git config --global user.email "[email protected]"
7778
git config --global user.name "Release Workflow"
78-
git remote set-url origin https://x-access-token:${{ secrets.GH_TOKEN }}@github.com/${{ github.repository }}
79+
git remote set-url origin https://github.com/${{ github.repository }}
7980
8081
- name: pnpm install
8182
run: pnpm install --frozen-lockfile

packages/typescript/src/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import findTypescriptOutput, {
1717
normalizePath,
1818
emitFile,
1919
isDeclarationOutputFile,
20-
isMapOutputFile
20+
isTypeScriptMapOutputFile
2121
} from './outputFile';
2222
import { preflight } from './preflight';
2323
import createWatchProgram, { WatchProgramHelper } from './watchProgram';
@@ -156,11 +156,11 @@ export default function typescript(options: RollupTypescriptOptions = {}): Plugi
156156
},
157157

158158
async generateBundle(outputOptions) {
159-
const declarationAndMapFiles = [...emittedFiles.keys()].filter(
160-
(fileName) => isDeclarationOutputFile(fileName) || isMapOutputFile(fileName)
159+
const declarationAndTypeScriptMapFiles = [...emittedFiles.keys()].filter(
160+
(fileName) => isDeclarationOutputFile(fileName) || isTypeScriptMapOutputFile(fileName)
161161
);
162162

163-
declarationAndMapFiles.forEach((id) => {
163+
declarationAndTypeScriptMapFiles.forEach((id) => {
164164
const code = getEmittedFile(id, emittedFiles, tsCache);
165165
if (!code || !parsedOptions.options.declaration) {
166166
return;

packages/typescript/src/outputFile.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ export function isCodeOutputFile(name: string): boolean {
2424
* Checks if the given OutputFile represents some source map
2525
*/
2626
export function isMapOutputFile(name: string): boolean {
27+
return name.endsWith('.map');
28+
}
29+
30+
/**
31+
* Checks if the given OutputFile represents some TypeScript source map
32+
*/
33+
export function isTypeScriptMapOutputFile(name: string): boolean {
2734
return name.endsWith('ts.map');
2835
}
2936

packages/typescript/test/test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -612,6 +612,24 @@ test.serial('should not emit null sourceContent', async (t) => {
612612
t.false(sourcemap.sourcesContent.includes(undefined));
613613
});
614614

615+
test.serial('should not emit sourceContent that references a non-existent file', async (t) => {
616+
const bundle = await rollup({
617+
input: 'fixtures/basic/main.ts',
618+
output: {
619+
sourcemap: true
620+
},
621+
plugins: [
622+
typescript({
623+
tsconfig: 'fixtures/basic/tsconfig.json'
624+
})
625+
],
626+
onwarn
627+
});
628+
const output = await getCode(bundle, { format: 'es', sourcemap: true }, true);
629+
const sourcemap = output[0].map;
630+
t.false(sourcemap.sourcesContent.includes('//# sourceMappingURL=main.js.map'));
631+
});
632+
615633
test.serial('should not fail if source maps are off', async (t) => {
616634
await t.notThrowsAsync(
617635
rollup({

0 commit comments

Comments
 (0)