1
1
import type { QwikVitePlugin } from '@qwik.dev/core/optimizer' ;
2
2
import type { StaticGenerateOptions , SsgRenderOptions } from 'packages/qwik-router/src/ssg' ;
3
3
import type { QwikRouterPlugin } from '@qwik.dev/router/vite' ;
4
- import fs from 'node:fs' ;
5
4
import { basename , dirname , join , resolve } from 'node:path' ;
6
5
import type { Plugin , UserConfig } from 'vite' ;
7
6
import type { BuildRoute } from '../../../buildtime/types' ;
8
7
import { postBuild } from './post-build' ;
9
8
10
- /** @public */
9
+ /**
10
+ * Implements the SSG after the build is complete. Also provides a `generate(...)` callback that is
11
+ * called after the SSG is complete, which allows for custom post-processing of the complete build
12
+ * results.
13
+ *
14
+ * @public
15
+ */
11
16
export function viteAdapter ( opts : ViteAdapterPluginOptions ) {
12
17
let qwikRouterPlugin : QwikRouterPlugin | null = null ;
13
18
let qwikVitePlugin : QwikVitePlugin | null = null ;
14
19
let serverOutDir : string | null = null ;
15
20
let renderModulePath : string | null = null ;
16
21
let qwikRouterConfigModulePath : string | null = null ;
17
22
let isSsrBuild = false ;
18
- let format = 'esm' ;
19
23
const outputEntries : string [ ] = [ ] ;
20
24
21
25
const plugin : Plugin < never > = {
22
- name : `vite-plugin-qwik-router-${ opts . name } ` ,
26
+ name : `vite-plugin-qwik-router-ssg- ${ opts . name } ` ,
23
27
enforce : 'post' ,
24
28
apply : 'build' ,
25
29
@@ -63,11 +67,6 @@ export function viteAdapter(opts: ViteAdapterPluginOptions) {
63
67
`"build.rollupOptions.input" must be set in order to use the "${ opts . name } " adapter.`
64
68
) ;
65
69
}
66
-
67
- // @ts -ignore `format` removed in Vite 5
68
- if ( config . ssr ?. format === 'cjs' ) {
69
- format = 'cjs' ;
70
- }
71
70
}
72
71
} ,
73
72
buildStart ( ) {
@@ -108,13 +107,7 @@ export function viteAdapter(opts: ViteAdapterPluginOptions) {
108
107
closeBundle : {
109
108
sequential : true ,
110
109
async handler ( ) {
111
- if (
112
- isSsrBuild &&
113
- opts . ssg !== null &&
114
- serverOutDir &&
115
- qwikRouterPlugin ?. api &&
116
- qwikVitePlugin ?. api
117
- ) {
110
+ if ( isSsrBuild && serverOutDir && qwikRouterPlugin ?. api && qwikVitePlugin ?. api ) {
118
111
const staticPaths : string [ ] = opts . staticPaths || [ ] ;
119
112
const routes = qwikRouterPlugin . api . getRoutes ( ) ;
120
113
const basePathname = qwikRouterPlugin . api . getBasePathname ( ) ;
@@ -124,6 +117,7 @@ export function viteAdapter(opts: ViteAdapterPluginOptions) {
124
117
125
118
const rootDir = qwikVitePlugin . api . getRootDir ( ) ?? undefined ;
126
119
if (
120
+ opts . ssg !== null &&
127
121
renderModulePath &&
128
122
qwikRouterConfigModulePath &&
129
123
clientOutDir &&
@@ -171,42 +165,36 @@ export function viteAdapter(opts: ViteAdapterPluginOptions) {
171
165
}
172
166
173
167
staticPaths . push ( ...staticGenerateResult . staticPaths ) ;
168
+ }
174
169
175
- const { staticPathsCode , notFoundPathsCode } = await postBuild (
176
- clientPublicOutDir ,
177
- assetsDir ? join ( basePathname , assetsDir ) : basePathname ,
178
- staticPaths ,
179
- format ,
180
- ! ! opts . cleanStaticGenerated
181
- ) ;
170
+ await postBuild (
171
+ clientPublicOutDir ,
172
+ serverOutDir ,
173
+ assetsDir ? join ( basePathname , assetsDir ) : basePathname ,
174
+ staticPaths ,
175
+ ! ! opts . cleanStaticGenerated
176
+ ) ;
182
177
183
- await Promise . all ( [
184
- fs . promises . writeFile ( join ( serverOutDir , RESOLVED_STATIC_PATHS_ID ) , staticPathsCode ) ,
185
- fs . promises . writeFile (
186
- join ( serverOutDir , RESOLVED_NOT_FOUND_PATHS_ID ) ,
187
- notFoundPathsCode
188
- ) ,
189
- ] ) ;
190
- if ( typeof opts . generate === 'function' ) {
191
- await opts . generate ( {
192
- outputEntries,
193
- serverOutDir,
194
- clientOutDir,
195
- clientPublicOutDir,
196
- basePathname,
197
- routes,
198
- assetsDir,
199
- warn : ( message ) => this . warn ( message ) ,
200
- error : ( message ) => this . error ( message ) ,
201
- } ) ;
202
- }
203
- this . warn (
204
- `\n==============================================` +
205
- `\nNote: Make sure that you are serving the built files with proper cache headers.` +
206
- `\nSee https://qwik.dev/docs/deployments/#cache-headers for more information.` +
207
- `\n==============================================`
208
- ) ;
178
+ if ( typeof opts . generate === 'function' ) {
179
+ await opts . generate ( {
180
+ outputEntries,
181
+ serverOutDir,
182
+ clientOutDir,
183
+ clientPublicOutDir,
184
+ basePathname,
185
+ routes,
186
+ assetsDir,
187
+ warn : ( message ) => this . warn ( message ) ,
188
+ error : ( message ) => this . error ( message ) ,
189
+ } ) ;
209
190
}
191
+
192
+ this . warn (
193
+ `\n==============================================` +
194
+ `\nNote: Make sure that you are serving the built files with proper cache headers.` +
195
+ `\nSee https://qwik.dev/docs/deployments/#cache-headers for more information.` +
196
+ `\n==============================================`
197
+ ) ;
210
198
}
211
199
} ,
212
200
} ,
@@ -288,15 +276,3 @@ export interface AdapterSSGOptions extends Omit<SsgRenderOptions, 'outDir' | 'or
288
276
*/
289
277
origin ?: string ;
290
278
}
291
-
292
- /** @public */
293
- export const STATIC_PATHS_ID = '@qwik-router-static-paths' ;
294
-
295
- /** @public */
296
- export const RESOLVED_STATIC_PATHS_ID = `${ STATIC_PATHS_ID } .js` ;
297
-
298
- /** @public */
299
- export const NOT_FOUND_PATHS_ID = '@qwik-router-not-found-paths' ;
300
-
301
- /** @public */
302
- export const RESOLVED_NOT_FOUND_PATHS_ID = `${ NOT_FOUND_PATHS_ID } .js` ;
0 commit comments