Commit cc971255 authored by Mike Sherov's avatar Mike Sherov

Update to Grunt 0.4, and Uglify2 with Source Maps!

parent 92001a38
/**
* Resources
*
* https://gist.github.com/2489540
*/
module.exports = function( grunt ) { module.exports = function( grunt ) {
"use strict"; "use strict";
// readOptionalJSON var distpaths = [
// by Ben Alman
// https://gist.github.com/2876125
function readOptionalJSON( filepath ) {
var data = {};
try {
data = grunt.file.readJSON( filepath );
grunt.verbose.write( "Reading " + filepath + "..." ).ok();
} catch(e) {}
return data;
}
var file = grunt.file,
log = grunt.log,
verbose = grunt.verbose,
config = grunt.config,
distpaths = [
"dist/jquery.js", "dist/jquery.js",
"dist/jquery.min.js" "dist/jquery.min.js"
]; ],
readOptionalJSON = function( filepath ) {
var data = {};
try {
data = grunt.file.readJSON( filepath );
} catch(e) {}
return data;
};
grunt.initConfig({ grunt.initConfig({
pkg: "<json:package.json>", pkg: grunt.file.readJSON("package.json"),
dst: readOptionalJSON("dist/.destination.json"), dst: readOptionalJSON("dist/.destination.json"),
meta: {
banner: "/*! jQuery v<%= pkg.version %> jquery.com | jquery.org/license */"
},
compare_size: { compare_size: {
files: distpaths files: distpaths
}, },
...@@ -74,37 +56,27 @@ module.exports = function( grunt ) { ...@@ -74,37 +56,27 @@ module.exports = function( grunt ) {
"src/outro.js" "src/outro.js"
] ]
}, },
min: {
"dist/jquery.min.js": [ "<banner>", "dist/jquery.js" ]
},
lint: {
dist: "dist/jquery.js",
grunt: "grunt.js",
// TODO: Once .jshintignore is supported, use that instead.
tests: ["test/data/{test,testinit,testrunner}.js", "test/unit/**/*.js"]
},
jshint: (function() { jshint: {
function jshintrc( path ) { dist: {
return readOptionalJSON( (path || "") + ".jshintrc" ) || {}; src: [ "dist/jquery.js" ],
} options: {
jshintrc: "src/.jshintrc"
return {
grunt: {
options: jshintrc()
},
dist: {
options: jshintrc( "src/" )
},
tests: {
options: jshintrc( "test/" )
} }
}; },
})(), grunt: {
src: [ "Gruntfile.js" ],
qunit: { options: {
files: "test/index.html" jshintrc: ".jshintrc"
}
},
tests: {
// TODO: Once .jshintignore is supported, use that instead.
src: [ "test/data/{test,testinit,testrunner}.js", "test/unit/**/*.js" ],
options: {
jshintrc: "test/.jshintrc"
}
}
}, },
testswarm: { testswarm: {
...@@ -112,31 +84,26 @@ module.exports = function( grunt ) { ...@@ -112,31 +84,26 @@ module.exports = function( grunt ) {
}, },
watch: { watch: {
files: [ files: [ "<%= jshint.grunt.src %>", "<%= jshint.tests.src %>", "src/**/*.js" ],
"<config:lint.grunt>", "<config:lint.tests>",
"src/**/*.js"
],
tasks: "dev" tasks: "dev"
}, },
uglify: { uglify: {
codegen: { all: {
ascii_only: true files: {
"dist/jquery.min.js": [ "dist/jquery.js" ]
},
options: {
banner: "/*! jQuery v<%= pkg.version %> jquery.com | jquery.org/license */",
sourceMap: "dist/jquery.source-map.js",
beautify: {
ascii_only: true
}
}
} }
} }
}); });
// Default grunt.
grunt.registerTask( "default", "update_submodules selector build:*:* lint min dist:* compare_size" );
// Short list as a high frequency watch task
grunt.registerTask( "dev", "selector build:*:* lint" );
// Load grunt tasks from NPM packages
grunt.loadNpmTasks( "grunt-compare-size" );
grunt.loadNpmTasks( "grunt-git-authors" );
grunt.loadNpmTasks( "grunt-update-submodules" );
grunt.registerTask( "testswarm", function( commit, configFile ) { grunt.registerTask( "testswarm", function( commit, configFile ) {
var jobName, var jobName,
testswarm = require( "testswarm" ), testswarm = require( "testswarm" ),
...@@ -179,8 +146,8 @@ module.exports = function( grunt ) { ...@@ -179,8 +146,8 @@ module.exports = function( grunt ) {
var name = this.file.dest, var name = this.file.dest,
files = this.file.src, files = this.file.src,
sizzle = { sizzle = {
api: file.read( files[0] ), api: grunt.file.read( files[0] ),
src: file.read( files[1] ) src: grunt.file.read( files[1] )
}, },
compiled, parts; compiled, parts;
...@@ -218,11 +185,10 @@ module.exports = function( grunt ) { ...@@ -218,11 +185,10 @@ module.exports = function( grunt ) {
// Rejoin the pieces // Rejoin the pieces
compiled = parts.join(""); compiled = parts.join("");
grunt.verbose.write("Injected sizzle-jquery.js into sizzle.js");
verbose.write("Injected sizzle-jquery.js into sizzle.js");
// Write concatenated source to file // Write concatenated source to file
file.write( name, compiled ); grunt.file.write( name, compiled );
// Fail task if errors were logged. // Fail task if errors were logged.
if ( this.errorCount ) { if ( this.errorCount ) {
...@@ -230,7 +196,7 @@ module.exports = function( grunt ) { ...@@ -230,7 +196,7 @@ module.exports = function( grunt ) {
} }
// Otherwise, print a success message. // Otherwise, print a success message.
log.writeln( "File '" + name + "' created." ); grunt.log.writeln( "File '" + name + "' created." );
}); });
...@@ -251,9 +217,9 @@ module.exports = function( grunt ) { ...@@ -251,9 +217,9 @@ module.exports = function( grunt ) {
grunt.log.writeln( "Creating custom build...\n" ); grunt.log.writeln( "Creating custom build...\n" );
grunt.utils.spawn({ grunt.util.spawn({
cmd: process.platform === "win32" ? "grunt.cmd" : "grunt", cmd: process.platform === "win32" ? "grunt.cmd" : "grunt",
args: [ "build:*:*:" + modules, "min" ] args: [ "build:*:*:" + modules, "uglify" ]
}, function( err, result ) { }, function( err, result ) {
if ( err ) { if ( err ) {
grunt.verbose.error(); grunt.verbose.error();
...@@ -261,7 +227,7 @@ module.exports = function( grunt ) { ...@@ -261,7 +227,7 @@ module.exports = function( grunt ) {
return; return;
} }
grunt.log.writeln( result.replace("Done, without errors.", "") ); grunt.log.writeln( result.stdout.replace("Done, without errors.", "") );
done(); done();
}); });
...@@ -279,9 +245,10 @@ module.exports = function( grunt ) { ...@@ -279,9 +245,10 @@ module.exports = function( grunt ) {
optIn = !modules["*"], optIn = !modules["*"],
explicit = optIn || Object.keys(modules).length > 1, explicit = optIn || Object.keys(modules).length > 1,
name = this.file.dest, name = this.file.dest,
src = this.file.srcRaw,
deps = {}, deps = {},
excluded = {}, excluded = {},
version = config( "pkg.version" ), version = grunt.config( "pkg.version" ),
excluder = function( flag, needsFlag ) { excluder = function( flag, needsFlag ) {
// optIn defaults implicit behavior to weak exclusion // optIn defaults implicit behavior to weak exclusion
if ( optIn && !modules[ flag ] && !modules[ "+" + flag ] ) { if ( optIn && !modules[ flag ] && !modules[ "+" + flag ] ) {
...@@ -325,7 +292,7 @@ module.exports = function( grunt ) { ...@@ -325,7 +292,7 @@ module.exports = function( grunt ) {
// *:*:-css all except css and dependents (explicit > implicit) // *:*:-css all except css and dependents (explicit > implicit)
// *:*:-css:+effects same (excludes effects because explicit include is trumped by explicit exclude of dependency) // *:*:-css:+effects same (excludes effects because explicit include is trumped by explicit exclude of dependency)
// *:+effects none except effects and its dependencies (explicit include trumps implicit exclude of dependency) // *:+effects none except effects and its dependencies (explicit include trumps implicit exclude of dependency)
this.file.src.forEach(function( filepath ) { src.forEach(function( filepath ) {
var flag = filepath.flag; var flag = filepath.flag;
if ( flag ) { if ( flag ) {
...@@ -351,7 +318,7 @@ module.exports = function( grunt ) { ...@@ -351,7 +318,7 @@ module.exports = function( grunt ) {
// conditionally concatenate source // conditionally concatenate source
this.file.src.forEach(function( filepath ) { src.forEach(function( filepath ) {
var flag = filepath.flag, var flag = filepath.flag,
specified = false, specified = false,
omit = false, omit = false,
...@@ -389,7 +356,7 @@ module.exports = function( grunt ) { ...@@ -389,7 +356,7 @@ module.exports = function( grunt ) {
} }
if ( !omit ) { if ( !omit ) {
compiled += file.read( filepath ); compiled += grunt.file.read( filepath );
} }
}); });
...@@ -408,7 +375,7 @@ module.exports = function( grunt ) { ...@@ -408,7 +375,7 @@ module.exports = function( grunt ) {
}); });
// Write concatenated source to file // Write concatenated source to file
file.write( name, compiled ); grunt.file.write( name, compiled );
// Fail task if errors were logged. // Fail task if errors were logged.
if ( this.errorCount ) { if ( this.errorCount ) {
...@@ -416,7 +383,7 @@ module.exports = function( grunt ) { ...@@ -416,7 +383,7 @@ module.exports = function( grunt ) {
} }
// Otherwise, print a success message. // Otherwise, print a success message.
log.writeln( "File '" + name + "' created." ); grunt.log.writeln( "File '" + name + "' created." );
}); });
// Allow custom dist file locations // Allow custom dist file locations
...@@ -425,7 +392,7 @@ module.exports = function( grunt ) { ...@@ -425,7 +392,7 @@ module.exports = function( grunt ) {
// Check for stored destination paths // Check for stored destination paths
// ( set in dist/.destination.json ) // ( set in dist/.destination.json )
stored = Object.keys( config("dst") ); stored = Object.keys( grunt.config("dst") );
// Allow command line input as well // Allow command line input as well
flags = Object.keys( this.flags ); flags = Object.keys( this.flags );
...@@ -438,16 +405,17 @@ module.exports = function( grunt ) { ...@@ -438,16 +405,17 @@ module.exports = function( grunt ) {
// Ensure the dist files are pure ASCII // Ensure the dist files are pure ASCII
var fs = require("fs"), var fs = require("fs"),
nonascii = false; nonascii = false;
distpaths.forEach(function( filename ) { distpaths.forEach(function( filename ) {
var buf = fs.readFileSync( filename, "utf8" ), var buf = fs.readFileSync( filename, "utf8" ),
i, c; i, c;
if ( buf.length !== Buffer.byteLength( buf, "utf8" ) ) { if ( buf.length !== Buffer.byteLength( buf, "utf8" ) ) {
log.writeln( filename + ": Non-ASCII characters detected:" ); grunt.log.writeln( filename + ": Non-ASCII characters detected:" );
for ( i = 0; i < buf.length; i++ ) { for ( i = 0; i < buf.length; i++ ) {
c = buf.charCodeAt( i ); c = buf.charCodeAt( i );
if ( c > 127 ) { if ( c > 127 ) {
log.writeln( "- position " + i + ": " + c ); grunt.log.writeln( "- position " + i + ": " + c );
log.writeln( "-- " + buf.substring( i - 20, i + 20 ) ); grunt.log.writeln( "-- " + buf.substring( i - 20, i + 20 ) );
nonascii = true; nonascii = true;
} }
} }
...@@ -474,11 +442,27 @@ module.exports = function( grunt ) { ...@@ -474,11 +442,27 @@ module.exports = function( grunt ) {
created = path + filename.replace( "dist/", "" ); created = path + filename.replace( "dist/", "" );
file.write( created, file.read(filename) ); grunt.file.write( created, grunt.file.read(filename) );
log.writeln( "File '" + created + "' created." ); grunt.log.writeln( "File '" + created + "' created." );
}); });
}); });
} }
}); });
// Load grunt tasks from NPM packages
grunt.loadNpmTasks( "grunt-compare-size" );
grunt.loadNpmTasks( "grunt-git-authors" );
grunt.loadNpmTasks( "grunt-update-submodules" );
// grunt contrib tasks
grunt.loadNpmTasks("grunt-contrib-watch");
grunt.loadNpmTasks("grunt-contrib-jshint");
grunt.loadNpmTasks("grunt-contrib-uglify");
// Default grunt
grunt.registerTask( "default", [ "update_submodules", "selector", "build:*:*", "jshint", "uglify", "dist:*", "compare_size" ] );
// Short list as a high frequency watch task
grunt.registerTask( "dev", [ "selector", "build:*:*", "jshint" ] );
}; };
...@@ -23,10 +23,13 @@ ...@@ -23,10 +23,13 @@
], ],
"dependencies": {}, "dependencies": {},
"devDependencies": { "devDependencies": {
"grunt-compare-size": "~0.2.0", "grunt-compare-size": "0.3.1",
"grunt-git-authors": "=1.0.0", "grunt-git-authors": "1.1.0-beta.1",
"grunt-update-submodules": "=0.1.2", "grunt-update-submodules": "0.2.0",
"grunt": "~0.3.17", "grunt-contrib-watch": "~0.1.4",
"grunt-contrib-jshint": "~0.1.0",
"grunt-contrib-uglify": "~0.1.0",
"grunt": "~0.4.0rc4",
"testswarm": "0.2.2" "testswarm": "0.2.2"
}, },
"keywords": [] "keywords": []
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment