7

I am trying to minify JS in Magento 2.
There is an option in the Backend: Stores -> Configuration -> Advanced -> Developer to minify js. I did that and JS got minified but it also includes my custom module's JS.

It should not include my custom module JS because it contains already minified JS. i want to exclude those JS from minification.

10

In your custom module, you'll want to add an interceptor to \Magento\Framework\View\Asset\Minification::getExcludes, which determines the files to exclude from minification.

Your files would look something like this:

di.xml

<?xml version="1.0" encoding="utf-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Framework\View\Asset\Minification">
        <plugin name="my-exclude" type="Vendor\Module\Plugin\ExcludeFilesFromMinification" />
    </type>
</config>

ExcludeFilesFromMinification.php (Supporting versions lower than 2.2)

<?php

namespace Vendor\Module\Plugin;
use Magento\Framework\View\Asset\Minification;

class ExcludeFilesFromMinification
{
    public function aroundGetExcludes(Minification $subject, callable $proceed, $contentType)
    {
        $result = $proceed($contentType);
        if ($contentType != 'js') {
            return $result;
        }
        $result[] = 'Vendor_Module/js/your-js-filename';
        return $result;
    }
}

ExcludeFilesFromMinification.php (Supporting 2.2+)

<?php

namespace Vendor\Module\Plugin;
use Magento\Framework\View\Asset\Minification;

class ExcludeFilesFromMinification
{
    public function afterGetExcludes(Minification $subject, array $result, $contentType)
    {
        if ($contentType == 'js') {
            $result[] = 'Vendor_Module/js/your-js-filename';
        }
        return $result;
    }
}
  • This answer is more conflict proof and should work in all cases – Milan Simek Oct 27 '17 at 17:04
1

This helped for a different issue but it might be useful here...

In your modules config.xml file add the following:

<default>
    <dev>
        <js>
            <minify_exclude>
                your-js-filename
            </minify_exclude>
        </js>
    </dev>
</default>
  • 3
    This will work but is a bad solution, as it changes the default setting (which is tiny_mce) – Navarr Oct 24 '17 at 19:17
  • @Navarr I don't think it does? If I'm correct the values of each minify_exclude xml node are retrieved as an array by \Magento\Framework\App\Config – Milan Simek Oct 24 '17 at 19:50
  • 2
    @milan it definitely uses scope config, and then just turns lines into an array. github.com/magento/magento2/blob/2.2-develop/lib/internal/… – Navarr Oct 24 '17 at 23:49
  • 1
    @Navarr I tested it and I can confirm that you're correct. The default tiny_mce value gets overwritten. When multiple modules use this method it will certainly cause conflicts. – Milan Simek Oct 27 '17 at 17:02
  • 1
    @Rob that might be the case indeed. My tests showed that Navarr is correct when he pointed out that an interceptor should be used instead of config.xml – Milan Simek May 18 '18 at 21:37
0

You need to update the value of 'dev/js/minify_exclude' on core_config_data to a list of ignored scripts (pipe separated).
Note that it's practically a regex pattern that Magento uses in order to exclude scripts from minify.
Therefore, it may look like that for example:

https://www.somedomain.com/somescript.js|https://www.another-url.com/anotherscript.js|https://www.blablabla.net/blablabla.js

After you do that, you need to flush all caches & pub/static files & re-deploy static-content.

*You may also check the file pub/static/frontend/{Vendor}/{Theme}/{Locale}/requirejs-min-resolver.min.js & see your list in the url.match(...)

0

The simplest solution is just to rename you files to end with *.min.js, Which is good practice, anyway. Magento will recognize the files as already minimized and skip them.

-1

Since Magento 2.3 it is now possible to define additional exclusions via a module's etc/config.xml with the following syntax:

<config>
    <default>
        <dev>
            <js>
                <minify_exclude>
                    <example1>/example1/</example1>
                </minify_exclude>
            </js>
            <css>
                <minify_exclude>
                    <example2>/example2/</example2>
                </minify_exclude>
            </css>
        </dev>
    </default>
</config>

All of these values will now be merged with, instead of overwriting, the previous value.

The pull request for this change can be seen here: https://github.com/magento/magento2/pull/13687

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.