Skip to content

Commit

Permalink
Merge pull request #290 from null-a/watchify
Browse files Browse the repository at this point in the history
Watchify grunt task
  • Loading branch information
stuhlmueller committed Jan 20, 2016
2 parents a905fb2 + a8c842f commit 60195d2
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 15 deletions.
50 changes: 36 additions & 14 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

var _ = require('underscore');
var open = require('open');
var execSync = require('child_process').execSync;
var child_process = require('child_process');

var jslintSettings = {
options: {
Expand Down Expand Up @@ -66,6 +66,17 @@ module.exports = function(grunt) {
clean: ['compiled/*.js']
});

function browserifyArgs(args) {
var pkgArg = '';
if (args.length > 0) {
var requires = _.chain(_.toArray(args))
.map(function(name) { return ['--require', name]; })
.flatten().value();
pkgArg = ' -t [' + ['./src/bundle.js'].concat(requires).join(' ') + ']';
}
return pkgArg + ' -g brfs src/browser.js -o compiled/webppl.js';
}

grunt.loadNpmTasks('grunt-gjslint');
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-contrib-nodeunit');
Expand All @@ -79,18 +90,28 @@ module.exports = function(grunt) {
grunt.registerTask('travis-phantomjs', ['compile', 'test-phantomjs']);

grunt.registerTask('compile', 'Compile for the browser', function() {
var pkgArg = '';
if (arguments.length > 0) {
var requires = _.chain(_.toArray(arguments))
.map(function(name) { return ['--require', '\"' + name + '\"']; })
.flatten().value();
pkgArg = ' -t [' + ['./src/bundle.js'].concat(requires).join(' ') + ']';
}
execSync('mkdir -p compiled');
grunt.log.writeln('Running browserify');
execSync('browserify' + pkgArg + ' -g brfs src/browser.js > compiled/webppl.js');
grunt.log.writeln('Running uglifyjs');
execSync('uglifyjs compiled/webppl.js -b ascii_only=true,beautify=false > compiled/webppl.min.js');
var taskArgs = (arguments.length > 0) ? ':' + _.toArray(arguments).join(':') : '';
grunt.task.run('browserify' + taskArgs, 'uglify');
});

grunt.registerTask('browserify', function() {
child_process.execSync('mkdir -p compiled');
child_process.execSync('browserify' + browserifyArgs(arguments));
});

grunt.registerTask('uglify', function() {
child_process.execSync('mkdir -p compiled');
child_process.execSync('uglifyjs compiled/webppl.js -b ascii_only=true,beautify=false > compiled/webppl.min.js');
});

grunt.registerTask('watchify', function() {
var done = this.async();
child_process.execSync('mkdir -p compiled');
var args = '-v' + browserifyArgs(arguments);
var p = child_process.spawn('watchify', args.split(' '));
p.stdout.on('data', grunt.log.writeln);
p.stderr.on('data', grunt.log.writeln);
p.on('close', done);
});

grunt.registerTask('test-browser', function() {
Expand All @@ -99,7 +120,8 @@ module.exports = function(grunt) {

grunt.registerTask('test-phantomjs', function() {
try {
var output = execSync('phantomjs node_modules/qunit-phantomjs-runner/runner-list.js tests/browser/index.html');
var cmd = 'phantomjs node_modules/qunit-phantomjs-runner/runner-list.js tests/browser/index.html';
var output = child_process.execSync(cmd);
grunt.log.writeln(output);
} catch (e) {
grunt.log.writeln(e.output.join('\n'));
Expand Down
30 changes: 29 additions & 1 deletion docs/development/workflow.rst
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,14 @@ To compile webppl for use in browser, run::
npm install -g browserify uglifyjs
grunt compile

Then, to run the browser tests use::
The compiled code is written to ``compiled/webppl.js`` and a minified
version is written to ``compiled/webppl.min.js``.

Testing
^^^^^^^

To check that compilation was successful, run the browser tests
using::

grunt test-browser

Expand All @@ -83,6 +90,26 @@ using the ``BROWSER`` environment variable. For example::

BROWSER="Google Chrome" grunt test-browser

Incremental Compilation
^^^^^^^^^^^^^^^^^^^^^^^

Repeatedly making changes to the code and then testing the changes in
the browser can be a slow process. `watchify`_ speeds up this process
by performing an incremental compile whenever it detects changes to
source files. To start `watchify`_ use::

npm install -g watchify
grunt watchify

Note that `watchify`_ only updates ``compiled/webppl.js``. Before
running the browser tests and deploying, create the minified version
like so::

grunt uglify

Packages
^^^^^^^^

Packages can also be used in the browser. For example, to include the
``webppl-viz`` package use::

Expand All @@ -92,3 +119,4 @@ Multiple packages can specified, separated by colons.

.. _continuous integration tests: https://travis-ci.org/probmods/webppl
.. _nodeunit documentation: https://github.com/caolan/nodeunit#command-line-options
.. _watchify: https://github.com/substack/watchify

0 comments on commit 60195d2

Please sign in to comment.