Skip to content

Commit

Permalink
Update to Grunt 0.4, and Uglify2 with Source Maps!
Browse files Browse the repository at this point in the history
  • Loading branch information
mikesherov committed Dec 19, 2012
1 parent 92001a3 commit cc97125
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 99 deletions.
174 changes: 79 additions & 95 deletions grunt.js → Gruntfile.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,22 @@
/**
* Resources
*
* https://gist.github.com/2489540
*/

module.exports = function( grunt ) {

"use strict";

// readOptionalJSON
// 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 = [
var distpaths = [
"dist/jquery.js",
"dist/jquery.min.js"
];
],
readOptionalJSON = function( filepath ) {
var data = {};
try {
data = grunt.file.readJSON( filepath );
} catch(e) {}
return data;
};

grunt.initConfig({
pkg: "<json:package.json>",
pkg: grunt.file.readJSON("package.json"),
dst: readOptionalJSON("dist/.destination.json"),
meta: {
banner: "/*! jQuery v<%= pkg.version %> jquery.com | jquery.org/license */"
},
compare_size: {
files: distpaths
},
Expand Down Expand Up @@ -74,69 +56,54 @@ module.exports = function( grunt ) {
"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() {
function jshintrc( path ) {
return readOptionalJSON( (path || "") + ".jshintrc" ) || {};
}

return {
grunt: {
options: jshintrc()
},
dist: {
options: jshintrc( "src/" )
},
tests: {
options: jshintrc( "test/" )
jshint: {
dist: {
src: [ "dist/jquery.js" ],
options: {
jshintrc: "src/.jshintrc"
}
};
})(),

qunit: {
files: "test/index.html"
},
grunt: {
src: [ "Gruntfile.js" ],
options: {
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: {
tests: "ajax attributes callbacks core css data deferred dimensions effects event manipulation offset queue selector serialize support traversing Sizzle".split(" ")
},

watch: {
files: [
"<config:lint.grunt>", "<config:lint.tests>",
"src/**/*.js"
],
files: [ "<%= jshint.grunt.src %>", "<%= jshint.tests.src %>", "src/**/*.js" ],
tasks: "dev"
},

uglify: {
codegen: {
ascii_only: true
all: {
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 ) {
var jobName,
testswarm = require( "testswarm" ),
Expand Down Expand Up @@ -179,8 +146,8 @@ module.exports = function( grunt ) {
var name = this.file.dest,
files = this.file.src,
sizzle = {
api: file.read( files[0] ),
src: file.read( files[1] )
api: grunt.file.read( files[0] ),
src: grunt.file.read( files[1] )
},
compiled, parts;

Expand Down Expand Up @@ -218,19 +185,18 @@ module.exports = function( grunt ) {
// Rejoin the pieces
compiled = parts.join("");


verbose.write("Injected sizzle-jquery.js into sizzle.js");
grunt.verbose.write("Injected sizzle-jquery.js into sizzle.js");

// Write concatenated source to file
file.write( name, compiled );
grunt.file.write( name, compiled );

// Fail task if errors were logged.
if ( this.errorCount ) {
return false;
}

// Otherwise, print a success message.
log.writeln( "File '" + name + "' created." );
grunt.log.writeln( "File '" + name + "' created." );
});


Expand All @@ -251,17 +217,17 @@ module.exports = function( grunt ) {

grunt.log.writeln( "Creating custom build...\n" );

grunt.utils.spawn({
grunt.util.spawn({
cmd: process.platform === "win32" ? "grunt.cmd" : "grunt",
args: [ "build:*:*:" + modules, "min" ]
args: [ "build:*:*:" + modules, "uglify" ]
}, function( err, result ) {
if ( err ) {
grunt.verbose.error();
done( err );
return;
}

grunt.log.writeln( result.replace("Done, without errors.", "") );
grunt.log.writeln( result.stdout.replace("Done, without errors.", "") );

done();
});
Expand All @@ -279,9 +245,10 @@ module.exports = function( grunt ) {
optIn = !modules["*"],
explicit = optIn || Object.keys(modules).length > 1,
name = this.file.dest,
src = this.file.srcRaw,
deps = {},
excluded = {},
version = config( "pkg.version" ),
version = grunt.config( "pkg.version" ),
excluder = function( flag, needsFlag ) {
// optIn defaults implicit behavior to weak exclusion
if ( optIn && !modules[ flag ] && !modules[ "+" + flag ] ) {
Expand Down Expand Up @@ -325,7 +292,7 @@ module.exports = function( grunt ) {
// *:*:-css all except css and dependents (explicit > implicit)
// *:*:-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)
this.file.src.forEach(function( filepath ) {
src.forEach(function( filepath ) {
var flag = filepath.flag;

if ( flag ) {
Expand All @@ -351,7 +318,7 @@ module.exports = function( grunt ) {


// conditionally concatenate source
this.file.src.forEach(function( filepath ) {
src.forEach(function( filepath ) {
var flag = filepath.flag,
specified = false,
omit = false,
Expand Down Expand Up @@ -389,7 +356,7 @@ module.exports = function( grunt ) {
}

if ( !omit ) {
compiled += file.read( filepath );
compiled += grunt.file.read( filepath );
}
});

Expand All @@ -408,15 +375,15 @@ module.exports = function( grunt ) {
});

// Write concatenated source to file
file.write( name, compiled );
grunt.file.write( name, compiled );

// Fail task if errors were logged.
if ( this.errorCount ) {
return false;
}

// Otherwise, print a success message.
log.writeln( "File '" + name + "' created." );
grunt.log.writeln( "File '" + name + "' created." );
});

// Allow custom dist file locations
Expand All @@ -425,7 +392,7 @@ module.exports = function( grunt ) {

// Check for stored destination paths
// ( set in dist/.destination.json )
stored = Object.keys( config("dst") );
stored = Object.keys( grunt.config("dst") );

// Allow command line input as well
flags = Object.keys( this.flags );
Expand All @@ -438,16 +405,17 @@ module.exports = function( grunt ) {
// Ensure the dist files are pure ASCII
var fs = require("fs"),
nonascii = false;

distpaths.forEach(function( filename ) {
var buf = fs.readFileSync( filename, "utf8" ),
i, c;
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++ ) {
c = buf.charCodeAt( i );
if ( c > 127 ) {
log.writeln( "- position " + i + ": " + c );
log.writeln( "-- " + buf.substring( i - 20, i + 20 ) );
grunt.log.writeln( "- position " + i + ": " + c );
grunt.log.writeln( "-- " + buf.substring( i - 20, i + 20 ) );
nonascii = true;
}
}
Expand All @@ -474,11 +442,27 @@ module.exports = function( grunt ) {

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" ] );
};
11 changes: 7 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,13 @@
],
"dependencies": {},
"devDependencies": {
"grunt-compare-size": "~0.2.0",
"grunt-git-authors": "=1.0.0",
"grunt-update-submodules": "=0.1.2",
"grunt": "~0.3.17",
"grunt-compare-size": "0.3.1",
"grunt-git-authors": "1.1.0-beta.1",
"grunt-update-submodules": "0.2.0",
"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"
},
"keywords": []
Expand Down

0 comments on commit cc97125

Please sign in to comment.