From 6ca0cb0aee177a4c920f4e86d06544d0c51d9de3 Mon Sep 17 00:00:00 2001 From: xiaohj <51224469+xia0hj@users.noreply.github.com> Date: Wed, 17 Jul 2024 23:16:14 +0800 Subject: [PATCH] fix: correct sourcemap with treeshake (#1069) --- src/plugin.ts | 5 ++--- src/plugins/tree-shaking.ts | 7 ++++--- test/index.test.ts | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 6 deletions(-) diff --git a/src/plugin.ts b/src/plugin.ts index 25c9384c8..39cb46eb3 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -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() diff --git a/src/plugins/tree-shaking.ts b/src/plugins/tree-shaking.ts index 68368ab47..ad042e946 100644 --- a/src/plugins/tree-shaking.ts +++ b/src/plugins/tree-shaking.ts @@ -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 @@ -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 } }, }, ], @@ -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, diff --git a/test/index.test.ts b/test/index.test.ts index 532b9296a..4cc39df51 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -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) + }), + ) +})