Magento, Magento 2

Magento 2 : Run setup:di:compile for specific module

Setup DI compile specific module in Magento 2

In this tutorial, Today I will explain to how to run setup di compile for specific module in Magento 2. There are many Magento Developers do compilations process so many times when do any changes with factory methods ,proxy, js changes or any code compilation. Sometimes, DI compilation process take too much time. So, I have found a solution to avoid this problem.

First of all, we should understand what is setup:di:compile process.

This below command is used for DI compile process :

php bin/magento setup:di:compile

Now, Let’s start process…

What is use of this command ?

This command generates the contents of the var/di folder in Magento < 2.2 and generated for Magento >= 2.2. Here, Looking at Magento’s official docs we can see that it compiles code such as Factories, Interceptors, Proxies etc and puts them in var/generation or generated directory.

Morever, This code compiler command do these below things when we run :

  • Application code generation (Factories, Proxies, Plugins and so on)
  • Area configuration aggregation (It will optimized dependency injection configurations per area)
  • Interceptor generation (It will optimized code generation of interceptors)
  • Interception cache generation
  • Repositories code generation (Generated code for APIs)
  • Service data attributes generation (It will generated extension classes for data objects)

These all actions needs to take time to complete process.

In other words, DI compile process is useful to factories, repositories code generation and optimize dependency injection.

Now, to do only specific module compile we need to follow some steps.

Steps of specific module DI compile :

1. First of all, Go to /var/www/html/yourMagento/setup/src/Magento/Setup/Console/Command/DiCompileCommand.php

2. Now, open this file and find this below line :

$modulePaths = $this->componentRegistrar->getPaths(ComponentRegistrar::MODULE);

3. Then, Replace this below line and add path of the module which you want to compile like this :

$modulePaths = ['CompanyName_ModuleName' => '/var/www/html/yourMagento/app/code/CompanyName/ModuleName'];

Note : Make sure module register with this CompanyName_ModuleName in registration.php file.

For example, You have created one custom module RH_Helloworld and you want to compile that specific RH_Helloworld module. Then, you can follow this tutorial to do the things. So, you can follow this tutorial to do single module compile with easy way.

It’s temporary solution for DI compile for specific moduel in Magento 2. Remove di generated module folder and refresh cache.

I hope this blog is easy to understand for you about how to setup di compile specific module in Magento 2. In case, I missed anything or need to add some information, always feel free to leave a comment in this blog, I’ll get back with proper solution 🙂

Tagged ,