@@ -371,4 +371,103 @@ public function testGetIncludeViteBuiltRequirementsWithAdditionalRequirementsInc
371371 }
372372 }
373373 }
374+
375+ public function testGetViteModulePreloadsIncludesJsImportsAndSkipsCss (): void
376+ {
377+ $ this ->testClass ->setDistPath ('app/client/dist/ ' );
378+
379+ $ manifest = [
380+ 'app/client/src/index.ts ' => [
381+ 'file ' => 'index-abc.js ' ,
382+ 'imports ' => [
383+ '_Registry-xyz.js ' ,
384+ '_Registry-css.js ' ,
385+ '_Loader-def.js ' ,
386+ '_Registry-xyz.js ' ,
387+ ],
388+ ],
389+ '_Registry-xyz.js ' => ['file ' => 'Registry-xyz.js ' ],
390+ '_Registry-css.js ' => ['file ' => 'Registry-xyz.css ' ],
391+ '_Loader-def.js ' => ['file ' => 'Loader-def.js ' ],
392+ ];
393+
394+ $ preloads = $ this ->testClass ->getViteModulePreloads ($ manifest , [
395+ 'app/client/src/index.ts ' ,
396+ ]);
397+
398+ $ this ->assertEquals (2 , $ preloads ->count ());
399+ $ this ->assertEquals (
400+ '/_resources/app/client/dist/Registry-xyz.js ' ,
401+ $ preloads ->first ()->Asset
402+ );
403+ $ this ->assertEquals (
404+ '/_resources/app/client/dist/Loader-def.js ' ,
405+ $ preloads ->last ()->Asset
406+ );
407+ }
408+
409+ public function testGetViteModulePreloadsFromAdditionalJsEntries (): void
410+ {
411+ $ manifest = [
412+ 'app/client/src/index.ts ' => [
413+ 'file ' => 'index-abc.js ' ,
414+ ],
415+ 'app/client/src/additional.jsx ' => [
416+ 'file ' => 'additional-ghi.js ' ,
417+ 'imports ' => ['_shared-jkl.js ' ],
418+ ],
419+ '_shared-jkl.js ' => ['file ' => 'shared-jkl.js ' ],
420+ ];
421+
422+ $ preloads = $ this ->testClass ->getViteModulePreloads ($ manifest , [
423+ 'app/client/src/index.ts ' ,
424+ 'app/client/src/additional.jsx ' ,
425+ ]);
426+
427+ $ this ->assertEquals (1 , $ preloads ->count ());
428+ $ this ->assertEquals (
429+ '/_resources/app/client/dist/shared-jkl.js ' ,
430+ $ preloads ->first ()->Asset
431+ );
432+ }
433+
434+ public function testGetIncludeViteBuiltRequirementsEmitsModulePreloadLinks (): void
435+ {
436+ $ manifestPath = Director::baseFolder () . '/app/client/dist/manifest.json ' ;
437+ $ dir = dirname ($ manifestPath );
438+
439+ if (!is_dir ($ dir )) {
440+ mkdir ($ dir , 0755 , true );
441+ }
442+
443+ $ manifestData = [
444+ 'app/client/src/index.css ' => ['file ' => 'index.css ' ],
445+ 'app/client/src/index.ts ' => [
446+ 'file ' => 'index.ts ' ,
447+ 'imports ' => ['_Registry-xyz.js ' ],
448+ ],
449+ '_Registry-xyz.js ' => ['file ' => 'Registry-xyz.js ' ],
450+ ];
451+
452+ file_put_contents ($ manifestPath , json_encode ($ manifestData ));
453+
454+ $ mockCache = $ this ->createMock (CacheInterface::class);
455+ $ mockCache ->method ('has ' )->willReturn (false );
456+ $ mockCache ->method ('set ' )->willReturn (true );
457+ $ mockCache ->method ('get ' )->willReturn ($ manifestData );
458+
459+ Injector::inst ()->registerService ($ mockCache , CacheInterface::class . '.ViteRequirementsManifest ' );
460+
461+ try {
462+ $ result = $ this ->testClass ->getIncludeViteBuiltRequirements ();
463+
464+ $ this ->assertStringContainsString ('rel="modulepreload" ' , $ result );
465+ $ this ->assertStringContainsString ('Registry-xyz.js ' , $ result );
466+ $ this ->assertStringContainsString ('script type="module" ' , $ result );
467+ } finally {
468+ if (file_exists ($ manifestPath )) {
469+ unlink ($ manifestPath );
470+ }
471+ }
472+ }
374473}
0 commit comments