The grunt-browserify is an NPM module to work with Browserify.It can generate browserified code that can be used by browsers.
In this demo, We will learn to create grunt task using grunt-browserify.
To demonstrate grunt-browserify module use we have created a project named GruntBrowserifyDemo. The project structure looks like following screenshot:-
We need to install grunt, grunt-browserify and grunt-contrib-concat NPM modules to create a Grunt task to use browserify.
The grunt-browserify module can be installed using npm install grunt-browserify –save-dev command in terminal.The following screenshot shows the grunt-browserify installation.
The grunt-contrib-concat module can be installed using npm install grunt-contrib-conca — save-dev command in terminal.The following screenshot shows the grunt-contrib-concat installation.
In this example we will use arithmeticsNPM module.This module provides a list of mathematical methods addition(), subtraction(),divide() etc.This module is basically for Node.js programming language.You can find more information on following URL:-https://www.npmjs.com/package/arithmetics
We will be using arithmetics module and browserify it to be used by my-script.js file in browser environment.Let install arithmetics NPM module using following command npm install arithmetics –save in terminal.The following screenshot shows the arithmetics installation.
The my-script.js file contains a method name doAddition() which imports arithmetics module using require statement.
We have created 2 grunt tasks name forArithmetic and concat.The forArithmetic task takes arithmetics module generates the browserified code in dist/arithmetic.browserified.js.The concat tasks then combines dist/arithmetic.browserified.js and dev/my-script.js to generate a combined file ad dist/bundle.js.
The demo.html file contains the HTML markup code with 3 input element number1, number2 and result.User can enter 2 numbers in number1 and number2 and press the calculate button. the calculate button calls the doAddition() method to get the summation of number1 and number2.The summation is then displayed in third input box.The content of demo.html file are as follows:-
<html><head><title>Grunt browserify demo</title><script src="dist/bundle.js"></script></head><body><input type="number"id="number1"placeholder="Enter a Number1"><br><br><input type="number"id="number2"placeholder="Enter a Number2"><br><br><input type="number"id="result"placeholder="Number1 + Number2" readonly><br><br><button onclick="doAddition()">Calculate</button></body></html>
We can run the grunt task using grunt command in terminal which will generate the dist/bundle.js file.The following screenshot shows the grunt command in execution.
On successful execution of grunt command the modified structure of GruntBrowserifyDemo looks like following screenshot.
The following screenshot shows the initial output of demo.html file in a browser.
Now we can enter two numbers 2 and 3 for number1 and number2 input box and press the calculate button.We can see the result 5 will be displayed in result input box.