Skip to content

Commit

Permalink
fix: directives and keep directives feature
Browse files Browse the repository at this point in the history
fix: change preg_replace to str_replace
  • Loading branch information
fahlisaputra authored Mar 14, 2024
2 parents a53cda6 + 6870453 commit 59d9358
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 9 deletions.
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,23 @@ You can skip minify by route by adding the route name to the `ignore` array in t
```

### Custom Directives Replacement
You can replace custom directives by adding the directive name to the `custom_directives` array in the `config/minify.php` file. For example in AlpineJS you can write `@click="function()"`. Unfortunately, Minify for Laravel will remove the `@` symbol. You can replace it by adding `@ => x-click:` to the `directives` array. For example:
You can replace custom directives by adding the directive name to the `directives` array in the `config/minify.php` file. For example in AlpineJS you can write `@click="function()"`. Unfortunately, Minify for Laravel will remove the `@` symbol. You can replace it by adding `@ => x-on:` to the `directives` array. For example:

```php
"directives" => [
'@' => 'x-on:',
],
```

### Keep Directives
You can keep directives by adding the directive name to the `keep_directives` array in the `config/minify.php` file. For example when you use `@vite`, you can add `@vite` to the `keep_directives` array. For example:

```php
"keep_directives" => [
'@vite'
],
```

## Known Issues

- Minify for Laravel will remove the `@` symbol in the blade file. This will make the blade directive not working properly. You can fix this by adding `@ => x-on:` to the `directives` array in the `config/minify.php` file.
Expand All @@ -144,6 +153,10 @@ You can replace custom directives by adding the directive name to the `custom_di

If you find an issue, or have a better way to do something, feel free to open an issue, or a pull request. The package is far from perfect, and any help is welcome. There are no formal contribution guidelines, and there should be no contribution too small. All coding styles will be fixed during the pull request by StyleCI. So, don't worry too much about the code style. We'd love to hear from you!

## Thanks
Big thanks to the people who have contributed to this package:
- [@SaeedHeydari](https://github.com/SaeedHeydari)

## License
Laravel Minify is licensed under the [MIT license](LICENSE).

Expand Down
35 changes: 33 additions & 2 deletions config/minify.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,18 +101,49 @@
// "*/user"
],

/*
|--------------------------------------------------------------------------
| Enable Directive Replacement
|--------------------------------------------------------------------------
|
| Known issue: Minify for Laravel will replace all unnecessary characters
| in the HTML, including @.
|
| Here you can specify whether to enable directive replacement or not.
|
| Default: false
|
*/

'enable_directive_replacement' => false,

/*
|--------------------------------------------------------------------------
| Custom Directives Replacement
|--------------------------------------------------------------------------
|
| Here you can specify the directives that you want to replace. For example,
| you can replace the @ symbol with x-on: for AlpineJS with '@' => 'x-on:'.
| Minify use preg_replace to replace the directives.
| if you using AlpineJS with shorthand directive @click, you can replace it
| by adding '@' => 'x-on:' to the directives array.
|
*/

'directives' => [
'@' => 'x-on:',
],

/*
|--------------------------------------------------------------------------
| Keep Directives
|--------------------------------------------------------------------------
|
| Here you can specify the directives that you want to keep. For example,
| if you want to keep @vite directive, you can add '@vite' to the
| keep_directives array.
|
*/

'keep_directives' => [
'@vite',
],
];
39 changes: 33 additions & 6 deletions src/Middleware/Minifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,45 @@ public function handle(Request $request, Closure $next)

$html = $response->getContent();

$html = self::replaceDirectives($html);

$this->loadDom($html);

return $response->setContent($this->apply());
}

protected function replaceDirectives($html): string
{
if (!config('minify.enable_directive_replacement', false)) {
return $html;
}

// split the html into head and body
$body = explode('<body', $html);

// mask the directive that want to keep, solution by @SaeedHeydari #12
$keepDirectivesKeys = config('minify.keep_directives', []);
$keepDirectives = [];
foreach ($keepDirectivesKeys as $key) {
$keepDirectives[$key] = '____'.uniqid().'____';
$body[1] = str_replace($key, $keepDirectives[$key], $body[1]);
}

// replace custom directives, issue #12
$directives = config('minify.directives', []);
foreach ($directives as $search => $replace) {
// check if $search is a valid regex
if (@preg_match($search, null) !== false) {
$html = preg_replace($search, $replace, $html);
}
$body[1] = str_replace($search, $replace, $body[1]);
}

$this->loadDom($html);
// unmask the directive that want to keep
foreach ($keepDirectives as $replace => $search) {
$body[1] = str_replace($search, $replace, $body[1]);
}

return $response->setContent($this->apply());
// rejoin the html
$html = $body[0].'<body'.$body[1];

return $html;
}

protected function shouldProcessMinify($request, $response): bool
Expand Down

0 comments on commit 59d9358

Please sign in to comment.