Skip to content

Commit

Permalink
Add Grunt build file. Update package.json to use Grunt.
Browse files Browse the repository at this point in the history
  • Loading branch information
mbest committed Sep 11, 2013
1 parent bc1b7a3 commit 345ca51
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 3 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ perf/*
*.orig

.DS_Store
npm-debug.log
npm-debug.log
node_modules
119 changes: 119 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*global module:false*/
module.exports = function(grunt) {
var _ = grunt.util._;

// Project configuration
grunt.initConfig({
// Metadata
pkg: grunt.file.readJSON('package.json'),
fragments: './build/fragments/',
banner: '// Knockout JavaScript library v<%= pkg.version %>\n' +
'// (c) Steven Sanderson - <%= pkg.url %>\n' +
'// License: <%= pkg.licenses[0].type %> (<%= pkg.licenses[0].url %>)\n\n',
build: {
debug: './build/output/knockout-latest.debug.js',
min: './build/output/knockout-latest.js'
},
test: {
phantomjs: 'spec/runner.phantom.js',
node: 'spec/runner.node.js'
}
});

grunt.registerTask('clean', 'Clean up output files.', function (target) {
var output = grunt.config('build');
var files = [ output.debug, output.min ];
var options = { force: (target == 'force') };
_.forEach(files, function (file) {
if (grunt.file.exists(file))
grunt.file.delete(file, options);
});
return !this.errorCount;
});


var combinedSources;
function combineSources() {
var source = [];
var fragments = grunt.config('fragments');
function readFragment(fragment) {
source.push(grunt.file.read(fragments + fragment));
}
global.knockoutDebugCallback = function(sources) {
_.forEach(sources, function (file) {
source.push(grunt.file.read('./' + file));
});
};
readFragment('extern-pre.js');
readFragment('amd-pre.js');
require(fragments + 'source-references');
readFragment('amd-post.js');
readFragment('extern-post.js');
combinedSources = source.join('').replace('##VERSION##', grunt.config('pkg.version'));
}

function buildDebug(output) {
var source = [];
source.push(grunt.config('banner'));
source.push('(function(){\n');
source.push('var DEBUG=true;\n');
source.push(combinedSources);
source.push('})();\n');
grunt.file.write(output, source.join('').replace(/\r\n/g, '\n'));
}

function buildMin(output, done) {
var cc = require('closure-compiler');
var options = {
compilation_level: 'ADVANCED_OPTIMIZATIONS',
output_wrapper: '(function() {%output%})();'
};
grunt.log.write('Compiling...');
cc.compile('/**@const*/var DEBUG=false;' + combinedSources, options, function (err, stdout, stderr) {
if (err) {
grunt.error(err);
done(false);
} else {
grunt.log.ok();
grunt.file.write(output, (grunt.config('banner') + stdout).replace(/\r\n/g, '\n'));
done(true);
}
});
}

grunt.registerMultiTask('build', 'Build', function() {
if (!combinedSources)
combineSources();

if (!this.errorCount) {
var output = this.data;
if (this.target === 'debug') {
buildDebug(output);
} else if (this.target === 'min') {
buildMin(output, this.async());
}
}
return !this.errorCount;
});

grunt.registerMultiTask('test', 'Run tests', function () {
var done = this.async();
grunt.util.spawn({ cmd: this.target, args: [this.data] },
function (error, result, code) {
if (code === 127 /*not found*/) {
grunt.verbose.error(result.stderr);
// ignore this error
done(true);
} else {
grunt.log.writeln(result.stdout);
if (error)
grunt.log.error(result.stderr);
done(!error);
}
}
);
});

// Default task.
grunt.registerTask('default', ['clean', 'build', 'test']);
};
11 changes: 9 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"author": "The Knockout.js team",
"main": "build/output/knockout-latest.debug.js",
"scripts": {
"prepublish": "bash build/build.sh",
"prepublish": "grunt",
"test": "node spec/runner.node.js"
},
"repository": {
Expand All @@ -27,5 +27,12 @@
"iphone/6..latest",
"ipad/6..latest"
]
},
"licenses": [
{ "type": "MIT", "url": "http://www.opensource.org/licenses/mit-license.php" }
],
"devDependencies": {
"grunt": "~0.4.1",
"closure-compiler": "~0.2.1"
}
}
}

0 comments on commit 345ca51

Please sign in to comment.