0.1.0 • Published 12 years ago
grunt-watchify v0.1.0
grunt-watchify
Grunt task for node-browserify.
Getting Started
This plugin requires Grunt ~0.4.0 and Node >=0.10.x.
Install this grunt plugin with:
npm install grunt-watchify --save-devThen add this line to your project's Gruntfile.js Gruntfile:
grunt.loadNpmTasks('grunt-watchify');Differences with grunt-browserify
- grunt-watchifywatches the dependencies like watchify and rebuilds the bundle, when one dependency is modified.
- grunt-watchifycaches the dependencies making the rebuild process very fast (useful for big projects).
- The configuration is different. You have the optionsof browserify pluskeepaliveandcallback.
- The instance of browserify is passed to callbackwhere you can use the api of browserify. Remember to return the instance.
- The keepaliveis usefull if usegrunt-watchifyalone. It works like in grunt-contrib-connect#keepalive.
watchify: {
  options: {
    // defaults options used in b.bundle(opts)
    detectGlobals: true,
    insertGlobals: false,
    ignoreMissing: false,
    debug: false,
    standalone: false,
    keepalive: false,
    callback: function(b) {
      // configure the browserify instance here
      b.add(...);
      b.require(...);
      b.external(...);
      b.ignore(...);
      b.transform(...);
      // return it
      return b;
    }
  }
  example: {
    src: './src/**/*.js',
    dest: 'app/js/bundle.js'
  }
}- The srcmakes difference betweenprocessand./process:
watchify: {
  example: {
    src: ['process', './src/**/*.js'],
    dest: 'app/js/bundle.js'
  },
}Your project files start usually with ./.
- You can use the glob only with your project files.
Examples
You find this example in the directory example.
- grunt-watchifybuilds the- bundle.jsand watches the dependencies.
- grunt-contrib-watchwatches the- bundle.jsand triggers- livereloadwhen it changes.
- grunt-contrib-connectserves the files.
- grunt-contrib-livereloadis used only for the livereload snippet.
/*
 * grunt-watchify
 * http://github.com/amiorin/grunt-watchify
 *
 * Copyright (c) 2013 Alberto Miorin, contributors
 * Licensed under the MIT license.
 */
'use strict';
var path = require('path');
var lrSnippet  = require('grunt-contrib-livereload/lib/utils').livereloadSnippet;
var mountFolder = function(connect, dir) {
  return connect.static(path.resolve(dir));
};
module.exports = function(grunt) {
  grunt.initConfig({
    watchify: {
      example: {
        src: './src/**/*.js',
        dest: 'app/js/bundle.js'
      },
    },
    watch: {
      app: {
        files: 'app/js/bundle.js',
        options: {
          livereload: true
        }
      }
    },
    connect: {
      options: {
        port: 9000,
        // Change this to '0.0.0.0' to access the server from outside.
        hostname: 'localhost'
      },
      livereload: {
        options: {
          middleware: function (connect) {
            return [
              lrSnippet,
              mountFolder(connect, 'app')
            ];
          }
        }
      }
    }
  });
  grunt.loadTasks('../tasks');
  grunt.loadNpmTasks('grunt-contrib-watch');
  grunt.loadNpmTasks('grunt-contrib-connect');
  grunt.loadNpmTasks('grunt-contrib-livereload');
  grunt.registerTask('default', ['watchify', 'connect', 'watch']);
};How can you start grunt:
# with other tasks like connect and watch
# done() is called
grunt
# alone like watchify
# done() is *never* called
grunt watchify:example:keepaliveCredits
- browserify ;-)
- watchify for the cache code
- grunt-browserify for the tests