diff --git a/packages/vite/src/node/plugins/importAnalysisBuild.ts b/packages/vite/src/node/plugins/importAnalysisBuild.ts index d04229dc73475f..0f49bb44d10b71 100644 --- a/packages/vite/src/node/plugins/importAnalysisBuild.ts +++ b/packages/vite/src/node/plugins/importAnalysisBuild.ts @@ -5,6 +5,7 @@ import { init, parse as parseImports } from 'es-module-lexer' import type { OutputChunk, SourceMap } from 'rollup' import colors from 'picocolors' import type { RawSourceMap } from '@ampproject/remapping' +import convertSourceMap from 'convert-source-map' import { bareImportRE, cleanUrl, @@ -608,6 +609,19 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin { ) as SourceMap map.toUrl = () => genSourceMapUrl(map) chunk.map = map + + if (config.build.sourcemap === 'inline') { + chunk.code = chunk.code.replace( + convertSourceMap.mapFileCommentRegex, + '', + ) + chunk.code += `\n//# sourceMappingURL=${genSourceMapUrl(map)}` + } else if (config.build.sourcemap) { + const mapAsset = bundle[chunk.fileName + '.map'] + if (mapAsset && mapAsset.type === 'asset') { + mapAsset.source = map.toString() + } + } } } } diff --git a/playground/js-sourcemap/__tests__/js-sourcemap.spec.ts b/playground/js-sourcemap/__tests__/js-sourcemap.spec.ts index 692de4c8ed2c6e..e04a7c36126d2f 100644 --- a/playground/js-sourcemap/__tests__/js-sourcemap.spec.ts +++ b/playground/js-sourcemap/__tests__/js-sourcemap.spec.ts @@ -1,7 +1,8 @@ import { URL } from 'node:url' -import { expect, test } from 'vitest' +import { describe, expect, test } from 'vitest' import { extractSourcemap, + findAssetFile, formatSourcemapForSnapshot, isBuild, page, @@ -40,14 +41,31 @@ if (!isBuild) { expect(log).not.toMatch(/Sourcemap for .+ points to missing source files/) }) }) -} else { - test('this file only includes test for serve', () => { - expect(true).toBe(true) - }) } -test.runIf(isBuild)('should not output sourcemap warning (#4939)', () => { - serverLogs.forEach((log) => { - expect(log).not.toMatch('Sourcemap is likely to be incorrect') +describe.runIf(isBuild)('build tests', () => { + test('should not output sourcemap warning (#4939)', () => { + serverLogs.forEach((log) => { + expect(log).not.toMatch('Sourcemap is likely to be incorrect') + }) + }) + + test('sourcemap is correct when preload information is injected', async () => { + const map = findAssetFile(/after-preload-dynamic.*\.js\.map/) + expect(formatSourcemapForSnapshot(JSON.parse(map))).toMatchInlineSnapshot(` + { + "mappings": "stBAAAA,aAAO,2BAAuB,EAAC,sEAE/B,QAAQ,IAAI,uBAAuB", + "sources": [ + "../../after-preload-dynamic.js", + ], + "sourcesContent": [ + "import('./dynamic/dynamic-foo') + + console.log('after preload dynamic') + ", + ], + "version": 3, + } + `) }) }) diff --git a/playground/js-sourcemap/after-preload-dynamic.js b/playground/js-sourcemap/after-preload-dynamic.js new file mode 100644 index 00000000000000..c11701f2320efc --- /dev/null +++ b/playground/js-sourcemap/after-preload-dynamic.js @@ -0,0 +1,3 @@ +import('./dynamic/dynamic-foo') + +console.log('after preload dynamic') diff --git a/playground/js-sourcemap/dynamic/dynamic-foo.css b/playground/js-sourcemap/dynamic/dynamic-foo.css new file mode 100644 index 00000000000000..0f85e3797cbb65 --- /dev/null +++ b/playground/js-sourcemap/dynamic/dynamic-foo.css @@ -0,0 +1,3 @@ +.dynamic-foo { + color: red; +} diff --git a/playground/js-sourcemap/dynamic/dynamic-foo.js b/playground/js-sourcemap/dynamic/dynamic-foo.js new file mode 100644 index 00000000000000..32bcdeeb03683f --- /dev/null +++ b/playground/js-sourcemap/dynamic/dynamic-foo.js @@ -0,0 +1,3 @@ +import './dynamic-foo.css' + +console.log('dynamic/dynamic-foo') diff --git a/playground/js-sourcemap/index.html b/playground/js-sourcemap/index.html index 025b161011a3fa..bda409a5cc9693 100644 --- a/playground/js-sourcemap/index.html +++ b/playground/js-sourcemap/index.html @@ -1,6 +1,8 @@

JS Sourcemap

+
dynamic
+ diff --git a/playground/js-sourcemap/vite.config.js b/playground/js-sourcemap/vite.config.js index 21a9d6a6cad1ea..b56e74e40f3633 100644 --- a/playground/js-sourcemap/vite.config.js +++ b/playground/js-sourcemap/vite.config.js @@ -3,5 +3,14 @@ import { defineConfig } from 'vite' export default defineConfig({ build: { sourcemap: true, + rollupOptions: { + output: { + manualChunks(name) { + if (name.includes('after-preload-dynamic')) { + return 'after-preload-dynamic' + } + }, + }, + }, }, })