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.

103 lines
3.3 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. if ($bundler->isViteDevEnabled()) {
  55. return '';
  56. }
  57. return $bundler->getEntrypointStylesheets($entrypointName);
  58. }
  59. /**
  60. * @param string $entrypointName
  61. * @return string
  62. * @throws AssetsManifestException
  63. */
  64. public static function viteEntrypointAssets(string $entrypointName): string
  65. {
  66. $bundler = app()->get(Bundler::class);
  67. assert($bundler instanceof Bundler);
  68. $bundler->validateBundlerType(Bundler::BUNDLER_VITE);
  69. assert($bundler instanceof ViteBundler);
  70. if ($bundler->isViteDevEnabled()) {
  71. return $bundler->getScriptTag($entrypointName);
  72. } else {
  73. return $bundler->getEntrypointAssets($entrypointName);
  74. }
  75. }
  76. public static function viteDevClientScriptTag()
  77. {
  78. $bundler = app()->get(Bundler::class);
  79. assert($bundler instanceof Bundler);
  80. $bundler->validateBundlerType(Bundler::BUNDLER_VITE);
  81. assert($bundler instanceof ViteBundler);
  82. if ($bundler->isViteDevEnabled()) {
  83. return $bundler->getViteDevClientScriptTag();
  84. }
  85. return '';
  86. }
  87. }