rollup / plugins

🍣 The one-stop shop for official Rollup plugins
MIT License
3.57k stars 567 forks source link

Source URLs in generated source map are wrong when `composite` is enabled in tsconfig.json #1715

Open pqnet opened 2 months ago

pqnet commented 2 months ago

Expected Behavior

Section sources in the generated source map (dist/index.js.map) should contain a valid path relative to the source file used to generate the bundle, relative to the position of the source map file itself, in this case ../src/index.ts

Actual Behavior

The path in sources map contains an extra ../ segment, i.e. ../../src/index.ts

Additional Information

Setting composite to true instructs the typescript compiler to copy the directory structure of the source files relative to the root dir (by default it's the tsconfig.json file location), hence it expects the source map file to be generated in dist/src/index.js.map, which does not match the path in the imported module ID.

The root cause is that the source map produced by Typescript has its path relative to the output file generated by Typescript, while the one expected by rollup should be relative to the module id (the input file in this case). This happens to match if the input (.ts) file and the output (.js) files are in the same directory or in directories with the same path depth.

See https://github.com/rollup/plugins/blob/master/packages/typescript/src/outputFile.ts#L89