This is an example of using Grunt to run Browserify to create minified client-side Javascript that uses jQuery.
To use the example, checkout & cd into the repo, then:
npm install
gruntNow open index.html in a web browser, and look in the console.
| *~ | |
| bundle.js | |
| bundle.min.js | |
| node_modules |
| module.exports = function(grunt) { | |
| // Project configuration. | |
| grunt.initConfig({ | |
| // pkg: grunt.file.readJSON('package.json'), | |
| uglify: { | |
| options: { | |
| banner: '/*! Grunt Uglify <%= grunt.template.today("yyyy-mm-dd") %> */ ' | |
| }, | |
| build: { | |
| src: 'bundle.js', | |
| dest: 'bundle.min.js' | |
| } | |
| }, | |
| browserify: { | |
| build: { | |
| src: 'index.js', | |
| dest: 'bundle.js' | |
| } | |
| } | |
| }); | |
| // Load the plugin that provides the "uglify" task. | |
| grunt.loadNpmTasks('grunt-contrib-uglify'); | |
| grunt.loadNpmTasks('grunt-browserify'); | |
| // Default task(s). | |
| grunt.registerTask('default', [ | |
| 'browserify', | |
| 'uglify' | |
| ]); | |
| }; |
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <title>node / browserify example</title> | |
| </head> | |
| <body> | |
| <script src="./bundle.min.js"></script> | |
| </body> | |
| </html> |
| var $ = require("jquery"); | |
| $(document).ready(function(){ | |
| console.log("hello world"); | |
| }); |
| { | |
| "name": "browserifylearn", | |
| "version": "0.0.0", | |
| "description": "foo", | |
| "main": "index.js", | |
| "dependencies": { | |
| "jquery": "~1.11.1" | |
| }, | |
| "devDependencies": { | |
| "grunt": "~0.4.5", | |
| "grunt-browserify": "~3.2.0", | |
| "grunt-contrib-uglify": "~0.6.0" | |
| }, | |
| "scripts": { | |
| "test": "echo \"Error: no test specified\" && exit 1" | |
| }, | |
| "author": "Mike Chelen <michael.chelen@gmail.com> (https://github.com/mchelen/)", | |
| "license": "BSD-2-Clause" | |
| } |