Making changes during the development of your theme can take some time while you are waiting on removing the compiled files, recompiling, and reloading. To optimize the workflow and speed up the reloading of the changes, it is possible to use the built-in client-side Less compiler based on a JavaScript compilation. Using this client compiler might not work fast enough for you and require manual reloads. Another option is to use the Grunt tool to watch changes made in the .less
files. In this recipe, we will see how to make use of this tool and update CSS files without the manual removing of files and refreshing the browser.
Using Grunt should only be needed on your local development setup; for production systems, you should use the built-in Magento Less compiler. Grunt is built on Node.js; in order to use it, you should first install Node.js in your system.
This recipe will explain how to install, configure, and use Grunt to monitor file changes and recompile the CSS scripts. This recipe is based on the theme configured in the Creating a new theme recipe of this chapter. When you use it on your own theme, change the names/files according to your theme needs:
npm install –g grunt-cli
npm install grunt –-save-dev
npm install npm update
dev/tools/grunt/configs/themes.js
file. This file configures the paths used to monitor file changes and the files that should be created. Add the following code to the file:genmato: { area: 'frontend', name: 'Genmato/default', locale: 'en_US', files: [ 'css/styles-m', 'css/styles-l', 'css/sample' ], dsl: 'less' },
pub/static/frontend/Genmato/default
theme directory to the source theme files (located in app/design/frontend/Genmato/default
):grunt exec:genmato
The following output displays the processing done by Grunt:
themes.js
file in step 4:grunt less:genmato
This can be seen in the following screenshot:
grunt watch
This will detect the changes once you save a theme Less file and trigger the compilation of your Less code to a CSS file.
app/design/frontend/Genmato/default/web/css/source/_theme.less
@genmato__orange: #FCBF45; @navigation__background: @genmato__orange;
After saving the file, the compilation of the CSS is triggered and a new CSS file is stored for your theme:
The Grunt watch command looks for changes made to the Less files in the template directory. When a change is detected, it will trigger the recompilation of the CSS files of the theme. To see what files Grunt is watching, you can run the watch command with the –v
option:
grunt watch –v
This will display all modules loaded and give you more debugging information for which file has been changed and the actions that it is running.
When using the Grunt method and deploy feature in Magento in the same setup, make sure that you run the command starting from step 5 again; otherwise, the file changes will not be detected.
When using the LiveReload plugin, it will create a web socket connection to the LiveReload service running on TCP port 35729. Through this socket, Grunt will notify when the compilation of the CSS file is completed and what file has changed so that the LiveReload plugin can reload that file and apply it in the browser:
18.116.80.45