Skip to content

Commit

Permalink
refactor: Clean sourcemaps in newer Vite versions
Browse files Browse the repository at this point in the history
  • Loading branch information
rschristian committed Feb 22, 2024
1 parent 0db4c5b commit d1a2486
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/prerender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 ||= [];
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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));
});
}
},
};
}

0 comments on commit d1a2486

Please sign in to comment.