2

I have enabled the production mode in my Magento 2.2.4 version, disabled the static sign functionality and added the parameter: urlArgs:"version='test'" in requirejs-config.js file. I have enabled the minify js functionality from the backend of Magento 2 . If i do not add this parameter all the dependency are loaded without any issues with the .min.js suffix, but once i added that parameter for cache busting functionality, all the dependency are loading with .js suffix and ignoring the .min.js files. I am unable to find the solution for this, can anyone please suggest me a solution for it?

  • Not getting your requirement. – Jimit Bhavsar Aug 2 '18 at 11:20
2

I have found a solution to this issue. I will say it is not a perfect solution, the below solution has solved my issue, so i am posting it here so that it can help others facing this issue.

Cause of the problem: The problem was that after enabling the production mode in my Magento 2 instance, disabling static version sign and enabling the js minification from backend requirejs was not loading the few .js files as min.js. So after long hours of debugging i found out that requirejs-min-resolver.min.js was the culprit. It was not loading few min.js files, the problem is with this line of code where the regular expression: url=url.replace(/(\.min)?\.js$/,'.min.js'); was not loading few .js files as min.js.

The fix: requirejs-min-resolver.min.js file is created from this php class, override this below method using di.xml:

vendor/magento/framework/RequireJs/Config.php

public function getMinResolverCode()
    {
        $excludes = [];
        foreach ($this->minification->getExcludes('js') as $expression) {
            $excludes[] = '!url.match(/' . str_replace('/', '\/', $expression) . '/)';
        }
        $excludesCode = empty($excludes) ? 'true' : implode('&&', $excludes);

        $result = <<<code
    var ctx = require.s.contexts._,
        origNameToUrl = ctx.nameToUrl;

    ctx.nameToUrl = function() {
        var url = origNameToUrl.apply(ctx, arguments);
        if ({$excludesCode}) {
            //url = url.replace(/(\.min)?\.js$/, '.min.js'); // commented this line of code.
           //added below line of code 
        if( (url.match('static/frontend/Vendor') || url.match('static/adminhtml/Magento')) && !url.match('.min.js') && !url.match('.json')){url=url.replace('.js','.min.js');} 
        }
        return url;
    };

code;

        if ($this->minification->isEnabled('js')) {
            $result = $this->minifyAdapter->minify($result);
        }
        return $result;
    }

Note: The expression that i have used has full filled my requirment, please change that according to your own requirement.

Solution impact area: The above expression will blindly start adding the .min.js extension to all the .js files that are loaded from our Magento 2 instance. Please take care that it does not start adding the .min.js extension to third party .js files even though i have taken care of it in the above custom expression.

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.