-
-
Notifications
You must be signed in to change notification settings - Fork 17
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
All attributes starting with"@" are removed after minification. #12
Comments
fix : (#12) directives auto replacement
Hi @SaeedHeydari, thank you for reporting the issue. The problem is from this code. protected function loadDom(string $html, bool $force = false)
{
.....
static::$dom = new DOMDocument();
@static::$dom->loadHTML($html, LIBXML_HTML_NODEFDTD | LIBXML_SCHEMA_CREATE);
} Method loadHTML from class DOMDocument automatically clear unnecessary characters. See: https://www.php.net/manual/en/domdocument.loadhtml.php. There is no option for this case. I took the approach to replace the directives '@' with x-on. But, this solution may break on another JS framework. So, I added new config that devs can change what characters to change. Please update your laravel-minify dependency to After that, please add this line at the bottom in file /*
|--------------------------------------------------------------------------
| 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.
|
*/
'directives' => [
"@" => "x-on:",
], And clear the config cache, run: If you encounter any issue, please report it to me. Thanks! |
Thank you @fahlisaputra I changed the above code and it works. I think you should change code to this: Also there is a mistake in the README line below: |
I see another problem: I solved it by adding new config as: and code below in Minifier:
At first I replaced all words in This was my solution, maybe you have a better solution. |
Sorry, in the previous commit it turns out I made a mistake when testing this feature. Fixed soon! |
Hi, I added new function to process the custom directives 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) {
$body[1] = str_replace($search, $replace, $body[1]);
}
// unmask the directive that want to keep
foreach ($keepDirectives as $replace => $search) {
$body[1] = str_replace($search, $replace, $body[1]);
}
// rejoin the html
$html = $body[0]. '<body'. $body[1];
return $html;
} I was forced to break the html into two parts so as not to affect anything in the |
The following Alipine code:
<button @click="alert('test')">Test</button>
After minification becomes:
<button>Test</button>
This applies to all attributes starting with @
We have to use its equivalent to be able to have it:
<button x-on:click="alert('test')">Test</button>
"@" is shortened version of "x-on"
The text was updated successfully, but these errors were encountered: