Skip to content

Commit

Permalink
Merge pull request #16 from edoardocavazza/cake5-entrypoint-shortcut
Browse files Browse the repository at this point in the history
Add single entrypoint shortcut for script and css methods (cake5)
  • Loading branch information
passchn authored Mar 8, 2024
2 parents 5d9ea4e + c561e0e commit 2f1d2d2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ In your php-template or in layout you can import javascript files with:
<?php $this->ViteScripts->script($options) ?>
```

or using this shourtcut for a single entrypoint:

```php
<?php $this->ViteScripts->script('webroot_src/main.ts') ?>
```

If you imported CSS files inside your JavaScript files, this method automatically
appends your css tags to the css view block.

Expand All @@ -76,6 +82,12 @@ In your php-template you can import css files with:
<?php $this->ViteScripts->css($options) ?>
```

or using this shourtcut for a single entrypoint:

```php
<?php $this->ViteScripts->script('webroot_src/style.css') ?>
```

## Configuration

The plugin comes with some default configuration. You may need to change it depending on your setup. Or you might not
Expand Down
15 changes: 10 additions & 5 deletions src/View/Helper/ViteScriptsHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,18 @@ public function isDev(ViteHelperConfig|string|null $config = null): bool
* * devEntries (string[]): entry files in development mode
* * other options are rendered as attributes to the html tag
*
* @param array $options see above
* @param array|string $options file entrypoint or script options
* @param \ViteHelper\Utilities\ViteHelperConfig|string|null $config config key or instance to use
* @return void
* @throws \ViteHelper\Exception\ConfigurationException
* @throws \ViteHelper\Exception\ManifestNotFoundException|\ViteHelper\Exception\InvalidArgumentException
*/
public function script(array $options = [], ViteHelperConfig|string|null $config = null): void
public function script(array|string $options = [], ViteHelperConfig|string|null $config = null): void
{
$config = $this->createConfig($config);
if (is_string($options)) {
$options = ['files' => [$options]];
}
$options['block'] = $options['block'] ?? $config->read('viewBlocks.script', ConfigDefaults::VIEW_BLOCK_SCRIPT);
$options['cssBlock'] = $options['cssBlock'] ?? $config->read('viewBlocks.css', ConfigDefaults::VIEW_BLOCK_CSS);
$options = $this->updateOptionsForFiltersAndEntries($options);
Expand Down Expand Up @@ -204,17 +207,19 @@ private function productionScript(array $options, ViteHelperConfig $config): voi
* * devEntries (string[]): entry files in development mode
* * other options are rendered as attributes to the html tag
*
* @param array $options see above
* @param array|string $options file entrypoint or css options
* @param \ViteHelper\Utilities\ViteHelperConfig|string|null $config config key or instance to use
* @return void
* @throws \ViteHelper\Exception\ManifestNotFoundException
* @throws \ViteHelper\Exception\ConfigurationException
* @throws \ViteHelper\Exception\InvalidArgumentException
*/
public function css(array $options = [], ViteHelperConfig|string|null $config = null): void
public function css(array|string $options = [], ViteHelperConfig|string|null $config = null): void
{
$config = $this->createConfig($config);

if (is_string($options)) {
$options = ['files' => [$options]];
}
// TODO the default should be css. This is a bug but might break in production.
// So this should be replaced in a major release.
$options['block'] = $options['block'] ?? $config->read('viewBlocks.css', ConfigDefaults::VIEW_BLOCK_SCRIPT);
Expand Down

0 comments on commit 2f1d2d2

Please sign in to comment.