Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add single entrypoint shortcut for script and css methods (cake4) #15

Merged
merged 1 commit into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading