11

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.

6 Answers 6

14

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:

MODULE/etc/di.xml (Be careful! Don't put it in etc/frontend/di.xml, it has to be etc/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>

MODULE/Plugin/ExcludeFilesFromMinification.php

Magento < 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;
    }
}

Magento > 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;
    }
}
1
  • This answer is more conflict proof and should work in all cases Commented Oct 27, 2017 at 17:04
5

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

1
  • 2
    Can you clarify what value needs to go in place of /example1/? is that a directory or file path? I haven't been able to find documentation on this yet. Commented Aug 5, 2020 at 18:03
1

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(...)

1

For exclude js file from the minify, Add below code in the config.xml file of you extension.

Open or create below file in your custom module.

EDIT: app/code/Vendor/Module/etc/config.xml

<dev>
    <js>
       <minify_exclude>
          <exclude_name>Vendor_Module/js/filename.js</exclude_name>
        </minify_exclude>
     </js>
</dev>

exclude_name :- You can change name here whatever you want.

File Path:- Pass file path as per the above mentioned.

Create: app/code/Vendor/Module/etc/config.xml

<?xml version="1.0"?>
<!--
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
    <default>
        <dev>
            <js>
               <minify_exclude>
                  <exclude_name>Vendor_Module/js/filename.js</exclude_name>
                </minify_exclude>
             </js>
        </dev>
    </default>
</config>

Run the below commands.

php bin/magento c:c

php bin/magento c:f

php bin/magento s:up

php bin/magento s:d:c

php bin/magento s:s:d -f

Thanks.

0

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>
7
  • 4
    This will work but is a bad solution, as it changes the default setting (which is tiny_mce)
    – Navarr
    Commented Oct 24, 2017 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 Commented Oct 24, 2017 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
    Commented Oct 24, 2017 at 23:49
  • 2
    @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. Commented Oct 27, 2017 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 Commented May 18, 2018 at 21:37
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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