You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

95 lines
3.1 KiB

  1. <?php namespace Wpstudio\AssetsManifest\Classes;
  2. use Wpstudio\AssetsManifest\Classes\Bundlers\Bundler;
  3. use Wpstudio\AssetsManifest\Classes\Bundlers\ViteBundler;
  4. use Wpstudio\AssetsManifest\Classes\Bundlers\WebpackEncoreBundler;
  5. class TwigFilters
  6. {
  7. /**
  8. * @param string $assetName
  9. * @return string
  10. * @throws AssetsManifestException
  11. * @throws \Psr\Container\ContainerExceptionInterface
  12. * @throws \Psr\Container\NotFoundExceptionInterface
  13. */
  14. public static function encoreAsset(string $assetName): string
  15. {
  16. $bundler = app()->get(Bundler::class);
  17. assert($bundler instanceof Bundler);
  18. $bundler->validateBundlerType(Bundler::BUNDLER_WEBPACK_ENCORE);
  19. assert($bundler instanceof WebpackEncoreBundler);
  20. return $bundler->getEntrypoint($assetName);
  21. }
  22. public static function hmrAssets()
  23. {
  24. if (\Config::get('app.debug')) {
  25. $bundler = app()->get(Bundler::class);
  26. assert($bundler instanceof Bundler);
  27. $bundler->validateBundlerType(Bundler::BUNDLER_WEBPACK_ENCORE);
  28. assert($bundler instanceof WebpackEncoreBundler);
  29. $manifestReader = app()->get(ManifestReader::class);
  30. assert($bundler instanceof ManifestReader);
  31. $startsWithSubstring = 'vendors-node_modules';
  32. foreach ($manifestReader->toArray() as $assetName => $assetsFullPath) {
  33. if (starts_with($assetName, $startsWithSubstring)) {
  34. if (ends_with($assetName, '.css')) {
  35. echo '<link rel="stylesheet" href="' . $assetsFullPath . '">' . PHP_EOL;
  36. } else {
  37. echo '<script src="' . $assetsFullPath . '"></script>' . PHP_EOL;
  38. }
  39. }
  40. }
  41. }
  42. }
  43. /**
  44. * @param string $entrypointName
  45. * @return string
  46. * @throws AssetsManifestException
  47. */
  48. public static function viteEntrypointStyles(string $entrypointName): string
  49. {
  50. $bundler = app()->get(Bundler::class);
  51. assert($bundler instanceof Bundler);
  52. $bundler->validateBundlerType(Bundler::BUNDLER_VITE);
  53. assert($bundler instanceof ViteBundler);
  54. return $bundler->getEntrypointStylesheets($entrypointName);
  55. }
  56. /**
  57. * @param string $entrypointName
  58. * @return string
  59. * @throws AssetsManifestException
  60. */
  61. public static function viteEntrypointAssets(string $entrypointName): string
  62. {
  63. $bundler = app()->get(Bundler::class);
  64. assert($bundler instanceof Bundler);
  65. $bundler->validateBundlerType(Bundler::BUNDLER_VITE);
  66. assert($bundler instanceof ViteBundler);
  67. return $bundler->getEntrypointAssets($entrypointName);
  68. }
  69. public static function viteDevClientScriptTag()
  70. {
  71. $bundler = app()->get(Bundler::class);
  72. assert($bundler instanceof Bundler);
  73. $bundler->validateBundlerType(Bundler::BUNDLER_VITE);
  74. assert($bundler instanceof ViteBundler);
  75. if ($bundler->isViteDevEnabled()) {
  76. return $bundler->getViteDevClientScriptTag();
  77. }
  78. return '';
  79. }
  80. }