forked from knockout/knockout
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Grunt build file. Update package.json to use Grunt.
- Loading branch information
Showing
3 changed files
with
130 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,4 +13,5 @@ perf/* | |
*.orig | ||
|
||
.DS_Store | ||
npm-debug.log | ||
npm-debug.log | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters