@@ -104,28 +104,33 @@ export function prerenderPlugin({ prerenderScript, renderTarget, additionalPrere
104104 name : 'vite-prerender-plugin' ,
105105 apply : 'build' ,
106106 enforce : 'post' ,
107- configResolved ( config ) {
108- userEnabledSourceMaps = ! ! config . build . sourcemap ;
107+ // Vite is pretty inconsistent with how it resolves config options, both
108+ // hooks are needed to set their respective options. ¯\_(ツ)_/¯
109+ config ( config ) {
110+ userEnabledSourceMaps = ! ! config . build ?. sourcemap ;
111+
109112 // Enable sourcemaps for generating more actionable error messages
113+ config . build ??= { } ;
110114 config . build . sourcemap = true ;
115+ } ,
116+ configResolved ( config ) {
117+ // We're only going to alter the chunking behavior in the default cases, where the user and/or
118+ // other plugins haven't already configured this. It'd be impossible to avoid breakages otherwise.
119+ if (
120+ Array . isArray ( config . build . rollupOptions . output ) ||
121+ config . build . rollupOptions . output ?. manualChunks
122+ ) {
123+ return ;
124+ }
111125
112- viteConfig = config ;
126+ config . build . rollupOptions . output ??= { } ;
127+ config . build . rollupOptions . output . manualChunks = ( id ) => {
128+ if ( id . includes ( prerenderScript ) || id . includes ( preloadPolyfillId ) ) {
129+ return "index" ;
130+ }
131+ } ;
113132
114- // We're only going to alter the chunking behavior in the default cases, where the user and/or
115- // other plugins haven't already configured this. It'd be impossible to avoid breakages otherwise.
116- if (
117- Array . isArray ( config . build . rollupOptions . output ) ||
118- config . build . rollupOptions . output ?. manualChunks
119- ) {
120- return ;
121- }
122-
123- config . build . rollupOptions . output ??= { } ;
124- config . build . rollupOptions . output . manualChunks = ( id ) => {
125- if ( id . includes ( prerenderScript ) || id . includes ( preloadPolyfillId ) ) {
126- return "index" ;
127- }
128- } ;
133+ viteConfig = config ;
129134 } ,
130135 async options ( opts ) {
131136 if ( ! opts . input ) return ;
@@ -226,13 +231,18 @@ export function prerenderPlugin({ prerenderScript, renderTarget, additionalPrere
226231 ) ;
227232 let htmlDoc = htmlParse ( tpl ) ;
228233
234+ // Workaround for PNPM mangling file paths with their symlinks
235+ const tmpDirRelative = path . join (
236+ 'node_modules' ,
237+ 'vite-prerender-plugin' ,
238+ 'headless-prerender' ,
239+ ) ;
240+
229241 // Create a tmp dir to allow importing & consuming the built modules,
230242 // before Rollup writes them to the disk
231243 const tmpDir = path . join (
232244 viteConfig . root ,
233- 'node_modules' ,
234- 'vite-prerender-plugin' ,
235- 'headless-prerender' ,
245+ tmpDirRelative ,
236246 ) ;
237247 try {
238248 await fs . rm ( tmpDir , { recursive : true } ) ;
@@ -249,26 +259,6 @@ export function prerenderPlugin({ prerenderScript, renderTarget, additionalPrere
249259 /** @type {OutputChunk | undefined } */
250260 let prerenderEntry ;
251261 for ( const output of Object . keys ( bundle ) ) {
252- // Clean up source maps if the user didn't enable them themselves
253- if ( ! userEnabledSourceMaps ) {
254- if ( output . endsWith ( '.map' ) ) {
255- delete bundle [ output ] ;
256- continue ;
257- }
258- if ( output . endsWith ( '.js' ) ) {
259- if ( bundle [ output ] . type == 'chunk' ) {
260- bundle [ output ] . code = bundle [ output ] . code . replace (
261- / \n \/ \/ # \s s o u r c e M a p p i n g U R L = .* / ,
262- '' ,
263- ) ;
264- } else {
265- // Workers and similar
266- bundle [ output ] . source = /** @type {string } */ (
267- bundle [ output ] . source
268- ) . replace ( / \n \/ \/ # \s s o u r c e M a p p i n g U R L = .* / , '' ) ;
269- }
270- }
271- }
272262 if ( ! output . endsWith ( '.js' ) || bundle [ output ] . type !== 'chunk' ) continue ;
273263
274264 await fs . writeFile (
@@ -310,7 +300,7 @@ export function prerenderPlugin({ prerenderScript, renderTarget, additionalPrere
310300 }
311301 ` . replace ( / ^ \t { 5 } / gm, '' ) ;
312302
313- const stack = StackTraceParse ( e ) . find ( ( s ) => s . getFileName ( ) . includes ( tmpDir ) ) ;
303+ const stack = StackTraceParse ( e ) . find ( ( s ) => s . getFileName ( ) . includes ( tmpDirRelative ) ) ;
314304
315305 const sourceMapContent = prerenderEntry . map ;
316306 if ( stack && sourceMapContent ) {
@@ -443,13 +433,31 @@ export function prerenderPlugin({ prerenderScript, renderTarget, additionalPrere
443433
444434 // Add generated HTML to compilation:
445435 route . url == '/'
446- ? /** @type {OutputAsset } */ ( ( bundle [ 'index.html' ] ) . source =
436+ ? ( /** @type {OutputAsset } */ ( bundle [ 'index.html' ] ) . source =
447437 htmlDoc . toString ( ) )
448438 : this . emitFile ( {
449439 type : 'asset' ,
450440 fileName : assetName ,
451441 source : htmlDoc . toString ( ) ,
452442 } ) ;
443+
444+ // Clean up source maps if the user didn't enable them themselves
445+ if ( ! userEnabledSourceMaps ) {
446+ for ( const output of Object . keys ( bundle ) ) {
447+ if ( output . endsWith ( '.map' ) ) {
448+ delete bundle [ output ] ;
449+ continue ;
450+ }
451+
452+ if ( output . endsWith ( '.js' ) ) {
453+ const codeOrSource = bundle [ output ] . type == 'chunk' ? 'code' : 'source' ;
454+ bundle [ output ] [ codeOrSource ] = bundle [ output ] [ codeOrSource ] . replace (
455+ / \n \/ \/ # \s s o u r c e M a p p i n g U R L = .* / ,
456+ '' ,
457+ ) ;
458+ }
459+ }
460+ }
453461 }
454462 } ,
455463 } ;
0 commit comments