|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace System\Classes\Asset; |
| 4 | + |
| 5 | +use Closure; |
| 6 | +use Winter\Storm\Support\Traits\Singleton; |
| 7 | + |
| 8 | +/** |
| 9 | + * Asset Bundle manager. |
| 10 | + * |
| 11 | + * This class manages "asset bundles" registered by the core and plugins that are used by the |
| 12 | + * [mix|vite]:create commands to generate & populate the required files for a given bundle. |
| 13 | + * Bundles include information on the specific packages & versions required for the bundle |
| 14 | + * to function in the context of the Winter package (plugin or theme) it is being used in, |
| 15 | + * as well as dependencies specific to the desired compiler (e.g. mix or vite). |
| 16 | + * |
| 17 | + * @package winter\wn-system-module |
| 18 | + * @author Jack Wilkinson <[email protected]> |
| 19 | + * @copyright Winter CMS Maintainers |
| 20 | + */ |
| 21 | +class BundleManager |
| 22 | +{ |
| 23 | + use Singleton; |
| 24 | + |
| 25 | + protected const HANDLER_SETUP = '_setup'; |
| 26 | + protected const HANDLER_SCAFFOLD = '_scaffold'; |
| 27 | + |
| 28 | + /** |
| 29 | + * List of packages available to install. Allows for `$compilerName` => [`CompilerSpecificPackage`] |
| 30 | + */ |
| 31 | + protected array $defaultPackages = [ |
| 32 | + 'tailwind' => [ |
| 33 | + 'tailwindcss' => '^3.4.0', |
| 34 | + '@tailwindcss/forms' => '^0.5.3', |
| 35 | + '@tailwindcss/typography' => '^0.5.2', |
| 36 | + ], |
| 37 | + 'vue' => [ |
| 38 | + 'vue' => '^3.4.0', |
| 39 | + 'vite' => [ |
| 40 | + '@vitejs/plugin-vue' => '^5.0.5' |
| 41 | + ], |
| 42 | + ], |
| 43 | + ]; |
| 44 | + |
| 45 | + /** |
| 46 | + * List of registered asset bundles in the system |
| 47 | + */ |
| 48 | + protected array $registeredBundles = []; |
| 49 | + |
| 50 | + /** |
| 51 | + * Initialize the singleton |
| 52 | + */ |
| 53 | + public function init(): void |
| 54 | + { |
| 55 | + // Register the default bundles |
| 56 | + $this->registerCallback(function (self $manager) { |
| 57 | + $manager->registerBundles($this->defaultPackages); |
| 58 | + |
| 59 | + $manager->registerSetupHandler('tailwind', function (string $packagePath, string $packageType) { |
| 60 | + $this->writeFile( |
| 61 | + $packagePath . '/tailwind.config.js', |
| 62 | + $this->getFixture('tailwind/tailwind.' . $packageType . '.config.js.fixture') |
| 63 | + ); |
| 64 | + |
| 65 | + $this->writeFile( |
| 66 | + $packagePath . '/postcss.config.mjs', |
| 67 | + $this->getFixture('tailwind/postcss.config.js.fixture') |
| 68 | + ); |
| 69 | + }); |
| 70 | + |
| 71 | + $manager->registerScaffoldHandler('tailwind', function (string $contents, string $contentType) { |
| 72 | + return match ($contentType) { |
| 73 | + 'mix' => $contents . PHP_EOL . <<<JAVASCRIPT |
| 74 | + mix.postCss('assets/src/css/{{packageName}}.css', 'assets/dist/css/{{packageName}}.css', [ |
| 75 | + require('postcss-import'), |
| 76 | + require('tailwindcss'), |
| 77 | + require('autoprefixer'), |
| 78 | + ]); |
| 79 | + JAVASCRIPT, |
| 80 | + 'css' => $this->getFixture('css/tailwind.css.fixture'), |
| 81 | + default => $contents |
| 82 | + }; |
| 83 | + }); |
| 84 | + |
| 85 | + $manager->registerScaffoldHandler('vue', function (string $contents, string $contentType) { |
| 86 | + return match ($contentType) { |
| 87 | + 'vite' => str_replace( |
| 88 | + '}),', |
| 89 | + <<<JAVASCRIPT |
| 90 | + }), |
| 91 | + vue({ |
| 92 | + template: { |
| 93 | + transformAssetUrls: { |
| 94 | + // The Vue plugin will re-write asset URLs, when referenced |
| 95 | + // in Single File Components, to point to the Laravel web |
| 96 | + // server. Setting this to `null` allows the Laravel plugin |
| 97 | + // to instead re-write asset URLs to point to the Vite |
| 98 | + // server instead. |
| 99 | + base: null, |
| 100 | +
|
| 101 | + // The Vue plugin will parse absolute URLs and treat them |
| 102 | + // as absolute paths to files on disk. Setting this to |
| 103 | + // `false` will leave absolute URLs un-touched so they can |
| 104 | + // reference assets in the public directory as expected. |
| 105 | + includeAbsolute: false, |
| 106 | + }, |
| 107 | + }, |
| 108 | + }), |
| 109 | + JAVASCRIPT, |
| 110 | + str_replace( |
| 111 | + 'import laravel from \'laravel-vite-plugin\';', |
| 112 | + 'import laravel from \'laravel-vite-plugin\';' . PHP_EOL . 'import vue from \'@vitejs/plugin-vue\';', |
| 113 | + $contents |
| 114 | + ) |
| 115 | + ), |
| 116 | + 'mix' => str_replace( |
| 117 | + 'mix.js(\'assets/src/js/{{packageName}}.js\', \'assets/dist/js/{{packageName}}.js\');', |
| 118 | + 'mix.js(\'assets/src/js/{{packageName}}.js\', \'assets/dist/js/{{packageName}}.js\').vue({ version: 3 });', |
| 119 | + $contents |
| 120 | + ), |
| 121 | + 'js' => $this->getFixture('js/vue.js.fixture'), |
| 122 | + default => $contents |
| 123 | + }; |
| 124 | + }); |
| 125 | + }); |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Returns a list of the registered asset bundles. |
| 130 | + */ |
| 131 | + public function listRegisteredBundles(): array |
| 132 | + { |
| 133 | + return $this->registeredBundles; |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Get all bundles configured |
| 138 | + */ |
| 139 | + public function getBundles(): array |
| 140 | + { |
| 141 | + return array_keys($this->listRegisteredBundles()); |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * Get the packages for a bundle, with compiler specific packages |
| 146 | + */ |
| 147 | + public function getBundlePackages(string $name, string $assetType): array |
| 148 | + { |
| 149 | + $config = $this->listRegisteredBundles()[$name] ?? []; |
| 150 | + |
| 151 | + $packages = []; |
| 152 | + foreach ($config as $key => $value) { |
| 153 | + // Skip handlers |
| 154 | + if (in_array($key, [static::HANDLER_SETUP, static::HANDLER_SCAFFOLD])) { |
| 155 | + continue; |
| 156 | + } |
| 157 | + |
| 158 | + // Merge in any compiler specific packages for the current compiler |
| 159 | + if (is_array($value)) { |
| 160 | + if ($key === $assetType) { |
| 161 | + $packages = array_merge($packages, $value); |
| 162 | + } |
| 163 | + continue; |
| 164 | + } |
| 165 | + |
| 166 | + $packages[$key] = $value; |
| 167 | + } |
| 168 | + |
| 169 | + return $packages; |
| 170 | + } |
| 171 | + |
| 172 | + /** |
| 173 | + * Registers a callback function that defines asset bundles. The callback function |
| 174 | + * should register bundles by calling the manager's registerBundles() function. |
| 175 | + * This instance is passed to the callback function as an argument. Usage: |
| 176 | + * |
| 177 | + * BundleManager::registerCallback(function ($manager) { |
| 178 | + * $manager->registerAssetBundles([...]); |
| 179 | + * }); |
| 180 | + * |
| 181 | + */ |
| 182 | + public function registerCallback(callable $callback): static |
| 183 | + { |
| 184 | + $callback($this); |
| 185 | + |
| 186 | + return $this; |
| 187 | + } |
| 188 | + |
| 189 | + /** |
| 190 | + * Registers asset bundles. |
| 191 | + */ |
| 192 | + public function registerBundles(array $definitions): static |
| 193 | + { |
| 194 | + foreach ($definitions as $name => $definition) { |
| 195 | + $this->registerBundle($name, $definition); |
| 196 | + } |
| 197 | + |
| 198 | + return $this; |
| 199 | + } |
| 200 | + |
| 201 | + /** |
| 202 | + * Registers a single asset bundle. |
| 203 | + */ |
| 204 | + public function registerBundle(string $name, array $definition): static |
| 205 | + { |
| 206 | + $this->registeredBundles[$name] = $definition; |
| 207 | + |
| 208 | + return $this; |
| 209 | + } |
| 210 | + |
| 211 | + /** |
| 212 | + * Registers a single bundle setup handler. |
| 213 | + */ |
| 214 | + public function registerSetupHandler(string $name, Closure $closure): static |
| 215 | + { |
| 216 | + $this->registeredBundles[$name][static::HANDLER_SETUP] = $closure; |
| 217 | + |
| 218 | + return $this; |
| 219 | + } |
| 220 | + |
| 221 | + /** |
| 222 | + * Registers a single bundle scaffold handler. |
| 223 | + */ |
| 224 | + public function registerScaffoldHandler(string $name, Closure $closure): static |
| 225 | + { |
| 226 | + $this->registeredBundles[$name][static::HANDLER_SCAFFOLD] = $closure; |
| 227 | + |
| 228 | + return $this; |
| 229 | + } |
| 230 | + |
| 231 | + /** |
| 232 | + * Gets the setup handler for a bundle. |
| 233 | + */ |
| 234 | + public function getSetupHandler(string $name): ?Closure |
| 235 | + { |
| 236 | + return $this->listRegisteredBundles()[$name][static::HANDLER_SETUP] ?? null; |
| 237 | + } |
| 238 | + |
| 239 | + /** |
| 240 | + * Gets the scaffold handler for a bundle. |
| 241 | + */ |
| 242 | + public function getScaffoldHandler(string $name): ?Closure |
| 243 | + { |
| 244 | + return $this->listRegisteredBundles()[$name][static::HANDLER_SCAFFOLD] ?? null; |
| 245 | + } |
| 246 | +} |
0 commit comments