Skip to content

Commit d1911d6

Browse files
author
Adam Hines
committed
fix(ssr): emit js sourcemaps for ssr builds
Rollup 3 switched the way sourcemaps were handled to be more consistent between the in-memory representation and what ultimately gets emitted to disk. Part of this change was moving the sourcemaps themselves to be assets within the bundle. At the same time, vite's `generateBundle` hook for the `assetPlugin` is configured to delete assets within the bundle (with the exception of "ssr-manifest.json"). Since `.js.map` files are now considered assets within the bundle at this stage, vite is removing the sourcemaps from the bundle when `build.ssr = true` and they never wind up on disk. This change adds an additional check to see if the file ends with `.js.map`, `.cjs.map`, or `.mjs.map` and it will leave these files in the bundle so that they are emitted to the outDir when the bundle is written.
1 parent cfedf9c commit d1911d6

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

packages/vite/src/node/plugins/asset.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export const assetUrlRE = /__VITE_ASSET__([a-z\d]+)__(?:\$_(.*?)__)?/g
2323

2424
const rawRE = /(?:\?|&)raw(?:&|$)/
2525
const urlRE = /(\?|&)url(?:&|$)/
26+
const jsSourceMapRE = /\.[cm]?js\.map$/
2627

2728
const assetCache = new WeakMap<ResolvedConfig, Map<string, string>>()
2829

@@ -187,10 +188,13 @@ export function assetPlugin(config: ResolvedConfig): Plugin {
187188
generateBundle(_, bundle) {
188189
// do not emit assets for SSR build
189190
if (config.command === 'build' && config.build.ssr) {
190-
for (const file in bundle) {
191+
const assetFiles = Object.keys(bundle).filter(
192+
(file) => bundle[file].type === 'asset',
193+
)
194+
for (const file of assetFiles) {
191195
if (
192-
bundle[file].type === 'asset' &&
193-
!file.includes('ssr-manifest.json')
196+
!file.endsWith('ssr-manifest.json') &&
197+
!jsSourceMapRE.test(file)
194198
) {
195199
delete bundle[file]
196200
}

0 commit comments

Comments
 (0)