Skip to content

Commit 6ca0cb0

Browse files
authored
fix: correct sourcemap with treeshake (#1069)
1 parent 43cf9f6 commit 6ca0cb0

3 files changed

Lines changed: 38 additions & 6 deletions

File tree

src/plugin.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,8 @@ export class PluginContainer {
168168
const newConsumer = await new SourceMapConsumer(
169169
parseSourceMap(result.map),
170170
)
171-
const generator =
172-
SourceMapGenerator.fromSourceMap(originalConsumer)
173-
generator.applySourceMap(newConsumer, info.path)
171+
const generator = SourceMapGenerator.fromSourceMap(newConsumer)
172+
generator.applySourceMap(originalConsumer, info.path)
174173
info.map = generator.toJSON()
175174
originalConsumer.destroy()
176175
newConsumer.destroy()

src/plugins/tree-shaking.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { rollup, type TreeshakingOptions, type TreeshakingPreset } from 'rollup'
22
import type { Plugin } from '../plugin'
3+
import path from 'path'
34

45
export type TreeshakingStrategy =
56
| boolean
@@ -31,7 +32,7 @@ export const treeShakingPlugin = ({
3132
return false
3233
},
3334
load(id) {
34-
if (id === info.path) return code
35+
if (id === info.path) return { code, map: info.map }
3536
},
3637
},
3738
],
@@ -44,14 +45,14 @@ export const treeShakingPlugin = ({
4445
const result = await bundle.generate({
4546
interop: 'auto',
4647
format: this.format,
47-
file: 'out.js',
48+
file: info.path,
4849
sourcemap: !!this.options.sourcemap,
4950
name,
5051
})
5152

5253
for (const file of result.output) {
5354
if (file.type === 'chunk') {
54-
if (file.fileName.endsWith('out.js')) {
55+
if (file.fileName === path.basename(info.path)) {
5556
return {
5657
code: file.code,
5758
map: file.map,

test/index.test.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -856,3 +856,35 @@ test('should load postcss esm config', async () => {
856856
expect(outFiles).toEqual(['input.cjs', 'input.css'])
857857
expect(await getFileContent('dist/input.css')).toContain('color: blue;')
858858
})
859+
860+
test('generate sourcemap with --treeshake', async () => {
861+
const sourceCode = 'export function getValue(val: any){ return val; }'
862+
const { outFiles, getFileContent } = await run(
863+
getTestName(),
864+
{
865+
'src/input.ts': sourceCode,
866+
},
867+
{
868+
entry: ['src/input.ts'],
869+
flags: ['--treeshake', '--sourcemap', '--format=cjs,esm,iife'],
870+
},
871+
)
872+
873+
expect(outFiles.length).toBe(6)
874+
875+
await Promise.all(
876+
outFiles
877+
.filter((fileName) => fileName.endsWith('.map'))
878+
.map(async (sourceMapFile) => {
879+
const sourceMap = await getFileContent(`dist/${sourceMapFile}`).then(
880+
(rawContent) => JSON.parse(rawContent),
881+
)
882+
883+
expect(sourceMap.sources[0]).toBe('../src/input.ts')
884+
expect(sourceMap.sourcesContent[0]).toBe(sourceCode)
885+
886+
const outputFileName = sourceMapFile.replace('.map', '')
887+
expect(sourceMap.file).toBe(outputFileName)
888+
}),
889+
)
890+
})

0 commit comments

Comments
 (0)