Specify one normal task and one watching task with Grunt Browserify

574 views Asked by At

I use Watchify (via grunt-browserify) for fast compilation during local dev:

grunt.initConfig({
    pkg: grunt.file.readJSON('package.json'),
    browserify: {
        app: {
            src: './src/app.js',
            dest: 'build/js/app.js'
        },
        options: {
            // next two lines for watchify + watch instead of browserify
            watch: true,
            keepAlive: true,
            transform: ['node-lessify', 'node-underscorify'],
            debug: true,
            browserifyOptions: {
                debug:true // include source maps.  currently only available with browserify
            }
        }
    }
}

grunt.loadNpmTasks('grunt-browserify');

Running grunt browserify enters a while true loop that continuously watches for changes. When it comes to build a production-ready bundle, I want it to run through once and not watch.

I have tried this configuration but the browserify:dev task never watches:

grunt.initConfig({
    pkg: grunt.file.readJSON('package.json'),
    browserify: {
        dev: {
            src: './src/app.js',
            dest: 'build/js/app.js',
            // next two lines for watchify + watch instead of browserify
            watch: true,
            keepAlive: true
        },
        package: {
            src: './src/app.js',
            dest: 'build/js/app.js'
        },
        options: {
            transform: ['node-lessify', 'node-underscorify'],
            browserifyOptions: {
                debug:true // include source maps.  currently only available with browserify
            }
        }
    }
});

How can I specify one Browserify task that watches with Watchify, and one task that builds the whole package and exits?

1

There are 1 answers

0
sennett On BEST ANSWER

I had my configuration wrong. Note that keepAlive: false is in the options namespace of the browserify:package job:

grunt.initConfig({
    pkg: grunt.file.readJSON('package.json'),
    browserify: {
        dev: {
            src: './src/app.js',
            dest: 'build/js/app.js'
        },
        package: {
            src: './src/app.js',
            dest: 'build/js/app.js',
            options: {
                keepAlive: false
            }
        },
        options: {
            watch: true,
            keepAlive: true,
            transform: ['node-lessify', 'node-underscorify'],
            browserifyOptions: {
                debug:true // include source maps.  currently only available with browserify
            }
        }
    }
});