How to Exclude External Js from Minification, Bundling and Merge in Magento 2

Basically Js minification is a part of Optimization of Magento, When we enable the minification and bundling then all the js files get merged into single file and cached, that leads to faster loading of JS.
For more details please visit this link Optimise-Js-Files

Previously we used to create the after Plugin as below, to exclude the External JS.

<?php

namespace Blogento\ExcludeJs\Plugin;
use Magento\Framework\View\Asset\Minification;

class ExcludeFilesFromMinification
{
    public function afterGetExcludes(Minification $subject, array $result, $contentType)
    {
        if ($contentType == 'js') {
            $result[] = 'Blogento_ExcludeJs/js/your-js-filename';
        }
        return $result;
    }
}

But now it possible to define additional exclusions via a module’s etc/config.xml configuration file with the following syntax

...
<default>
    <dev>
        <js>
            <minify_exclude>
                <blogento_excludejs>/v1/blogento</blogento_excludejs>
            </minify_exclude>
        </js>
    </dev>
</default>

And Link JS through reguire js file in module’s view/frontend/requirejs-config.js :


var config = {
    shim: {
        connectJs: {
            exports: 'Blogento'
        }
    },
    paths: {
        connectJs: 'https://cdn.exmaple.com/v1/blogento'
    }
};

Now, connectJs component you can add in any js dependencies, where so ever places it’s presence required.

This is a simple trick to Exclude External Js. Happy Coding 🙂

2 thoughts on “How to Exclude External Js from Minification, Bundling and Merge in Magento 2”

  1. James R. Johnson

    Superb blog you have here but I was wanting to know if you knew
    of any discussion boards that cover the same topics discussed here?
    I’d really love to be a part of online community where I can get
    suggestions from other knowledgeable individuals
    that share the same interest. If you have any suggestions, please let me know.
    Bless you!

  2. Somebody necessarily help to make seriously articles I
    would state. This is the very first time I frequented
    your web page and thus far? I amazed with the analysis
    you made to make this actual submit amazing.
    Magnificent task!

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top