Magento2 JavaScript Bundling – How to Generate

javascriptmagento2

With the JavaScript Bundling configuration active, Magento2 combine the JS into a series of bundles:

Magento2 JavaScript Bundles

This scripts don't seem generated by RequireJS, so how does Magento know which modules to include for each page and in what order?

Best Answer

Go to Admin -> Stores -> Configuration -> Advanced -> Developer -> Javascript Settings -> Enable Javascript Bundling set to Yes

After above configuration.

magento\vendor\magento\framework\View\Asset\Config.php that flag is called here

/**
 * Check whether bundling of JavScript files is on
 *
 * @return bool
 */
public function isBundlingJsFiles()
{
    return (bool)$this->scopeConfig->isSetFlag(
        self::XML_PATH_JS_BUNDLING,
        ScopeInterface::SCOPE_STORE
    );
}

magento\vendor\magento\module-require-js\Block\Html\Head\Config.php

/**
 * Include RequireJs configuration as an asset on the page
 *
 * @return $this
 */
protected function _prepareLayout()
{

    if ($this->bundleConfig->isBundlingJsFiles()) { // Check for that FLAG
    }
}

So whenever module or theme's JS called from requirejs-config.js. It will check this flag & if flag is set it will bundle (merge) JS for that Module/Theme respectively.

Can refer below files more detail

magento\vendor\magento\module-require-js\Model\FileManager.php

public function createBundleJsPool()
{
}

magento\vendor\magento\framework\View\Asset\Bundle\Manager.php