Skip to content

Commit 2f1d2d2

Browse files
authored
Merge pull request #16 from edoardocavazza/cake5-entrypoint-shortcut
Add single entrypoint shortcut for script and css methods (cake5)
2 parents 5d9ea4e + c561e0e commit 2f1d2d2

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ In your php-template or in layout you can import javascript files with:
6565
<?php $this->ViteScripts->script($options) ?>
6666
```
6767

68+
or using this shourtcut for a single entrypoint:
69+
70+
```php
71+
<?php $this->ViteScripts->script('webroot_src/main.ts') ?>
72+
```
73+
6874
If you imported CSS files inside your JavaScript files, this method automatically
6975
appends your css tags to the css view block.
7076

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

85+
or using this shourtcut for a single entrypoint:
86+
87+
```php
88+
<?php $this->ViteScripts->script('webroot_src/style.css') ?>
89+
```
90+
7991
## Configuration
8092

8193
The plugin comes with some default configuration. You may need to change it depending on your setup. Or you might not

src/View/Helper/ViteScriptsHelper.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,18 @@ public function isDev(ViteHelperConfig|string|null $config = null): bool
6868
* * devEntries (string[]): entry files in development mode
6969
* * other options are rendered as attributes to the html tag
7070
*
71-
* @param array $options see above
71+
* @param array|string $options file entrypoint or script options
7272
* @param \ViteHelper\Utilities\ViteHelperConfig|string|null $config config key or instance to use
7373
* @return void
7474
* @throws \ViteHelper\Exception\ConfigurationException
7575
* @throws \ViteHelper\Exception\ManifestNotFoundException|\ViteHelper\Exception\InvalidArgumentException
7676
*/
77-
public function script(array $options = [], ViteHelperConfig|string|null $config = null): void
77+
public function script(array|string $options = [], ViteHelperConfig|string|null $config = null): void
7878
{
7979
$config = $this->createConfig($config);
80+
if (is_string($options)) {
81+
$options = ['files' => [$options]];
82+
}
8083
$options['block'] = $options['block'] ?? $config->read('viewBlocks.script', ConfigDefaults::VIEW_BLOCK_SCRIPT);
8184
$options['cssBlock'] = $options['cssBlock'] ?? $config->read('viewBlocks.css', ConfigDefaults::VIEW_BLOCK_CSS);
8285
$options = $this->updateOptionsForFiltersAndEntries($options);
@@ -204,17 +207,19 @@ private function productionScript(array $options, ViteHelperConfig $config): voi
204207
* * devEntries (string[]): entry files in development mode
205208
* * other options are rendered as attributes to the html tag
206209
*
207-
* @param array $options see above
210+
* @param array|string $options file entrypoint or css options
208211
* @param \ViteHelper\Utilities\ViteHelperConfig|string|null $config config key or instance to use
209212
* @return void
210213
* @throws \ViteHelper\Exception\ManifestNotFoundException
211214
* @throws \ViteHelper\Exception\ConfigurationException
212215
* @throws \ViteHelper\Exception\InvalidArgumentException
213216
*/
214-
public function css(array $options = [], ViteHelperConfig|string|null $config = null): void
217+
public function css(array|string $options = [], ViteHelperConfig|string|null $config = null): void
215218
{
216219
$config = $this->createConfig($config);
217-
220+
if (is_string($options)) {
221+
$options = ['files' => [$options]];
222+
}
218223
// TODO the default should be css. This is a bug but might break in production.
219224
// So this should be replaced in a major release.
220225
$options['block'] = $options['block'] ?? $config->read('viewBlocks.css', ConfigDefaults::VIEW_BLOCK_SCRIPT);

0 commit comments

Comments
 (0)