Skip to content

Commit

Permalink
fix(typescript): fix sourcemap sourcecontent referencing non-existent…
Browse files Browse the repository at this point in the history
… files (#1571)

* test(typescript): add test case for incorrect `sourceContent`

* fix(typescript): fix .js.map files being treated as declarations
  • Loading branch information
ianyong authored and shellscape committed Sep 25, 2023
1 parent a683315 commit dcb319e
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 5 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ jobs:
uses: actions/checkout@v4
with:
fetch-depth: 2
token: ${{ secrets.GH_TOKEN }}

- name: Update Master
run: |
Expand Down Expand Up @@ -75,7 +76,7 @@ jobs:
git config pull.rebase false
git config --global user.email "release-workflow@rollup.dev"
git config --global user.name "Release Workflow"
git remote set-url origin https://x-access-token:${{ secrets.GH_TOKEN }}@github.com/${{ github.repository }}
git remote set-url origin https://github.com/${{ github.repository }}
- name: pnpm install
run: pnpm install --frozen-lockfile
Expand Down
8 changes: 4 additions & 4 deletions packages/typescript/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import findTypescriptOutput, {
normalizePath,
emitFile,
isDeclarationOutputFile,
isMapOutputFile
isTypeScriptMapOutputFile
} from './outputFile';
import { preflight } from './preflight';
import createWatchProgram, { WatchProgramHelper } from './watchProgram';
Expand Down Expand Up @@ -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;
Expand Down
7 changes: 7 additions & 0 deletions packages/typescript/src/outputFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

Expand Down
18 changes: 18 additions & 0 deletions packages/typescript/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down

0 comments on commit dcb319e

Please sign in to comment.