@@ -38,11 +38,9 @@ final class ImportMapManager
38
38
39
39
public function __construct (
40
40
private readonly string $ path = 'importmap.php ' ,
41
- private readonly string $ vendorDir = 'public/vendor/ ' ,
42
- private readonly string $ vendorUrl = '/vendor/ ' ,
43
- private readonly string $ javascriptDir = 'javascript/ ' ,
44
- private readonly string $ publicJavascriptDir = 'public/javascript/ ' ,
45
- private readonly string $ javascriptUrl = '/javascript/ ' ,
41
+ private readonly string $ assetsDir = 'assets/ ' ,
42
+ private readonly string $ publicAssetsDir = 'public/assets/ ' ,
43
+ private readonly string $ assetsUrl = '/assets/ ' ,
46
44
private readonly Provider $ provider = Provider::Jspm,
47
45
private ?HttpClientInterface $ httpClient = null ,
48
46
private readonly string $ api = 'https://api.jspm.io ' ,
@@ -66,15 +64,21 @@ public function getImportMap(): string
66
64
$ this ->loadImportMap ();
67
65
68
66
$ importmap = ['imports ' => []];
69
- foreach ($ this ->importMap as $ package => $ data ) {
67
+ foreach ($ this ->importMap as $ packageName => $ data ) {
70
68
if (isset ($ data ['url ' ])) {
71
- $ importmap ['imports ' ][$ package ] = isset ($ data ['digest ' ]) ? $ this ->vendorUrl .$ data ['digest ' ] : $ data ['url ' ];
69
+ if ($ data ['download ' ] ?? false ) {
70
+ $ importmap ['imports ' ][$ packageName ] = $ this ->vendorUrl ($ packageName );
71
+
72
+ continue ;
73
+ }
74
+
75
+ $ importmap ['imports ' ][$ packageName ] = $ data ['url ' ];
72
76
73
77
continue ;
74
78
}
75
79
76
80
if (isset ($ data ['path ' ])) {
77
- $ importmap ['imports ' ][$ package ] = $ this ->javascriptUrl .$ this ->digestName ($ package , $ data ['path ' ]);
81
+ $ importmap ['imports ' ][$ packageName ] = $ this ->assetsUrl .$ this ->digestName ($ packageName , $ data ['path ' ]);
78
82
}
79
83
}
80
84
@@ -131,8 +135,8 @@ private function createImportMap(Env $env, ?Provider $provider, bool $update, ar
131
135
$ packages = [];
132
136
foreach ($ this ->importMap ?? [] as $ name => $ data ) {
133
137
if (isset ($ data ['path ' ])) {
134
- $ this ->filesystem ->mkdir ($ this ->publicJavascriptDir );
135
- $ this ->filesystem ->copy ($ this ->javascriptDir .$ data ['path ' ], $ this ->publicJavascriptDir .$ this ->digestName ($ name , $ data ['path ' ]));
138
+ $ this ->filesystem ->mkdir ($ this ->publicAssetsDir );
139
+ $ this ->filesystem ->copy ($ this ->assetsDir .$ data ['path ' ], $ this ->publicAssetsDir .$ this ->digestName ($ name , $ data ['path ' ]));
136
140
137
141
continue ;
138
142
}
@@ -141,7 +145,7 @@ private function createImportMap(Env $env, ?Provider $provider, bool $update, ar
141
145
continue ;
142
146
}
143
147
144
- $ packages [$ name ] = new PackageOptions ((bool ) ($ data ['digest ' ] ?? false ), $ data ['preload ' ] ?? false );
148
+ $ packages [$ name ] = new PackageOptions ((bool ) ($ data ['download ' ] ?? false ), $ data ['preload ' ] ?? false );
145
149
146
150
if (preg_match (self ::PACKAGE_PATTERN , $ data ['url ' ], $ matches )) {
147
151
$ constraint = ($ matches ['registry ' ] ?? null ) ? "{$ matches ['registry ' ]}: {$ matches ['package ' ]}" : $ matches ['package ' ];
@@ -178,7 +182,7 @@ private function removeFromImportMap(array $remove): void {
178
182
continue ;
179
183
}
180
184
181
- $ this ->removeIfExists ($ this ->vendorDir .$ this ->importMap [$ packageName ]['digest ' ]);
185
+ $ this ->removeIfExists ($ this ->assetsDir .$ this ->importMap [$ packageName ]['digest ' ]);
182
186
unset($ this ->importMap [$ packageName ]);
183
187
}
184
188
}
@@ -192,41 +196,59 @@ private function jspmGenerate(Env $env, ?Provider $provider, array $install, arr
192
196
$ json = [
193
197
'install ' => array_values ($ install ),
194
198
'flattenScope ' => true ,
195
- 'provider ' => $ provider ?->value ?? $ this ->provider ->value ,
196
199
];
200
+ $ provider = $ provider ?->value ?? $ this ->provider ->value ;
201
+ if ($ provider !== Provider::Jspm) {
202
+ $ json ['env ' ] = $ provider ;
203
+ }
197
204
198
205
$ json ['env ' ] = ['browser ' , 'module ' , $ env ->value ];
199
206
200
207
$ response = $ this ->apiHttpClient ->request ('POST ' , '/generate ' , [
201
208
'json ' => $ json ,
202
209
]);
203
210
204
- $ this ->filesystem ->mkdir ($ this ->vendorDir );
205
- foreach ($ response ->toArray ()['map ' ]['imports ' ] as $ packageName => $ url ) {
206
- $ previousPackageData = $ this ->importMap [$ packageName ] ?? null ;
207
- $ this ->importMap [$ packageName ] = ['url ' => $ url ];
211
+ if ($ response ->getStatusCode () !== 200 ) {
212
+ $ data = $ response ->toArray ((false ));
208
213
214
+ if ($ data ['error ' ]) {
215
+ throw new \RuntimeException ($ data ['error ' ]);
216
+ }
217
+
218
+ $ response ->getHeaders ();
219
+ }
220
+
221
+ $ this ->filesystem ->mkdir ($ this ->assetsDir .'vendor/ ' );
222
+ foreach ($ response ->toArray ()['map ' ]['imports ' ] as $ packageName => $ url ) {
209
223
if ($ packages [$ packageName ]->preload ) {
210
224
$ this ->importMap [$ packageName ]['preload ' ] = true ;
225
+ } else {
226
+ unset($ this ->importMap [$ packageName ]['preload ' ]);
211
227
}
212
228
229
+ $ relativePath = 'vendor/ ' .$ packageName .'.js ' ;
213
230
if (!$ packages [$ packageName ]->download ) {
214
- continue ;
215
- }
231
+ if ($ this ->importMap [$ packageName ]['download ' ] ?? false ) {
232
+ $ this ->removeIfExists ($ this ->assetsDir .$ relativePath );
233
+ // todo: remove parent dir if empty
234
+ }
235
+ unset($ this ->importMap [$ packageName ]['download ' ]);
216
236
217
- $ this ->importMap [$ packageName ]['digest ' ] = sprintf ('%s.%s.js ' , $ packageName , hash ('xxh128 ' , $ url ));
218
- if ($ this ->importMap [$ packageName ]['digest ' ] === ($ previousPackageData ['digest ' ] ?? null )) {
219
237
continue ;
220
238
}
221
239
222
- if (isset ($ previousPackageData ['digest ' ])) {
223
- $ this ->removeIfExists ($ this ->vendorDir .$ previousPackageData ['digest ' ]);
240
+ $ this ->importMap [$ packageName ]['download ' ] = true ;
241
+ if ($ previousPackageData ['url ' ] ?? null === $ url ) {
242
+ continue ;
224
243
}
225
244
245
+ $ this ->importMap [$ packageName ]['url ' ] = $ url ;
226
246
$ this ->filesystem ->dumpFile (
227
- $ this ->vendorDir . $ this -> importMap [ $ packageName ][ ' digest ' ] ,
247
+ $ this ->assetsDir . $ relativePath ,
228
248
$ this ->httpClient ->request ('GET ' , $ url )->getContent (),
229
249
);
250
+ $ this ->filesystem ->mkdir ($ this ->publicAssetsDir );
251
+ $ this ->filesystem ->copy ($ this ->assetsDir .$ relativePath , $ this ->publicAssetsDir .'vendor/ ' .$ this ->digestName ($ packageName , $ relativePath ).'.js ' );
230
252
}
231
253
}
232
254
@@ -237,8 +259,18 @@ private function removeIfExists(string $path): void
237
259
}
238
260
}
239
261
240
- private function digestName (string $ package , string $ path ): string
262
+ private function digestName (string $ packageName , string $ path ): string
263
+ {
264
+ return sprintf ('%s.%s.js ' , $ packageName , hash ('xxh128 ' , file_get_contents ($ this ->assetsDir .$ path )));
265
+ }
266
+
267
+ private function vendorPath (string $ packageName ): string
268
+ {
269
+ return $ this ->assetsUrl .'vendor/ ' .$ packageName .'.js ' ;
270
+ }
271
+
272
+ private function vendorUrl (string $ packageName ): string
241
273
{
242
- return sprintf ( ' %s.%s.js ' , $ package , hash ( ' xxh128 ' , file_get_contents ( $ this ->javascriptDir . $ path ) ));
274
+ return $ this -> publicAssetsDir . ' vendor/ ' . $ this -> digestName ( $ packageName , $ this ->vendorPath ( $ packageName ));
243
275
}
244
276
}
0 commit comments