@@ -4,6 +4,7 @@ import path from "node:path";
44import  {  fileURLToPath  }  from  "node:url" ; 
55import  MagicString  from  "magic-string" ; 
66import  {  toNodeHandler  }  from  "srvx/node" ; 
7+ import  {  stripLiteral  }  from  "strip-literal" ; 
78import  { 
89  DevEnvironment , 
910  type  Plugin , 
@@ -131,8 +132,8 @@ export function assetsPlugin(pluginOpts?: FullstackPluginOptions): Plugin[] {
131132        async  handler ( code ,  id ,  _options )  { 
132133          if  ( ! code . includes ( "import.meta.vite.assets" ) )  return ; 
133134
134-           // TODO: strip comments 
135135          const  output  =  new  MagicString ( code ) ; 
136+           const  strippedCode  =  stripLiteral ( code ) ; 
136137
137138          const  emptyResult : ImportAssetsResult  =  { 
138139            js : [ ] , 
@@ -146,6 +147,15 @@ export function assetsPlugin(pluginOpts?: FullstackPluginOptions): Plugin[] {
146147          ) )  { 
147148            const  [ start ,  end ]  =  match . indices ! [ 0 ] ! ; 
148149
150+             // skip if inside comment or string literal 
151+             if  ( 
152+               ! strippedCode 
153+                 . slice ( start ,  end ) 
154+                 . includes ( "import.meta.vite.assets" ) 
155+             )  { 
156+               continue ; 
157+             } 
158+ 
149159            // No-op on client since vite build handles preload/css for dynamic import on client. 
150160            // https://vite.dev/guide/features.html#async-chunk-loading-optimization 
151161            if  ( this . environment . name  ===  "client" )  { 
@@ -155,28 +165,44 @@ export function assetsPlugin(pluginOpts?: FullstackPluginOptions): Plugin[] {
155165            } 
156166
157167            const  argCode  =  match [ 1 ] ! . trim ( ) ; 
158-             const  options :  Required < ImportAssetsOptions >  =  { 
168+             const  options  =  { 
159169              import : id , 
160-               environment : this . environment . name , 
170+               environment : undefined , 
161171              asEntry : false , 
162-             } ; 
172+             }   satisfies   ImportAssetsOptions ; 
163173            if  ( argCode )  { 
164174              const  argValue  =  evalValue < ImportAssetsOptions > ( argCode ) ; 
165175              Object . assign ( options ,  argValue ) ; 
166176            } 
167177
168-             const  importSource  =  toAssetsVirtual ( { 
169-               import : options . import , 
170-               importer : id , 
171-               environment : options . environment , 
172-               entry : options . asEntry  ? "1"  : "" , 
173-             } ) ; 
174-             const  hash  =  hashString ( importSource ) ; 
175-             const  importedName  =  `__assets_${ hash }  ` ; 
176-             newImports . add ( 
177-               `;import ${ importedName }   from ${ JSON . stringify ( importSource ) }  ;\n` , 
178-             ) ; 
179-             output . update ( start ,  end ,  `(${ importedName }  )` ) ; 
178+             // when `environment` is omitted, import both client and 
179+             // current environment (i.e. treat is as universal route) 
180+             const  environments  =  options . environment 
181+               ? [ options . environment ] 
182+               : [ "client" ,  this . environment . name ] ; 
183+             const  importedNames : string [ ]  =  [ ] ; 
184+             for  ( const  environment  of  environments )  { 
185+               const  importSource  =  toAssetsVirtual ( { 
186+                 import : options . import , 
187+                 importer : id , 
188+                 environment, 
189+                 entry : options . asEntry  ? "1"  : "" , 
190+               } ) ; 
191+               const  hash  =  hashString ( importSource ) ; 
192+               const  importedName  =  `__assets_${ hash }  ` ; 
193+               newImports . add ( 
194+                 `;import ${ importedName }   from ${ JSON . stringify ( importSource ) }  ;\n` , 
195+               ) ; 
196+               importedNames . push ( importedName ) ; 
197+             } 
198+             let  replacement  =  importedNames [ 0 ] ! ; 
199+             if  ( importedNames . length  >  1 )  { 
200+               newImports . add ( 
201+                 `;import * as __assets_runtime from "@hiogawa/vite-plugin-fullstack/runtime";\n` , 
202+               ) ; 
203+               replacement  =  `__assets_runtime.mergeAssets(${ importedNames . join ( ", " ) }  )` ; 
204+             } 
205+             output . update ( start ,  end ,  `(${ replacement }  )` ) ; 
180206          } 
181207
182208          if  ( output . hasChanged ( ) )  { 
0 commit comments