From 580bcb8223dfb13aec08fdeaa2223473e4d21956 Mon Sep 17 00:00:00 2001 From: xia0hj <719887163@qq.com> Date: Thu, 28 Dec 2023 23:12:39 +0800 Subject: [PATCH 1/5] fix: correct use of applySourceMap --- src/plugin.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 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() From 0b5577122fe5da6351fcccb0a5b6eb9b88a6a51c Mon Sep 17 00:00:00 2001 From: xia0hj <719887163@qq.com> Date: Thu, 28 Dec 2023 23:14:16 +0800 Subject: [PATCH 2/5] fix: fix treeshake sourcemap --- src/plugins/tree-shaking.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/plugins/tree-shaking.ts b/src/plugins/tree-shaking.ts index d3e7cf76e..85f5304cf 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 @@ -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, From 15dfc0bb4c08aea044f8b814505fafcb1a1f7c84 Mon Sep 17 00:00:00 2001 From: xia0hj <719887163@qq.com> Date: Thu, 28 Dec 2023 23:14:44 +0800 Subject: [PATCH 3/5] test: add test case --- test/index.test.ts | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/test/index.test.ts b/test/index.test.ts index 532b9296a..bd94194ba 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -856,3 +856,36 @@ 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 { outFiles, getFileContent } = await run( + getTestName(), + { + 'src/input.ts': 'export function getValue(val: any){ return val; }', + }, + { + 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( + 'export function getValue(val: any){ return val; }', + ) + + const outputFileName = sourceMapFile.replace('.map', '') + expect(sourceMap.file).toBe(outputFileName) + }), + ) +}) From 3b6dd4565bc085829a430d25149a74bc45a59a05 Mon Sep 17 00:00:00 2001 From: xia0hj <719887163@qq.com> Date: Wed, 17 Jul 2024 22:17:06 +0800 Subject: [PATCH 4/5] fix: fix treeshake sourcemap --- src/plugins/tree-shaking.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/plugins/tree-shaking.ts b/src/plugins/tree-shaking.ts index e4b078f49..ad042e946 100644 --- a/src/plugins/tree-shaking.ts +++ b/src/plugins/tree-shaking.ts @@ -32,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 } }, }, ], From e1548dc4df51e8d8cb0c1f933aff2ac0c3d21e67 Mon Sep 17 00:00:00 2001 From: xia0hj <719887163@qq.com> Date: Wed, 17 Jul 2024 22:26:14 +0800 Subject: [PATCH 5/5] test: add test case --- test/index.test.ts | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/test/index.test.ts b/test/index.test.ts index bd94194ba..4cc39df51 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -858,10 +858,11 @@ test('should load postcss esm config', async () => { }) test('generate sourcemap with --treeshake', async () => { + const sourceCode = 'export function getValue(val: any){ return val; }' const { outFiles, getFileContent } = await run( getTestName(), { - 'src/input.ts': 'export function getValue(val: any){ return val; }', + 'src/input.ts': sourceCode, }, { entry: ['src/input.ts'], @@ -880,9 +881,7 @@ test('generate sourcemap with --treeshake', async () => { ) expect(sourceMap.sources[0]).toBe('../src/input.ts') - expect(sourceMap.sourcesContent[0]).toBe( - 'export function getValue(val: any){ return val; }', - ) + expect(sourceMap.sourcesContent[0]).toBe(sourceCode) const outputFileName = sourceMapFile.replace('.map', '') expect(sourceMap.file).toBe(outputFileName)