Compiling code and deploying static view files in magento2

Compiling code and deploying static view files in magento2


Code compilation

To compile the code in magento2, we use setup:di:compile command which performs the following tasks :
  • Application code generation – for factories, proxies
  • Interceptor generation – improve code generation of interceptors
  • Interception cache generation – improve cache generation of interceptors
  • Area configuration aggregation – improve dependency injection configurations per area
  • Repositories code generation – create code for APIs
  • Service data attributes generation – create extension classes for data objects
Switch to the magento root folder (it is generally public_html) and run the above command -
      bin/magento setup:di:compile

The above command will show the output as below.
============================================
Compilation was started.
Plugin list generation... 9/9 [============================] 100% 46 secs 428.0 MiB
Generated code and dependency injection configuration successfully
============================================


Deploying static assets 

The deployment commands of static view files enable us to write static files to the Magento 2 file system. These static view files are located in Magento’s static directory (your Magento2 install dir/pub/static) and some files are cached in your Magento 2 install dir/var/view_preprocessed directory.

For deployment of static view files, follow these steps:

  • Log in to your Magento 2 server as, or switch to, the Magento 2 file system owner.
  • Delete the content under <your Magento install dir>/pub/static.
  • Run the static view files deployment tool <your Magento install dir>/bin/magento setup:static-content:deploy.
Use the following commands to deploy static view files :
  1. cd pub/static && find . -depth -name .htaccess -prune -o -delete && cd ../.. 
  2. rm -rf var/cache/* var/view_preprocessed/* generated/* pub/static/* var/page_cache/* var/composer_home/*
  3. php bin/magento setup:static-content:deploy -f
  4. chmod -R 777 pub/static
Flush the magento cache using the above commands to make the site load fresh content -
  1. bin/magento cache:clean
  2. bin/magento cache:flush