Skip to content

Commit

Permalink
fix: correct sourcemap with treeshake (#1069)
Browse files Browse the repository at this point in the history
  • Loading branch information
xia0hj authored Jul 17, 2024
1 parent 43cf9f6 commit 6ca0cb0
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 6 deletions.
5 changes: 2 additions & 3 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,8 @@ export class PluginContainer {
const newConsumer = await new SourceMapConsumer(
parseSourceMap(result.map),
)
const generator =
SourceMapGenerator.fromSourceMap(originalConsumer)
generator.applySourceMap(newConsumer, info.path)
const generator = SourceMapGenerator.fromSourceMap(newConsumer)
generator.applySourceMap(originalConsumer, info.path)
info.map = generator.toJSON()
originalConsumer.destroy()
newConsumer.destroy()
Expand Down
7 changes: 4 additions & 3 deletions src/plugins/tree-shaking.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { rollup, type TreeshakingOptions, type TreeshakingPreset } from 'rollup'
import type { Plugin } from '../plugin'
import path from 'path'

export type TreeshakingStrategy =
| boolean
Expand Down Expand Up @@ -31,7 +32,7 @@ export const treeShakingPlugin = ({
return false
},
load(id) {
if (id === info.path) return code
if (id === info.path) return { code, map: info.map }
},
},
],
Expand All @@ -44,14 +45,14 @@ export const treeShakingPlugin = ({
const result = await bundle.generate({
interop: 'auto',
format: this.format,
file: 'out.js',
file: info.path,
sourcemap: !!this.options.sourcemap,
name,
})

for (const file of result.output) {
if (file.type === 'chunk') {
if (file.fileName.endsWith('out.js')) {
if (file.fileName === path.basename(info.path)) {
return {
code: file.code,
map: file.map,
Expand Down
32 changes: 32 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -856,3 +856,35 @@ test('should load postcss esm config', async () => {
expect(outFiles).toEqual(['input.cjs', 'input.css'])
expect(await getFileContent('dist/input.css')).toContain('color: blue;')
})

test('generate sourcemap with --treeshake', async () => {
const sourceCode = 'export function getValue(val: any){ return val; }'
const { outFiles, getFileContent } = await run(
getTestName(),
{
'src/input.ts': sourceCode,
},
{
entry: ['src/input.ts'],
flags: ['--treeshake', '--sourcemap', '--format=cjs,esm,iife'],
},
)

expect(outFiles.length).toBe(6)

await Promise.all(
outFiles
.filter((fileName) => fileName.endsWith('.map'))
.map(async (sourceMapFile) => {
const sourceMap = await getFileContent(`dist/${sourceMapFile}`).then(
(rawContent) => JSON.parse(rawContent),
)

expect(sourceMap.sources[0]).toBe('../src/input.ts')
expect(sourceMap.sourcesContent[0]).toBe(sourceCode)

const outputFileName = sourceMapFile.replace('.map', '')
expect(sourceMap.file).toBe(outputFileName)
}),
)
})

0 comments on commit 6ca0cb0

Please sign in to comment.