Skip to content

Instantly share code, notes, and snippets.

@mchelen
Forked from mchelen/ README.md
Last active August 29, 2015 14:09
Show Gist options
  • Save mchelen/f4dbcb130dd77971ce17 to your computer and use it in GitHub Desktop.
Save mchelen/f4dbcb130dd77971ce17 to your computer and use it in GitHub Desktop.

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
grunt

Now 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"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment