Skip to content

Commit

Permalink
fix(ssr): emit js sourcemaps for ssr builds
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Adam Hines committed Dec 12, 2022
1 parent cfedf9c commit d1911d6
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions packages/vite/src/node/plugins/asset.ts
Expand Up @@ -23,6 +23,7 @@ export const assetUrlRE = /__VITE_ASSET__([a-z\d]+)__(?:\$_(.*?)__)?/g

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

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

Expand Down Expand Up @@ -187,10 +188,13 @@ export function assetPlugin(config: ResolvedConfig): Plugin {
generateBundle(_, bundle) {
// do not emit assets for SSR build
if (config.command === 'build' && config.build.ssr) {
for (const file in bundle) {
const assetFiles = Object.keys(bundle).filter(
(file) => bundle[file].type === 'asset',
)
for (const file of assetFiles) {
if (
bundle[file].type === 'asset' &&
!file.includes('ssr-manifest.json')
!file.endsWith('ssr-manifest.json') &&
!jsSourceMapRE.test(file)
) {
delete bundle[file]
}
Expand Down

0 comments on commit d1911d6

Please sign in to comment.