@@ -22,8 +22,7 @@ import { versions } from '../versions';
2222export function qwikVite ( qwikViteOpts : QwikVitePluginOptions = { } ) : any {
2323 let isClientDevOnly = false ;
2424 let clientDevInput : undefined | string = undefined ;
25- let serverInput : undefined | string = undefined ;
26- let ssrInput : undefined | string = undefined ;
25+ let tmpClientManifestPath : undefined | string = undefined ;
2726
2827 const qwikPlugin = createPlugin ( qwikViteOpts . optimizerOptions ) ;
2928
@@ -39,6 +38,9 @@ export function qwikVite(qwikViteOpts: QwikVitePluginOptions = {}): any {
3938 async config ( viteConfig , viteEnv ) {
4039 await qwikPlugin . init ( ) ;
4140
41+ const sys = qwikPlugin . getSys ( ) ;
42+ const path = qwikPlugin . getPath ( ) ;
43+
4244 qwikPlugin . log ( `vite config(), command: ${ viteEnv . command } , env.mode: ${ viteEnv . mode } ` ) ;
4345
4446 isClientDevOnly = viteEnv . command === 'serve' && viteEnv . mode !== 'ssr' ;
@@ -91,19 +93,29 @@ export function qwikVite(qwikViteOpts: QwikVitePluginOptions = {}): any {
9193 pluginOpts . manifestOutput = qwikViteOpts . client ?. manifestOutput ;
9294 }
9395
94- const opts = qwikPlugin . normalizeOptions ( pluginOpts ) ;
95- const path = qwikPlugin . getPath ( ) ;
96-
97- if ( opts . target === 'ssr' ) {
98- if ( typeof viteConfig . build ?. ssr === 'string' ) {
99- // entry.express.tsx user config
100- serverInput = opts . input [ 0 ] ;
101- } else {
102- // entry.ssr.tsx render() input
103- ssrInput = opts . input [ 0 ] ;
96+ if ( sys . env === 'node' ) {
97+ // In a NodeJs environment, create a path to a q-manifest.json file within the
98+ // OS tmp directory. This path should always be the same for both client and ssr.
99+ // Client build will write to this path, and SSR will read from it. For this reason,
100+ // the Client build should always start and finish before the SSR build.
101+ const nodeOs : typeof import ( 'os' ) = await sys . dynamicImport ( 'os' ) ;
102+ tmpClientManifestPath = path . join ( nodeOs . tmpdir ( ) , `vite-plugin-qwik-q-manifest.json` ) ;
103+
104+ if ( target === 'ssr' && ! pluginOpts . manifestInput ) {
105+ // This is a SSR build so we should load the client build's manifest
106+ // so it can be used as the manifestInput of the SSR build
107+ const fs : typeof import ( 'fs' ) = await sys . dynamicImport ( 'fs' ) ;
108+ try {
109+ const clientManifestStr = await fs . promises . readFile ( tmpClientManifestPath , 'utf-8' ) ;
110+ pluginOpts . manifestInput = JSON . parse ( clientManifestStr ) ;
111+ } catch ( e ) {
112+ /**/
113+ }
104114 }
105115 }
106116
117+ const opts = qwikPlugin . normalizeOptions ( pluginOpts ) ;
118+
107119 if ( typeof qwikViteOpts . client ?. devInput === 'string' ) {
108120 clientDevInput = path . resolve ( opts . rootDir , qwikViteOpts . client . devInput ) ;
109121 } else {
@@ -214,7 +226,6 @@ export function qwikVite(qwikViteOpts: QwikVitePluginOptions = {}): any {
214226
215227 async generateBundle ( _ , rollupBundle ) {
216228 const opts = qwikPlugin . getOptions ( ) ;
217- const path = qwikPlugin . getPath ( ) ;
218229
219230 const outputAnalyzer = qwikPlugin . createOutputAnalyzer ( ) ;
220231
@@ -241,15 +252,23 @@ export function qwikVite(qwikViteOpts: QwikVitePluginOptions = {}): any {
241252
242253 if ( opts . target === 'client' ) {
243254 // client build
255+ const clientManifestStr = JSON . stringify ( manifest , null , 2 ) ;
244256 this . emitFile ( {
245257 type : 'asset' ,
246258 fileName : Q_MANIFEST_FILENAME ,
247- source : JSON . stringify ( manifest , null , 2 ) ,
259+ source : clientManifestStr ,
248260 } ) ;
249261
250262 if ( typeof opts . manifestOutput === 'function' ) {
251263 await opts . manifestOutput ( manifest ) ;
252264 }
265+
266+ const sys = qwikPlugin . getSys ( ) ;
267+ if ( tmpClientManifestPath && sys . env === 'node' ) {
268+ // Client build should write the manifest to a tmp dir
269+ const fs : typeof import ( 'fs' ) = await sys . dynamicImport ( 'fs' ) ;
270+ await fs . promises . writeFile ( tmpClientManifestPath , clientManifestStr ) ;
271+ }
253272 }
254273 } ,
255274
@@ -329,7 +348,7 @@ export function qwikVite(qwikViteOpts: QwikVitePluginOptions = {}): any {
329348
330349 qwikPlugin . log ( `handleSSR()` , 'symbols' , manifest ) ;
331350
332- const renderInputModule = await server . moduleGraph . getModuleByUrl ( ssrInput ! ) ;
351+ const renderInputModule = await server . moduleGraph . getModuleByUrl ( opts . input [ 0 ] ! ) ;
333352 if ( renderInputModule ) {
334353 renderInputModule . importedModules . forEach ( ( moduleNode ) => {
335354 if ( moduleNode . url . endsWith ( '.css' ) ) {
0 commit comments