@@ -3,21 +3,25 @@ use pack_core::client::context::{
33 get_client_module_options_context, get_client_resolve_options_context,
44 get_client_runtime_entries,
55} ;
6+ use pack_core:: config:: Platform ;
7+ use pack_core:: server:: contexts:: {
8+ get_server_module_options_context, get_server_resolve_options_context,
9+ } ;
610use pack_core:: util:: convert_to_project_relative;
7- use qstring:: QString ;
811use tracing:: Instrument ;
912use turbo_rcstr:: { RcStr , rcstr} ;
1013use turbo_tasks:: { Completion , JoinIterExt , ResolvedVc , TryJoinIterExt , ValueToString , Vc } ;
1114use turbo_tasks_fs:: { File , FileContent } ;
1215use turbopack:: {
1316 ModuleAssetContext , module_options:: ModuleOptionsContext , transition:: TransitionOptions ,
1417} ;
18+ use turbopack_core:: chunk:: ChunkingContextExt ;
19+ use turbopack_core:: output:: OutputAssetsWithReferenced ;
1520use turbopack_core:: resolve:: origin:: ResolveOrigin ;
1621use turbopack_core:: {
1722 asset:: AssetContent ,
1823 chunk:: {
19- ChunkGroupResult , ChunkingContext , EvaluatableAsset , EvaluatableAssets ,
20- availability_info:: AvailabilityInfo ,
24+ ChunkingContext , EvaluatableAsset , EvaluatableAssets , availability_info:: AvailabilityInfo ,
2125 } ,
2226 context:: AssetContext ,
2327 ident:: { AssetIdent , Layer } ,
@@ -181,58 +185,101 @@ impl AppEntrypoint {
181185 }
182186
183187 #[ turbo_tasks:: function]
184- async fn chunk_group_for_entry (
188+ async fn client_chunk_group (
185189 self : Vc < Self > ,
186190 asset_context : Vc < Box < dyn AssetContext > > ,
187191 runtime_entries : Vc < EvaluatableAssets > ,
188- ) -> Result < Vc < ChunkGroupResult > > {
192+ ) -> Result < Vc < OutputAssetsWithReferenced > > {
189193 async move {
190194 let this = self . await ?;
191195
192196 let project = self . project ( ) ;
193197
194- let app_chunking_context = project. client_chunking_context ( ) ;
195-
196198 let module_graph = self . module_graph_for_entry ( asset_context, runtime_entries) ;
197199
198- let query = QString :: new ( vec ! [ ( "name" , this. name. as_str( ) ) ] ) . to_string ( ) ;
199- let query = if query. is_empty ( ) {
200- // If name is empty, provide a default fallback
201- QString :: new ( vec ! [ ( "name" , "index" ) ] ) . to_string ( )
202- } else {
203- format ! ( "?{query}" )
204- } ;
200+ let query = format ! ( "?name={}" , this. name) ;
205201
206- let app_chunk_group = app_chunking_context. evaluated_chunk_group (
207- AssetIdent :: from_path ( project. project_path ( ) . await ?. join ( this. import . as_str ( ) ) ?)
202+ let app_chunk_group = project
203+ . client_chunking_context ( )
204+ . evaluated_chunk_group_assets (
205+ AssetIdent :: from_path (
206+ project. project_path ( ) . await ?. join ( this. import . as_str ( ) ) ?,
207+ )
208208 . with_query ( query. into ( ) ) ,
209- ChunkGroup :: Entry (
210- self . entry_evaluatable_assets ( asset_context, runtime_entries)
211- . await ?
212- . iter ( )
213- . map ( |m| ResolvedVc :: upcast ( * m) )
214- . collect ( ) ,
215- ) ,
216- module_graph,
217- AvailabilityInfo :: root ( ) ,
218- ) ;
209+ ChunkGroup :: Entry (
210+ self . entry_evaluatable_assets ( asset_context, runtime_entries)
211+ . await ?
212+ . iter ( )
213+ . map ( |m| ResolvedVc :: upcast ( * m) )
214+ . collect ( ) ,
215+ ) ,
216+ module_graph,
217+ AvailabilityInfo :: root ( ) ,
218+ ) ;
219219
220220 Ok ( app_chunk_group)
221221 }
222222 . instrument ( tracing:: trace_span!( "app chunk rendering" ) )
223223 . await
224224 }
225225
226+ #[ turbo_tasks:: function]
227+ async fn server_chunk_group (
228+ self : Vc < Self > ,
229+ asset_context : Vc < Box < dyn AssetContext > > ,
230+ runtime_entries : Vc < EvaluatableAssets > ,
231+ ) -> Result < Vc < OutputAssetsWithReferenced > > {
232+ async move {
233+ let this = self . await ?;
234+
235+ let project = self . project ( ) ;
236+
237+ let module_graph = self . module_graph_for_entry ( asset_context, runtime_entries) ;
238+
239+ let app_chunk_group = project
240+ . server_chunking_context ( )
241+ . entry_chunk_group (
242+ project. dist_root ( ) . owned ( ) . await ?. join ( & this. name ) ?,
243+ self . entry_evaluatable_assets ( asset_context, runtime_entries) ,
244+ module_graph,
245+ OutputAssets :: empty ( ) ,
246+ OutputAssets :: empty ( ) ,
247+ AvailabilityInfo :: root ( ) ,
248+ )
249+ . await ?;
250+
251+ Ok ( OutputAssetsWithReferenced {
252+ assets : ResolvedVc :: cell ( vec ! [ app_chunk_group. asset] ) ,
253+ referenced_assets : ResolvedVc :: cell ( vec ! [ ] ) ,
254+ references : ResolvedVc :: cell ( vec ! [ ] ) ,
255+ }
256+ . cell ( ) )
257+ }
258+ . instrument ( tracing:: trace_span!( "app chunk rendering" ) )
259+ . await
260+ }
261+
226262 #[ turbo_tasks:: function]
227263 pub async fn output_assets_for_entry (
228264 self : Vc < Self > ,
229265 asset_context : Vc < Box < dyn AssetContext > > ,
230266 runtime_entries : Vc < EvaluatableAssets > ,
231267 ) -> Result < Vc < OutputAssets > > {
232- let chunk_group_assets = * self
233- . chunk_group_for_entry ( asset_context, runtime_entries)
234- . await ?
235- . assets ;
268+ let platform = & * self . project ( ) . platform ( ) . await ?;
269+ let chunk_group_assets = match platform {
270+ Platform :: Web => {
271+ * self
272+ . client_chunk_group ( asset_context, runtime_entries)
273+ . await ?
274+ . assets
275+ }
276+ Platform :: Node => {
277+ * self
278+ . server_chunk_group ( asset_context, runtime_entries)
279+ . await ?
280+ . assets
281+ }
282+ } ;
236283 Ok ( chunk_group_assets)
237284 }
238285}
@@ -252,66 +299,104 @@ impl AppEndpoint {
252299
253300 #[ turbo_tasks:: function]
254301 pub async fn app_runtime_entries ( self : Vc < Self > ) -> Result < Vc < EvaluatableAssets > > {
255- let watch = self . project ( ) . await ?. watch . enable ;
256- Ok ( get_client_runtime_entries (
257- self . project ( ) . project_path ( ) . owned ( ) . await ?,
258- self . project ( ) . mode ( ) ,
259- self . project ( ) . config ( ) ,
260- self . project ( ) . execution_context ( ) ,
261- self . project ( ) . pack_path ( ) . owned ( ) . await ?,
262- Vc :: cell ( watch) ,
263- Vc :: cell (
264- watch
265- && self
266- . project ( )
267- . config ( )
268- . dev_server ( )
269- . await ?
270- . hot
271- . unwrap_or_default ( ) ,
272- ) ,
273- )
274- . resolve_entries ( Vc :: upcast ( self . app_module_context ( ) ) ) )
302+ match & * self . project ( ) . platform ( ) . await ? {
303+ Platform :: Web => {
304+ let watch = self . project ( ) . await ?. watch . enable ;
305+ Ok ( get_client_runtime_entries (
306+ self . project ( ) . project_path ( ) . owned ( ) . await ?,
307+ self . project ( ) . mode ( ) ,
308+ self . project ( ) . config ( ) ,
309+ self . project ( ) . execution_context ( ) ,
310+ self . project ( ) . pack_path ( ) . owned ( ) . await ?,
311+ Vc :: cell ( watch) ,
312+ Vc :: cell (
313+ watch
314+ && self
315+ . project ( )
316+ . config ( )
317+ . dev_server ( )
318+ . await ?
319+ . hot
320+ . unwrap_or_default ( ) ,
321+ ) ,
322+ )
323+ . resolve_entries ( Vc :: upcast ( self . app_module_context ( ) ) ) )
324+ }
325+ Platform :: Node => Ok ( EvaluatableAssets :: empty ( ) ) ,
326+ }
275327 }
276328
277329 #[ turbo_tasks:: function]
278330 pub async fn app_module_context ( self : Vc < Self > ) -> Result < Vc < ModuleAssetContext > > {
331+ let platform = & * self . project ( ) . platform ( ) . await ?;
332+
333+ let compile_time_info = match platform {
334+ Platform :: Web => self . project ( ) . client_compile_time_info ( ) ,
335+ Platform :: Node => self . project ( ) . server_compile_time_info ( ) ,
336+ } ;
337+
338+ let layer = match platform {
339+ Platform :: Web => {
340+ Layer :: new_with_user_friendly_name ( rcstr ! ( "client" ) , rcstr ! ( "Browser" ) )
341+ }
342+ Platform :: Node => {
343+ Layer :: new_with_user_friendly_name ( rcstr ! ( "server" ) , rcstr ! ( "Nodejs" ) )
344+ }
345+ } ;
346+
279347 Ok ( ModuleAssetContext :: new (
280348 // FIXME:
281349 TransitionOptions {
282350 ..Default :: default ( )
283351 }
284352 . cell ( ) ,
285- self . project ( ) . client_compile_time_info ( ) ,
353+ compile_time_info ,
286354 self . app_module_options_context ( ) ,
287355 self . app_resolve_options_context ( ) ,
288- // TODO: add server Layer for SSR
289- Layer :: new_with_user_friendly_name ( rcstr ! ( "client" ) , rcstr ! ( "Browser" ) ) ,
356+ layer,
290357 ) )
291358 }
292359
293360 #[ turbo_tasks:: function]
294361 async fn app_module_options_context ( self : Vc < Self > ) -> Result < Vc < ModuleOptionsContext > > {
295- Ok ( get_client_module_options_context (
296- self . project ( ) . project_path ( ) . owned ( ) . await ?,
297- self . project ( ) . execution_context ( ) ,
298- self . project ( ) . client_compile_time_info ( ) . environment ( ) ,
299- self . project ( ) . mode ( ) ,
300- self . project ( ) . config ( ) ,
301- Vc :: cell ( self . project ( ) . await ?. watch . enable ) ,
302- self . project ( ) . pack_path ( ) . owned ( ) . await ?,
303- ) )
362+ match & * self . project ( ) . platform ( ) . await ? {
363+ Platform :: Web => Ok ( get_client_module_options_context (
364+ self . project ( ) . project_path ( ) . owned ( ) . await ?,
365+ self . project ( ) . execution_context ( ) ,
366+ self . project ( ) . client_compile_time_info ( ) . environment ( ) ,
367+ self . project ( ) . mode ( ) ,
368+ self . project ( ) . config ( ) ,
369+ Vc :: cell ( self . project ( ) . await ?. watch . enable ) ,
370+ self . project ( ) . pack_path ( ) . owned ( ) . await ?,
371+ ) ) ,
372+ Platform :: Node => Ok ( get_server_module_options_context (
373+ self . project ( ) . project_path ( ) . owned ( ) . await ?,
374+ self . project ( ) . execution_context ( ) ,
375+ self . project ( ) . server_compile_time_info ( ) . environment ( ) ,
376+ self . project ( ) . mode ( ) ,
377+ self . project ( ) . config ( ) ,
378+ ) ) ,
379+ }
304380 }
305381
306382 #[ turbo_tasks:: function]
307383 async fn app_resolve_options_context ( self : Vc < Self > ) -> Result < Vc < ResolveOptionsContext > > {
308- Ok ( get_client_resolve_options_context (
309- self . project ( ) . project_path ( ) . owned ( ) . await ?,
310- self . project ( ) . mode ( ) ,
311- self . project ( ) . config ( ) ,
312- self . project ( ) . execution_context ( ) ,
313- self . project ( ) . pack_path ( ) . owned ( ) . await ?,
314- ) )
384+ match & * self . project ( ) . platform ( ) . await ? {
385+ Platform :: Web => Ok ( get_client_resolve_options_context (
386+ self . project ( ) . project_path ( ) . owned ( ) . await ?,
387+ self . project ( ) . mode ( ) ,
388+ self . project ( ) . config ( ) ,
389+ self . project ( ) . execution_context ( ) ,
390+ self . project ( ) . pack_path ( ) . owned ( ) . await ?,
391+ ) ) ,
392+ Platform :: Node => Ok ( get_server_resolve_options_context (
393+ self . project ( ) . project_path ( ) . owned ( ) . await ?,
394+ self . project ( ) . mode ( ) ,
395+ self . project ( ) . config ( ) ,
396+ self . project ( ) . execution_context ( ) ,
397+ self . project ( ) . pack_path ( ) . owned ( ) . await ?,
398+ ) ) ,
399+ }
315400 }
316401}
317402
0 commit comments