@@ -31,6 +31,9 @@ class ImportMapRenderer
31
31
private const DEFAULT_ES_MODULE_SHIMS_POLYFILL_URL = 'https://ga.jspm.io/npm:es-module-shims@1.10.0/dist/es-module-shims.js ' ;
32
32
private const DEFAULT_ES_MODULE_SHIMS_POLYFILL_INTEGRITY = 'sha384-ie1x72Xck445i0j4SlNJ5W5iGeL3Dpa0zD48MZopgWsjNB/lt60SuG1iduZGNnJn ' ;
33
33
34
+ private const LOADER_JSON = "export default (async()=>await(await fetch('%s')).json())() " ;
35
+ private const LOADER_CSS = "document.head.appendChild(Object.assign(document.createElement('link'),{rel:'stylesheet',href:'%s'})) " ;
36
+
34
37
public function __construct (
35
38
private readonly ImportMapGenerator $ importMapGenerator ,
36
39
private readonly ?Packages $ assetPackages = null ,
@@ -48,7 +51,7 @@ public function render(string|array $entryPoint, array $attributes = []): string
48
51
$ importMapData = $ this ->importMapGenerator ->getImportMapData ($ entryPoint );
49
52
$ importMap = [];
50
53
$ modulePreloads = [];
51
- $ cssLinks = [];
54
+ $ webLinks = [];
52
55
$ polyfillPath = null ;
53
56
foreach ($ importMapData as $ importName => $ data ) {
54
57
$ path = $ data ['path ' ];
@@ -70,29 +73,34 @@ public function render(string|array $entryPoint, array $attributes = []): string
70
73
}
71
74
72
75
$ preload = $ data ['preload ' ] ?? false ;
73
- if ('css ' !== $ data ['type ' ]) {
76
+ if ('json ' === $ data ['type ' ]) {
77
+ $ importMap [$ importName ] = 'data:application/javascript, ' .str_replace ('% ' , '%25 ' , \sprintf (self ::LOADER_JSON , addslashes ($ path )));
78
+ if ($ preload ) {
79
+ $ webLinks [$ path ] = 'fetch ' ;
80
+ }
81
+ } elseif ('css ' !== $ data ['type ' ]) {
74
82
$ importMap [$ importName ] = $ path ;
75
83
if ($ preload ) {
76
84
$ modulePreloads [] = $ path ;
77
85
}
78
86
} elseif ($ preload ) {
79
- $ cssLinks [ ] = $ path ;
87
+ $ webLinks [ $ path ] = ' style ' ;
80
88
// importmap entry is a noop
81
89
$ importMap [$ importName ] = 'data:application/javascript, ' ;
82
90
} else {
83
- $ importMap [$ importName ] = 'data:application/javascript, ' .rawurlencode ( \sprintf (' document.head.appendChild(Object.assign(document.createElement("link"),{rel:"stylesheet",href:"%s"})) ' , addslashes ($ path )));
91
+ $ importMap [$ importName ] = 'data:application/javascript, ' .str_replace ( ' % ' , ' %25 ' , \sprintf (self :: LOADER_CSS , addslashes ($ path )));
84
92
}
85
93
}
86
94
87
95
$ output = '' ;
88
- foreach ($ cssLinks as $ url ) {
89
- $ url = $ this -> escapeAttributeValue ( $ url );
90
-
91
- $ output .= "\n <link rel= \" stylesheet \" href= \" $ url \" > " ;
96
+ foreach ($ webLinks as $ url => $ as ) {
97
+ if ( ' style ' === $ as ) {
98
+ $ output .= "\n <link rel= \" stylesheet \" href= \"{ $ this -> escapeAttributeValue ( $ url )}\" > " ;
99
+ }
92
100
}
93
101
94
102
if (class_exists (AddLinkHeaderListener::class) && $ request = $ this ->requestStack ?->getCurrentRequest()) {
95
- $ this ->addWebLinkPreloads ($ request , $ cssLinks );
103
+ $ this ->addWebLinkPreloads ($ request , $ webLinks );
96
104
}
97
105
98
106
$ scriptAttributes = $ attributes || $ this ->scriptAttributes ? ' ' .$ this ->createAttributesString ($ attributes ) : '' ;
@@ -186,12 +194,16 @@ private function createAttributesString(array $attributes, string $pattern = '%s
186
194
return $ attributeString ;
187
195
}
188
196
189
- private function addWebLinkPreloads (Request $ request , array $ cssLinks ): void
197
+ private function addWebLinkPreloads (Request $ request , array $ links ): void
190
198
{
191
- $ cssPreloadLinks = array_map (fn ($ url ) => (new Link ('preload ' , $ url ))->withAttribute ('as ' , 'style ' ), $ cssLinks );
199
+ foreach ($ links as $ url => $ as ) {
200
+ $ links [$ url ] = (new Link ('preload ' , $ url ))
201
+ ->withAttribute ('crossorigin ' , 'anonymous ' )
202
+ ->withAttribute ('as ' , $ as );
203
+ }
192
204
193
205
if (null === $ linkProvider = $ request ->attributes ->get ('_links ' )) {
194
- $ request ->attributes ->set ('_links ' , new GenericLinkProvider ($ cssPreloadLinks ));
206
+ $ request ->attributes ->set ('_links ' , new GenericLinkProvider ($ links ));
195
207
196
208
return ;
197
209
}
@@ -200,7 +212,7 @@ private function addWebLinkPreloads(Request $request, array $cssLinks): void
200
212
return ;
201
213
}
202
214
203
- foreach ($ cssPreloadLinks as $ link ) {
215
+ foreach ($ links as $ link ) {
204
216
$ linkProvider = $ linkProvider ->withLink ($ link );
205
217
}
206
218
0 commit comments