From d1911d613424e11335694a8f84400a581828a7df Mon Sep 17 00:00:00 2001 From: Adam Hines Date: Mon, 12 Dec 2022 09:29:56 -0700 Subject: [PATCH 1/2] 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. --- packages/vite/src/node/plugins/asset.ts | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/packages/vite/src/node/plugins/asset.ts b/packages/vite/src/node/plugins/asset.ts index 954d50e936862d..d68e6a05b50595 100644 --- a/packages/vite/src/node/plugins/asset.ts +++ b/packages/vite/src/node/plugins/asset.ts @@ -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>() @@ -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] } From 43495929e6caf00d85657b9aadb94ebb71b1ed8d Mon Sep 17 00:00:00 2001 From: Adam Hines Date: Fri, 30 Dec 2022 09:44:14 -0700 Subject: [PATCH 2/2] chore: applying review feedback --- packages/vite/src/node/plugins/asset.ts | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/packages/vite/src/node/plugins/asset.ts b/packages/vite/src/node/plugins/asset.ts index d68e6a05b50595..563f238702c9da 100644 --- a/packages/vite/src/node/plugins/asset.ts +++ b/packages/vite/src/node/plugins/asset.ts @@ -188,11 +188,9 @@ export function assetPlugin(config: ResolvedConfig): Plugin { generateBundle(_, bundle) { // do not emit assets for SSR build if (config.command === 'build' && config.build.ssr) { - const assetFiles = Object.keys(bundle).filter( - (file) => bundle[file].type === 'asset', - ) - for (const file of assetFiles) { + for (const file in bundle) { if ( + bundle[file].type === 'asset' && !file.endsWith('ssr-manifest.json') && !jsSourceMapRE.test(file) ) {