1
1
use std:: io:: Write ;
2
2
3
- use anyhow:: Result ;
3
+ use anyhow:: { Context , Result } ;
4
+ use either:: Either ;
4
5
use indoc:: writedoc;
5
- 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
+ } ;
6
11
use turbo_tasks_fs:: File ;
7
12
use turbopack_core:: {
8
13
asset:: { Asset , AssetContent } ,
@@ -20,10 +25,18 @@ use super::{
20
25
update:: update_chunk_list,
21
26
version:: EcmascriptDevChunkListVersion ,
22
27
} ;
28
+ use crate :: chunking_context:: CurrentChunkMethod ;
29
+
30
+ #[ derive( Clone , Debug , Serialize , Deserialize , TraceRawVcs , PartialEq , Eq , NonLocalValue ) ]
31
+ enum CurrentChunkMethodWithData {
32
+ StringLiteral ( RcStr ) ,
33
+ DocumentCurrentScript ,
34
+ }
23
35
24
36
/// Contents of an [`EcmascriptDevChunkList`].
25
37
#[ turbo_tasks:: value]
26
38
pub ( super ) struct EcmascriptDevChunkListContent {
39
+ current_chunk_method : CurrentChunkMethodWithData ,
27
40
pub ( super ) chunks_contents : FxIndexMap < String , ResolvedVc < Box < dyn VersionedContent > > > ,
28
41
source : EcmascriptDevChunkListSource ,
29
42
}
@@ -35,21 +48,35 @@ impl EcmascriptDevChunkListContent {
35
48
pub async fn new ( chunk_list : Vc < EcmascriptDevChunkList > ) -> Result < Vc < Self > > {
36
49
let chunk_list_ref = chunk_list. await ?;
37
50
let output_root = chunk_list_ref. chunking_context . output_root ( ) . await ?;
51
+ let current_chunk_method = match * chunk_list_ref
52
+ . chunking_context
53
+ . current_chunk_method ( )
54
+ . await ?
55
+ {
56
+ CurrentChunkMethod :: StringLiteral => {
57
+ let path = output_root
58
+ . get_path_to ( & * chunk_list. path ( ) . await ?)
59
+ . context ( "chunk list path not in output root" ) ?
60
+ . into ( ) ;
61
+ CurrentChunkMethodWithData :: StringLiteral ( path)
62
+ }
63
+ CurrentChunkMethod :: DocumentCurrentScript => {
64
+ CurrentChunkMethodWithData :: DocumentCurrentScript
65
+ }
66
+ } ;
38
67
Ok ( EcmascriptDevChunkListContent {
68
+ current_chunk_method,
39
69
chunks_contents : chunk_list_ref
40
70
. chunks
41
71
. await ?
42
72
. iter ( )
43
- . map ( |chunk| {
44
- let output_root = output_root. clone ( ) ;
45
- async move {
46
- Ok ( (
47
- output_root
48
- . get_path_to ( & * chunk. path ( ) . await ?)
49
- . map ( |path| path. to_string ( ) ) ,
50
- chunk. versioned_content ( ) . to_resolved ( ) . await ?,
51
- ) )
52
- }
73
+ . map ( async |chunk| {
74
+ Ok ( (
75
+ output_root
76
+ . get_path_to ( & * chunk. path ( ) . await ?)
77
+ . map ( |path| path. to_string ( ) ) ,
78
+ chunk. versioned_content ( ) . to_resolved ( ) . await ?,
79
+ ) )
53
80
} )
54
81
. try_join ( )
55
82
. await ?
@@ -111,6 +138,13 @@ impl EcmascriptDevChunkListContent {
111
138
. map ( |s| s. as_str ( ) )
112
139
. collect :: < Vec < _ > > ( ) ;
113
140
141
+ let script_or_path = match & this. current_chunk_method {
142
+ CurrentChunkMethodWithData :: StringLiteral ( path) => Either :: Left ( StringifyJs ( path) ) ,
143
+ CurrentChunkMethodWithData :: DocumentCurrentScript => {
144
+ Either :: Right ( "document.currentScript" )
145
+ }
146
+ } ;
147
+
114
148
let mut code = CodeBuilder :: default ( ) ;
115
149
116
150
// When loaded, JS chunks must register themselves with the `TURBOPACK` global
@@ -120,11 +154,11 @@ impl EcmascriptDevChunkListContent {
120
154
code,
121
155
r#"
122
156
(globalThis.TURBOPACK = globalThis.TURBOPACK || []).push([
123
- document.currentScript ,
157
+ {script_or_path} ,
124
158
{{}},
125
159
]);
126
160
(globalThis.TURBOPACK_CHUNK_LISTS = globalThis.TURBOPACK_CHUNK_LISTS || []).push({{
127
- script: document.currentScript ,
161
+ script: {script_or_path} ,
128
162
chunks: {:#},
129
163
source: {:#}
130
164
}});
0 commit comments