From d1a2486dde1863c4e48dbbb9e2612c0355eb8425 Mon Sep 17 00:00:00 2001 From: Ryan Christian Date: Wed, 21 Feb 2024 23:42:01 -0600 Subject: [PATCH 1/2] refactor: Clean sourcemaps in newer Vite versions --- src/prerender.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/prerender.ts b/src/prerender.ts index 73a7922..e2d4d77 100644 --- a/src/prerender.ts +++ b/src/prerender.ts @@ -76,6 +76,7 @@ export function PrerenderPlugin({ }: PrerenderPluginOptions = {}): Plugin { const preloadHelperId = "vite/preload-helper"; let viteConfig = {} as ResolvedConfig; + let userEnabledSourceMaps: boolean | undefined; renderTarget ||= "body"; additionalPrerenderRoutes ||= []; @@ -118,6 +119,7 @@ export function PrerenderPlugin({ apply: "build", enforce: "post", configResolved(config) { + userEnabledSourceMaps = !!config.build.sourcemap; // Enable sourcemaps for generating more actionable error messages config.build.sourcemap = true; @@ -405,5 +407,14 @@ export function PrerenderPlugin({ }); } }, + async writeBundle(_opts, bundle) { + if (!userEnabledSourceMaps) { + Object.keys(bundle) + .filter(f => /\.map$/.test(f)) + .forEach(async f => { + fs.rm(path.join(viteConfig.root, viteConfig.build.outDir, f)); + }); + } + }, }; } From 775e26dc5f3e38705b3dd410d970037529e5bf34 Mon Sep 17 00:00:00 2001 From: Ryan Christian Date: Wed, 21 Feb 2024 23:53:35 -0600 Subject: [PATCH 2/2] refactor: Skip second loop, delete from bundle earlier --- src/prerender.ts | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/src/prerender.ts b/src/prerender.ts index e2d4d77..d8d9a4e 100644 --- a/src/prerender.ts +++ b/src/prerender.ts @@ -221,6 +221,11 @@ export function PrerenderPlugin({ let prerenderEntry: OutputChunk | undefined; for (const output of Object.keys(bundle)) { + // Clean up source maps if the user didn't enable them themselves + if (/\.map$/.test(output) && !userEnabledSourceMaps) { + delete bundle[output]; + continue; + } if (!/\.js$/.test(output) || bundle[output].type !== "chunk") continue; await fs.writeFile( @@ -407,14 +412,5 @@ export function PrerenderPlugin({ }); } }, - async writeBundle(_opts, bundle) { - if (!userEnabledSourceMaps) { - Object.keys(bundle) - .filter(f => /\.map$/.test(f)) - .forEach(async f => { - fs.rm(path.join(viteConfig.root, viteConfig.build.outDir, f)); - }); - } - }, }; }