@@ -38,11 +38,9 @@ final class ImportMapManager
3838
3939 public function __construct (
4040 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/ ' ,
4644 private readonly Provider $ provider = Provider::Jspm,
4745 private ?HttpClientInterface $ httpClient = null ,
4846 private readonly string $ api = 'https://api.jspm.io ' ,
@@ -66,15 +64,21 @@ public function getImportMap(): string
6664 $ this ->loadImportMap ();
6765
6866 $ importmap = ['imports ' => []];
69- foreach ($ this ->importMap as $ package => $ data ) {
67+ foreach ($ this ->importMap as $ packageName => $ data ) {
7068 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 ' ];
7276
7377 continue ;
7478 }
7579
7680 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 ' ]);
7882 }
7983 }
8084
@@ -131,8 +135,8 @@ private function createImportMap(Env $env, ?Provider $provider, bool $update, ar
131135 $ packages = [];
132136 foreach ($ this ->importMap ?? [] as $ name => $ data ) {
133137 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 ' ]));
136140
137141 continue ;
138142 }
@@ -141,7 +145,7 @@ private function createImportMap(Env $env, ?Provider $provider, bool $update, ar
141145 continue ;
142146 }
143147
144- $ packages [$ name ] = new PackageOptions ((bool ) ($ data ['digest ' ] ?? false ), $ data ['preload ' ] ?? false );
148+ $ packages [$ name ] = new PackageOptions ((bool ) ($ data ['download ' ] ?? false ), $ data ['preload ' ] ?? false );
145149
146150 if (preg_match (self ::PACKAGE_PATTERN , $ data ['url ' ], $ matches )) {
147151 $ constraint = ($ matches ['registry ' ] ?? null ) ? "{$ matches ['registry ' ]}: {$ matches ['package ' ]}" : $ matches ['package ' ];
@@ -178,7 +182,7 @@ private function removeFromImportMap(array $remove): void {
178182 continue ;
179183 }
180184
181- $ this ->removeIfExists ($ this ->vendorDir .$ this ->importMap [$ packageName ]['digest ' ]);
185+ $ this ->removeIfExists ($ this ->assetsDir .$ this ->importMap [$ packageName ]['digest ' ]);
182186 unset($ this ->importMap [$ packageName ]);
183187 }
184188 }
@@ -192,41 +196,59 @@ private function jspmGenerate(Env $env, ?Provider $provider, array $install, arr
192196 $ json = [
193197 'install ' => array_values ($ install ),
194198 'flattenScope ' => true ,
195- 'provider ' => $ provider ?->value ?? $ this ->provider ->value ,
196199 ];
200+ $ provider = $ provider ?->value ?? $ this ->provider ->value ;
201+ if ($ provider !== Provider::Jspm) {
202+ $ json ['env ' ] = $ provider ;
203+ }
197204
198205 $ json ['env ' ] = ['browser ' , 'module ' , $ env ->value ];
199206
200207 $ response = $ this ->apiHttpClient ->request ('POST ' , '/generate ' , [
201208 'json ' => $ json ,
202209 ]);
203210
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 ));
208213
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 ) {
209223 if ($ packages [$ packageName ]->preload ) {
210224 $ this ->importMap [$ packageName ]['preload ' ] = true ;
225+ } else {
226+ unset($ this ->importMap [$ packageName ]['preload ' ]);
211227 }
212228
229+ $ relativePath = 'vendor/ ' .$ packageName .'.js ' ;
213230 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 ' ]);
216236
217- $ this ->importMap [$ packageName ]['digest ' ] = sprintf ('%s.%s.js ' , $ packageName , hash ('xxh128 ' , $ url ));
218- if ($ this ->importMap [$ packageName ]['digest ' ] === ($ previousPackageData ['digest ' ] ?? null )) {
219237 continue ;
220238 }
221239
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 ;
224243 }
225244
245+ $ this ->importMap [$ packageName ]['url ' ] = $ url ;
226246 $ this ->filesystem ->dumpFile (
227- $ this ->vendorDir . $ this -> importMap [ $ packageName ][ ' digest ' ] ,
247+ $ this ->assetsDir . $ relativePath ,
228248 $ this ->httpClient ->request ('GET ' , $ url )->getContent (),
229249 );
250+ $ this ->filesystem ->mkdir ($ this ->publicAssetsDir );
251+ $ this ->filesystem ->copy ($ this ->assetsDir .$ relativePath , $ this ->publicAssetsDir .'vendor/ ' .$ this ->digestName ($ packageName , $ relativePath ).'.js ' );
230252 }
231253 }
232254
@@ -237,8 +259,18 @@ private function removeIfExists(string $path): void
237259 }
238260 }
239261
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
241273 {
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 ));
243275 }
244276}
0 commit comments