TheCoachSMB

Install and Configure Grunt in Magento2

Install and Configure Grunt in Magento2

Grunt is a JavaScriptΒ task runnerΒ which essentially simplifies repetitive tasks by automating compilation, minification, linting, unit testing, etc.

In this article, we are going to understand how to install and configure Grunt for Magento2.

Here are the steps :

Step 1: Install nodejs in our machine
Step 2: Install Grunt CLI
Step 3: Configure In Magento2
Step 4:Β Grunt configuration file

Let’s start the process..

Step1: Install nodejs in our machine

Grunt and Grunt plugins are installed and managed viaΒ npm, theΒ Node.jsΒ package manager.

For Windows:-

Install nodejs – https://nodejs.org/en/

 

Click to download as shown above. Once download completes, Install it by double clicking on the downloaded file.

 

 

Then in the Windows Search box, type node and open it in “Run as administrator”

 

 

To check which version of Node.js you have installed after these initial steps, type in terminal/cmd :
node -v

For, Linux / Ubuntu:-

You can install it by following the mentioned link. For, Linux / Ubuntu user you can execute the below commands to installΒ Node.jsΒ :
sudo apt-get update
sudo apt-get install nodejs
To check which version of Node.js you have installed after these initial steps, type in terminal/cmd :
nodejs -v

Step 2: Install Grunt CLI

To install Grunt’s command line interface (CLI) globally. You may need to use sudo (for OSX, *nix, BSD etc) or run your command shell as Administrator (for Windows) to do this.

npm install -g grunt-cliΒ 

This will put theΒ gruntΒ command in your system path, allowing it to be run from any directory.

Step 3:Β Working with Magento project

To configure project with Grunt, Go to Magento 2 Root Directory to copy and paste the contents of the following sample files:

  • FromΒ package.json.sampleΒ toΒ package.json
  • FromΒ Gruntfile.js.sampleΒ toΒ Gruntfile.js
  • FromΒ grunt-config.json.sampleΒ intoΒ grunt-config.json

In a command prompt, run the following commands to refresh or install theΒ node.jsΒ project dependency, including Grunt:

cd your_Magento_instance_directory 
npm install 
npm update 

Step 4:Β Grunt configuration file

Copy the contents ofΒ themes.jsΒ intoΒ local-themes.jsΒ in theΒ dev/tools/grunt/configs/Β directory.

If installed as described above, Grunt will use the default configuration files located in theΒ dev/tools/grunt/configs/Β directory. You can define your theme in theΒ local-themes.jsΒ file. The following shows an example of how you can define your theme.

<theme>: {
      area: '<area>',
      name: '<Vendor>/<theme>,
      locale: '<language>',
      files: [
        '<path_to_file1>', //path to root source file
        '<path_to_file2>'
     ],
 dsl: 'less'
}

Where the following notation is used:

  • <Vendor>: vendor name.
  • <theme>: your theme code, conventionally should correspond to the theme directory name.
  • <area>: area code, can be eitherΒ frontendΒ orΒ adminhtml.
  • <language>: specified in theΒ code_subtagΒ format, for exampleΒ en_US. Only one locale can be specified here.
  • <path_to_file>: path to the root source file, relative to theΒ app/design/frontend/<Vendor>/<theme>/webΒ directory. You need to specify allΒ root source files of the theme. If your theme [inherits] from a certain theme, and does not contain its own root source files, specify the root source files of the parent theme.

Using custom Grunt configuration files

To use a custom file for Grunt configuration:

Open theΒ grunt-config.jsonΒ file in the Magento root and set configurations object as follows.

  • key: file alias
  • value: path to your custom file

Example: If your custom configuration fileΒ local-themes.jsΒ is located in theΒ <magento_root>/dev/tools/grunt/configsΒ directory, the following is already set in yourΒ grunt-config.json:

"themes": "dev/tools/grunt/configs/local-themes.js"

This path is also added to your .gitignore by default.

Once these are set correctly, run grunt to watch your changes.

  • RunΒ grunt exec:<theme>Β from the root directory to republish the symlinks.
  • RunΒ grunt watch:<theme>Β so that grunt will watch for file changes.

Lets take a example to perform these commands:-

Style Changes in Magento 2 Server-Side Compilation Mode via Grunt

  • Go to your theme folder,
  • Edit the _theme.less file with the following code

@navigation__background: #000;

save the file.

  • run below command in the root directory of Magento2
Β  Β  Β  Β  Β  Β  Β  Β  grunt exec: <your_theme>
Β  Β  Β  Β  Β  Β  Β  Β grunt less: <your_theme>
  • Β Refresh your browser and check the changes.

Uses of grunt watch command :-

Now we will understand the use of grunt watch command.
  • In root directory of Magento2, run the below command

grunt watch

 

So this command is watching for the changes in the less file. So lets make change in the _theme.less file. Lets change the background of the navigation….

  • Then Go to your theme folder,
  • Edit the _theme.less file with the following code

@navigation__background: @color-blue1;

save the file.

Now file will be executed and compiles automatically on the server.

  • Refresh the page once more to view the changes.

 

Magento 2 Grunt Commands

Use the Grunt commands described below to perform different customization tasks in your Magento 2 project. All commands should be run from the installation directory of Magento 2.

  • grunt clean:<theme>. This command removes static files related to your theme in bothΒ pub/staticΒ andΒ varΒ directories.
  • grunt exec:<theme>. This one republishes source files symlinks toΒ pub/static/frontend/<Vendor>/<theme>/<locale>.
  • grunt less:<theme>Β should be used to compile .css files using symlinks from the aforementioned directory.
  • grunt watch. This command is designed for the LiveReload extension. It tracks changes in the source files, recompiles .css files and reloads browser pages.
Sonal Motghare-Balpande

Hello Readers, My passion is to guide people by sharing the knowledge I have. If I can contribute even little to the people life, its very big achievement for me. Thank You, Sonal

Leave a Reply

Your email address will not be published. Required fields are marked *

πŸš€ Let’s Connect on LinkedIn! πŸš€

Want to level up your skills and knowledge? πŸš€
Follow us on LinkedIn to get:
βœ… Expert insights on business growth
βœ… Daily tips to sharpen your skills
βœ… Exclusive updates from The Coach SMB

Let's grow together!