With the JavaScript Bundling configuration active, Magento2 combine the JS into a series of 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
);
}
/**
* 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.
First of all autogeneration is happening based on class name suffix, e.g. Factory, ExtensionInterface (see \Magento\Framework\Api\Code\Generator\ExtensionAttributesInterfaceGenerator::EXTENSION_INTERFACE_SUFFIX) or Extension (see \Magento\Framework\Api\Code\Generator\ExtensionAttributesGenerator::EXTENSION_SUFFIX).
Proper generator is selected based on suffix here \Magento\Framework\Code\Generator::generateClass.
Let's assume Magento mode is developer and missing classes can be generated on fly (similar process will happen when compiler is used). When object manager tries to instantiate let's say Magento\Quote\Api\Data\CartItemExtensionFactory and it does not exist, the following happens:
Autoloader fails to instantiate class and initiates code generation here \Magento\Framework\Code\Generator\Autoloader::load
Then class suffix is determined as Factory (list of all declared suffixes can be found here \Magento\Framework\ObjectManager\DefinitionFactory::getCodeGenerator) and corresponding Factory generator class (Magento\Framework\ObjectManager\Code\Generator\Factory) is used to generate missing factory
All autogenerated classes are always based on another classes, in case of factory, source class name is calculated just by removing Factory suffix, it will be Magento\Quote\Api\Data\CartItemExtension. This class does not exist and autogeneration is invoked once again by autoloader, but this time for Extension class
Now suffix is Extension and \Magento\Framework\Api\Code\Generator\ExtensionAttributesGenerator will be used to generate this class
Source class for Extension class generation is calculated as Magento\Quote\Api\Data\CartItemInterface, it exists and Extension class is successfully generated. However, on the attempt to include Extension class file, autogeneration is triggered once again because Magento\Quote\Api\Data\CartItemExtension implements Magento\Quote\Api\Data\CartItemExtensionInterface, which does not exist
Suffix is ExtensionInterface and \Magento\Framework\Api\Code\Generator\ExtensionAttributesInterfaceGenerator will be used for generation
ExtensionInterface and Extension classes are generated based on information from extension_attributes.xml, accessible via \Magento\Framework\Api\ExtensionAttribute\Config, then Factory gets generated
One important note is that there is no preference for ExtensionInterface in di.xml because both Extension and ExtensionInterface are autogenerated. This is not a problem because ExtentionInterface is not expected to be injected via construct directly.
Best Answer
Go to
Admin -> Stores -> Configuration -> Advanced -> Developer -> Javascript Settings -> Enable Javascript Bundling
set toYes
After above configuration.
magento\vendor\magento\framework\View\Asset\Config.php
that flag is called heremagento\vendor\magento\module-require-js\Block\Html\Head\Config.php
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
magento\vendor\magento\framework\View\Asset\Bundle\Manager.php