Skip to content

Commit

Permalink
Merge pull request #15 from edoardocavazza/cake4-entrypoint-shortcut
Browse files Browse the repository at this point in the history
Add single entrypoiny shortcut for script and css methods (cake4)
  • Loading branch information
passchn authored Mar 8, 2024
2 parents 4b71f36 + 4ebcf7f commit 53cc288
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 @@ -54,6 +54,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 @@ -65,6 +71,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 need any config at all.
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 $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 string|array $options file entrypoint or script options
* @param \ViteHelper\Utilities\ViteHelperConfig|null $config config instance
* @return void
* @throws \ViteHelper\Exception\ConfigurationException
* @throws \ViteHelper\Exception\ManifestNotFoundException|\ViteHelper\Exception\InvalidArgumentException
*/
public function script(array $options = [], ?ViteHelperConfig $config = null): void
public function script(string|array $options = [], ?ViteHelperConfig $config = null): void
{
$config = $config ?: ViteHelperConfig::create();
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 @@ -181,17 +184,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 string|array $options file entrypoint or css options
* @param \ViteHelper\Utilities\ViteHelperConfig|null $config config instance
* @return void
* @throws \ViteHelper\Exception\ManifestNotFoundException
* @throws \ViteHelper\Exception\ConfigurationException
* @throws \ViteHelper\Exception\InvalidArgumentException
*/
public function css(array $options = [], ?ViteHelperConfig $config = null): void
public function css(string|array $options = [], ?ViteHelperConfig $config = null): void
{
$config = $config ?: ViteHelperConfig::create();

if (is_string($options)) {
$options = ['files' => [$options]];
}
$options['block'] = $options['block'] ?? $config->read('viewBlocks.css', ConfigDefaults::VIEW_BLOCK_SCRIPT);
$options = $this->updateOptionsForFiltersAndEntries($options);

Expand Down

0 comments on commit 53cc288

Please sign in to comment.