1
1
use std:: io:: Write ;
2
2
3
3
use anyhow:: { Context , Result } ;
4
+ use either:: Either ;
4
5
use indoc:: writedoc;
5
- use serde:: Serialize ;
6
- use turbo_tasks:: { FxIndexMap , IntoTraitRef , ResolvedVc , TryJoinIterExt , Vc } ;
6
+ use serde:: { Deserialize , Serialize } ;
7
+ use turbo_rcstr:: RcStr ;
8
+ use turbo_tasks:: {
9
+ trace:: TraceRawVcs , FxIndexMap , IntoTraitRef , NonLocalValue , ResolvedVc , TryJoinIterExt , Vc ,
10
+ } ;
7
11
use turbo_tasks_fs:: File ;
8
12
use turbopack_core:: {
9
13
asset:: { Asset , AssetContent } ,
@@ -21,11 +25,20 @@ use super::{
21
25
update:: update_chunk_list,
22
26
version:: EcmascriptDevChunkListVersion ,
23
27
} ;
28
+ use crate :: chunking_context:: {
29
+ CurrentChunkMethod , CURRENT_CHUNK_METHOD_DOCUMENT_CURRENT_SCRIPT_EXPR ,
30
+ } ;
31
+
32
+ #[ derive( Clone , Debug , Serialize , Deserialize , TraceRawVcs , PartialEq , Eq , NonLocalValue ) ]
33
+ enum CurrentChunkMethodWithData {
34
+ StringLiteral ( RcStr ) ,
35
+ DocumentCurrentScript ,
36
+ }
24
37
25
38
/// Contents of an [`EcmascriptDevChunkList`].
26
39
#[ turbo_tasks:: value]
27
40
pub ( super ) struct EcmascriptDevChunkListContent {
28
- chunk_list_path : String ,
41
+ current_chunk_method : CurrentChunkMethodWithData ,
29
42
pub ( super ) chunks_contents : FxIndexMap < String , ResolvedVc < Box < dyn VersionedContent > > > ,
30
43
source : EcmascriptDevChunkListSource ,
31
44
}
@@ -37,25 +50,35 @@ impl EcmascriptDevChunkListContent {
37
50
pub async fn new ( chunk_list : Vc < EcmascriptDevChunkList > ) -> Result < Vc < Self > > {
38
51
let chunk_list_ref = chunk_list. await ?;
39
52
let output_root = chunk_list_ref. chunking_context . output_root ( ) . await ?;
53
+ let current_chunk_method = match * chunk_list_ref
54
+ . chunking_context
55
+ . current_chunk_method ( )
56
+ . await ?
57
+ {
58
+ CurrentChunkMethod :: StringLiteral => {
59
+ let path = output_root
60
+ . get_path_to ( & * chunk_list. path ( ) . await ?)
61
+ . context ( "chunk list path not in output root" ) ?
62
+ . into ( ) ;
63
+ CurrentChunkMethodWithData :: StringLiteral ( path)
64
+ }
65
+ CurrentChunkMethod :: DocumentCurrentScript => {
66
+ CurrentChunkMethodWithData :: DocumentCurrentScript
67
+ }
68
+ } ;
40
69
Ok ( EcmascriptDevChunkListContent {
41
- chunk_list_path : output_root
42
- . get_path_to ( & * chunk_list. path ( ) . await ?)
43
- . context ( "chunk list path not in output root" ) ?
44
- . to_string ( ) ,
70
+ current_chunk_method,
45
71
chunks_contents : chunk_list_ref
46
72
. chunks
47
73
. await ?
48
74
. iter ( )
49
- . map ( |chunk| {
50
- let output_root = output_root. clone ( ) ;
51
- async move {
52
- Ok ( (
53
- output_root
54
- . get_path_to ( & * chunk. path ( ) . await ?)
55
- . map ( |path| path. to_string ( ) ) ,
56
- chunk. versioned_content ( ) . to_resolved ( ) . await ?,
57
- ) )
58
- }
75
+ . map ( async |chunk| {
76
+ Ok ( (
77
+ output_root
78
+ . get_path_to ( & * chunk. path ( ) . await ?)
79
+ . map ( |path| path. to_string ( ) ) ,
80
+ chunk. versioned_content ( ) . to_resolved ( ) . await ?,
81
+ ) )
59
82
} )
60
83
. try_join ( )
61
84
. await ?
@@ -111,10 +134,17 @@ impl EcmascriptDevChunkListContent {
111
134
pub ( super ) async fn code ( self : Vc < Self > ) -> Result < Vc < Code > > {
112
135
let this = self . await ?;
113
136
114
- let params = EcmascriptDevChunkListParams {
115
- path : & this. chunk_list_path ,
116
- chunks : this. chunks_contents . keys ( ) . map ( |s| s. as_str ( ) ) . collect ( ) ,
117
- source : this. source ,
137
+ let chunks = this
138
+ . chunks_contents
139
+ . keys ( )
140
+ . map ( |s| s. as_str ( ) )
141
+ . collect :: < Vec < _ > > ( ) ;
142
+
143
+ let script_or_path = match & this. current_chunk_method {
144
+ CurrentChunkMethodWithData :: StringLiteral ( path) => Either :: Left ( StringifyJs ( path) ) ,
145
+ CurrentChunkMethodWithData :: DocumentCurrentScript => {
146
+ Either :: Right ( CURRENT_CHUNK_METHOD_DOCUMENT_CURRENT_SCRIPT_EXPR )
147
+ }
118
148
} ;
119
149
120
150
let mut code = CodeBuilder :: default ( ) ;
@@ -125,14 +155,14 @@ impl EcmascriptDevChunkListContent {
125
155
writedoc ! (
126
156
code,
127
157
r#"
128
- (globalThis.TURBOPACK = globalThis.TURBOPACK || []).push([
129
- { },
130
- {{} },
131
- ]);
132
- (globalThis.TURBOPACK_CHUNK_LISTS = globalThis.TURBOPACK_CHUNK_LISTS || []).push({:# });
158
+ (globalThis.TURBOPACK_CHUNK_LISTS = globalThis.TURBOPACK_CHUNK_LISTS || []).push({{
159
+ script: {script_or_path },
160
+ chunks: {:# },
161
+ source: {:#}
162
+ } });
133
163
"# ,
134
- StringifyJs ( & this . chunk_list_path ) ,
135
- StringifyJs ( & params ) ,
164
+ StringifyJs ( & chunks ) ,
165
+ StringifyJs ( & this . source ) ,
136
166
) ?;
137
167
138
168
Ok ( Code :: cell ( code. build ( ) ) )
@@ -159,14 +189,3 @@ impl VersionedContent for EcmascriptDevChunkListContent {
159
189
update_chunk_list ( self , from_version)
160
190
}
161
191
}
162
-
163
- #[ derive( Debug , Clone , Serialize ) ]
164
- #[ serde( rename_all = "camelCase" ) ]
165
- struct EcmascriptDevChunkListParams < ' a > {
166
- /// Path to the chunk list to register.
167
- path : & ' a str ,
168
- /// All chunks that belong to the chunk list.
169
- chunks : Vec < & ' a str > ,
170
- /// Where this chunk list is from.
171
- source : EcmascriptDevChunkListSource ,
172
- }
0 commit comments