In this post, I’ll walk you through how to exclude specific JS files from minify in Magento 2. There are plenty of moments when you need to load third-party JavaScript in a custom module or theme, but the provider doesn’t ship a proper .min file. That gap can slow you down, so tightening this setup keeps your workflow steady and helps you move with confidence.
Use Case
- You are implementing a custom payment method module for the current project:
- Here, you found that the payment gateway is providing minified JavaScript file(s), but the file(s) name doesn’t have a .min suffix like payment-abc.min.js
- At this point, you have a tight budget and ETA. You can’t just tell the client that, or the payment gateway refused your request.
How do minified JS files work in Magento 2?
At first, you have to enable minify JavaScript from the Admin or using the command line in production mode.
\> php bin/magento config:set dev/js/minify_files 1
Disable JS merge if enable:
\> php bin/magento config:set dev/js/merge_files 0
\> php bin/magento cache:flushThen run deploy command:
\> php bin/magento setup:static-content:deployNow open the Magento 2 website on the browser and open DevTools, in Network panel it will show the minify js loader file: requirejs-min-resolver.min.js. This file is responsible to open all js files in .min and contains RegEx rules to exclude js files from adding .min suffix. Magento creates this file when JS minify & production mode is enabled.
Let’s do it
Using the above test case:
- You are implementing a custom payment method module for the current project but JavaScript files of the payment gateway do not have .min suffix and you will tell Magento 2 to add file path in the requirejs-min-resolver.min.js file.
- We will add an after plugin on the getExcludes method of Magento\Framework\View\Asset\Minification class. So you got an idea this class is responsible for minifying files in Magento 2.
In your di.xml file at app/code/Vendor/Payment/etc directory:
<type name="Magento\Framework\View\Asset\Minification">
<plugin name="vendor_prevent_minification" type="Vendor\PaymentModule\Plugin\View\MinificationPlugin" />
</type>Then create a plugin file: MinificationPlugin.php at app/code/Vendor/Payment/Plugin/View directory:
<?php
namespace Vendor\PaymentModule\Plugin\View;
use \Magento\Framework\View\Asset\Minification;
class MinificationPlugin
{
/**
* Exclude static componentry files from being minified.
*
* Using the config node `minify_exclude` is not an option because it does
* not get merged but overridden by subsequent modules.
*
* @see \Magento\Framework\View\Asset\Minification::XML_PATH_MINIFICATION_EXCLUDES
*
* @param Minification $subject
* @param string[] $excludes
* @param string $contentType
* @return string[]
*/
public function afterGetExcludes(Minification $subject, array $excludes, $contentType)
{
// Here we are checking if not “js” then skip it
if ($contentType !== 'js') {
return $excludes; // Skip if contentType is not “js”
}
// Array variable adds path to exclude minify
$excludes[]= 'https://webservices.paymentgateway.net/';
return $excludes;
}
}Now change the Vendor/PaymentModule name as per your module and then run below commands:
\> php bin/magento cache:flush
\> php bin/magento setup:di:compile
\> php bin/magento setup:static-content:deployIn the browser, now check the payment JS will open properly because the file path is added in requirejs-min-resolver.min.js file.
Conclusion
Being able to exclude specific JS files from minify is a built-in Magento 2 win, especially when you’re dealing with third-party scripts for payments, shipping, ReCaptcha, or email tools. It keeps your stack clean and your rollout steady, which is exactly what devs need when timelines get tight.
Let me know if you found the article helpful so that I will be encouraged to write new blogs.
Thank you.
Post a Comment
Got a question? Have a feedback? Please feel free to leave your ideas, opinions, and questions in the comments section of our post! ❤️