Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: correct sourcemap with treeshake #1069

Merged
merged 6 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}),
)
})
Loading