Skip to content

Commit

Permalink
Added asset BundleManager docs (#203)
Browse files Browse the repository at this point in the history
Co-authored-by: Luke Towers <[email protected]>
  • Loading branch information
jaxwilko and LukeTowers authored Jul 30, 2024
1 parent d232cb4 commit 6c88598
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 0 deletions.
2 changes: 2 additions & 0 deletions console/asset-compilation-mix.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ PackageManager::instance()->registerPackage(
);
```

<div id="automatic-configuration"></div>

## Automatic Mix configuration

The command `mix:create` will allow you to automatically generate a basic Mix config which you can then build on top of.
Expand Down
2 changes: 2 additions & 0 deletions console/asset-compilation-vite.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ PackageManager::instance()->registerPackage(
);
```

<div id="automatic-configuration"></div>

## Automatic Vite configuration

The command `vite:create` will allow you to automatically generate a basic Vite config which you can then build on top of.
Expand Down
73 changes: 73 additions & 0 deletions console/asset-compilation.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,79 @@ Winter CMS supports the following:

For more information on these compilers, see the links above.

### Supported Toolset (Bundles)

When using Vite or Mix, Winter can automatically configure your plugin/theme via the `vite:create` or `mix:create` commands. These commands allow you to set up all config files, stub files and register any required npm packages.

More information on the `[vite|mix]:create` classes can be found here: [Vite](asset-compilation-vite#automatic-configuration), [Mix](asset-compilation-mix#automatic-configuration).

The toolsets offered by these commands can be dynamically registered or modified.

```php
use System\Classes\Asset\BundleManager;

BundleManager::instance()->registerCallback(function (BundleManager $manager) {
// Register a new bundle named `winterjs`, this will tell Winter to add these packages when a plugin / theme is configured
$manager->registerBundle('winterjs', [
// Define the packages required for the bundle
'winterjs' => '^1.0.1',
'example-package' => '^6.1.2'
// Define compiler only requirements, these will only be installed if the listed compiler is in use
'vite' => [
'winterjs-vite-adapter' => '^0.1.0'
],
'mix' => [
'winterjs-mix-adapter' => '^0.6.3'
]
]);

// Register a setup handler, this will allow you to generate any files your bundle may require during the config generation
// The handler will be run in the context of the `mix|vite:create` command, so `$this` allows access to any methods available on that command and parent classes.
$manager->registerSetupHandler('winterjs', function (string $packagePath, string $packageType) {
$this->writeFile(
$packagePath . '/winterjs.config.js',
File::get(__DIR__ . '/path/to/fixtures/winterjs.config.example.js')
);
});

// Register a scaffold handler, this will allow you to modify config files generated by winter on the fly
// `contentType` can be: vite, mix, js, css.
// The handler will be run in the context of the `mix|vite:create` command, so `$this` allows access to any methods available on that command and parent classes.
$manager->registerScaffoldHandler('winterjs', function (string $contents, string $contentType) {
return match ($contentType) {
// Modify the `mix` contents with a heredoc append
'mix' => $contents . PHP_EOL . <<<JAVASCRIPT
console.log('winterjs installed');
JAVASCRIPT,
// Modify the `vite` contents with a simple append
'vite' => $contents . PHP_EOL . '// example',
// Modify the `css` contents with an append from a file
'css' => $content . PHP_EOL . File::get(__DIR__ . 'css/winterjs.css.fixture'),
// Replace the entire contents with the contents from a default fixture (getFixture() loads from system fixtures)
'js' => $this->getFixture('js/default.js.fixture'),
// Return the contents unmodified
default => $contents
};
});
});
```

Once a custom bundle has been registered, it will be accessible via the `[vite|mix]:create` command as an option flag. I.e. with the above:

```bash
php artisan vite:config Acme.Plugin --winterjs
```

The above can also be used to modify the versions of packages to install, e.g.

```php
BundleManager::instance()->registerBundle('tailwind', [
'tailwindcss' => 'dev-999.999.999',
'@tailwindcss/forms' => 'dev-999.999.999',
'@tailwindcss/typography' => 'dev-999.999.999',
]);
```

### Run a package script

```bash
Expand Down

0 comments on commit 6c88598

Please sign in to comment.