Minify js scripts via php

is it possible to somehow minify the javascript that I connect to the block?

<?php
$this->append('script');
?>
<script>
    $('.js-add-to-cart').on('click', function(e) {
        e.preventDefault();
        ......
    });
</script>
<?php
$this->end();
?>

You can use something like GitHub - tedious/JShrink: Javascript Minifier built in PHP and output the JS minified like that.

But rather than doing that via PHP (and therefore on every request) I would rather recommend you look into something like Installation | Laravel Mix Documentation

It is very easy to setup locally and you can use modern SCSS and JS (ES6+) to generate minified versions of your CSS and JS.

Then you only have to embed those minified files.

You can also look at GitHub - ishanvyas22/asset-mix: Provides helpers functions for CakePHP to use Laravel Mix.

1 Like

Static Files like JS and CSS (which don’t change regularely) should be cachable so the browser doesn’t have to load them on every request. Thats also a reason why to not embed JS directly in your HTML.

1 Like

Thank you! I try it.