@@ -78,14 +78,18 @@ export function PrerenderPlugin({
78
78
renderTarget,
79
79
additionalPrerenderRoutes,
80
80
} : PrerenderPluginOptions = { } ) : Plugin {
81
- const preloadHelperId = "vite/preload-helper" ;
82
- const preloadPolyfillId = "vite/modulepreload-polyfill" ;
83
81
let viteConfig = { } as ResolvedConfig ;
84
82
let userEnabledSourceMaps : boolean | undefined ;
85
83
86
84
renderTarget ||= "body" ;
87
85
additionalPrerenderRoutes ||= [ ] ;
88
86
87
+ const preloadHelperId = "vite/preload-helper" ;
88
+ const preloadPolyfillId = "vite/modulepreload-polyfill" ;
89
+ // PNPM, Yalc, and anything else utilizing symlinks mangle the file
90
+ // path a bit so we need a minimal, fairly unique ID to check against
91
+ const tmpDirId = "headless-prerender" ;
92
+
89
93
/**
90
94
* From the non-external scripts in entry HTML document, find the one (if any)
91
95
* that provides a `prerender` export
@@ -123,13 +127,16 @@ export function PrerenderPlugin({
123
127
name : "preact:prerender" ,
124
128
apply : "build" ,
125
129
enforce : "post" ,
126
- configResolved ( config ) {
127
- userEnabledSourceMaps = ! ! config . build . sourcemap ;
130
+ // As of Vite 6, `sourcemap` can *only* be set in `config` and
131
+ // `manualChunks` can *only* be set in `configResolved`.
132
+ config ( config ) {
133
+ userEnabledSourceMaps = ! ! config . build ?. sourcemap ;
134
+
128
135
// Enable sourcemaps for generating more actionable error messages
136
+ config . build ??= { } ;
129
137
config . build . sourcemap = true ;
130
-
131
- viteConfig = config ;
132
-
138
+ } ,
139
+ configResolved ( config ) {
133
140
// With this plugin adding an additional input, Rollup/Vite tries to be smart
134
141
// and extract our prerender script (which is often their main bundle) to a separate
135
142
// chunk that the entry & prerender chunks can depend on. Unfortunately, this means the
@@ -157,6 +164,8 @@ export function PrerenderPlugin({
157
164
return "index" ;
158
165
}
159
166
} ;
167
+
168
+ viteConfig = config ;
160
169
} ,
161
170
async options ( opts ) {
162
171
if ( ! opts . input ) return ;
@@ -263,7 +272,7 @@ export function PrerenderPlugin({
263
272
viteConfig . root ,
264
273
"node_modules" ,
265
274
"@preact/preset-vite" ,
266
- "headless-prerender" ,
275
+ tmpDirId ,
267
276
) ;
268
277
try {
269
278
await fs . rm ( tmpDir , { recursive : true } ) ;
@@ -279,18 +288,6 @@ export function PrerenderPlugin({
279
288
280
289
let prerenderEntry : OutputChunk | undefined ;
281
290
for ( const output of Object . keys ( bundle ) ) {
282
- // Clean up source maps if the user didn't enable them themselves
283
- if ( ! userEnabledSourceMaps ) {
284
- if ( output . endsWith ( ".map" ) ) {
285
- delete bundle [ output ] ;
286
- continue ;
287
- }
288
- if ( output . endsWith ( ".js" ) && bundle [ output ] . type == "chunk" ) {
289
- ( bundle [ output ] as OutputChunk ) . code = ( bundle [
290
- output
291
- ] as OutputChunk ) . code . replace ( / \n \/ \/ # \s s o u r c e M a p p i n g U R L = .* / , "" ) ;
292
- }
293
- }
294
291
if ( ! output . endsWith ( ".js" ) || bundle [ output ] . type !== "chunk" )
295
292
continue ;
296
293
@@ -316,10 +313,6 @@ export function PrerenderPlugin({
316
313
) ;
317
314
prerender = m . prerender ;
318
315
} catch ( e ) {
319
- const stack = await import ( "stack-trace" ) . then ( ( { parse } ) =>
320
- parse ( e as Error ) . find ( s => s . getFileName ( ) . includes ( tmpDir ) ) ,
321
- ) ;
322
-
323
316
const isReferenceError = e instanceof ReferenceError ;
324
317
let message = `\n
325
318
${ e }
@@ -335,6 +328,10 @@ export function PrerenderPlugin({
335
328
}
336
329
` . replace ( / ^ \t { 5 } / gm, "" ) ;
337
330
331
+ const stack = await import ( "stack-trace" ) . then ( ( { parse } ) =>
332
+ parse ( e as Error ) . find ( s => s . getFileName ( ) . includes ( tmpDirId ) ) ,
333
+ ) ;
334
+
338
335
const sourceMapContent = prerenderEntry . map ;
339
336
if ( stack && sourceMapContent ) {
340
337
await SourceMapConsumer . with (
@@ -407,7 +404,10 @@ export function PrerenderPlugin({
407
404
}
408
405
409
406
const result = await prerender ( { ssr : true , url : route . url , route } ) ;
410
- if ( result == null ) continue ;
407
+ if ( result == null ) {
408
+ this . warn ( `No result returned for route "${ route . url } "` ) ;
409
+ continue ;
410
+ }
411
411
412
412
// Reset HTML doc & head data
413
413
htmlDoc = htmlParse ( tpl ) ;
@@ -485,6 +485,24 @@ export function PrerenderPlugin({
485
485
fileName : assetName ,
486
486
source : htmlDoc . toString ( ) ,
487
487
} ) ;
488
+
489
+ // Clean up source maps if the user didn't enable them themselves
490
+ if ( ! userEnabledSourceMaps ) {
491
+ for ( const output of Object . keys ( bundle ) ) {
492
+ if ( output . endsWith ( ".map" ) ) {
493
+ delete bundle [ output ] ;
494
+ continue ;
495
+ }
496
+ if ( output . endsWith ( ".js" ) ) {
497
+ const codeOrSource =
498
+ bundle [ output ] . type == "chunk" ? "code" : "source" ;
499
+ // @ts -ignore
500
+ bundle [ output ] [ codeOrSource ] = bundle [ output ] [
501
+ codeOrSource
502
+ ] . replace ( / \n \/ \/ # \s s o u r c e M a p p i n g U R L = .* / , "" ) ;
503
+ }
504
+ }
505
+ }
488
506
}
489
507
} ,
490
508
} ;
0 commit comments