Permalink
Browse files

Build: Break down logic of grunt config

* Use separate jshint config for Gruntfile.js
* Move grunt tasks to separate modules
  • Loading branch information...
1 parent c6a651b commit cff78bceba7044647bd46990e33573ed2a872add @markelog markelog committed Dec 23, 2013
Showing with 281 additions and 165 deletions.
  1. +2 −13 .jshintrc
  2. +10 −150 Gruntfile.js
  3. +64 −0 karma.conf.js
  4. +10 −2 package.json
  5. +28 −0 src/.jshintrc
  6. +10 −0 tasks/commit.js
  7. +34 −0 tasks/compile.js
  8. +35 −0 tasks/dist.js
  9. +43 −0 tasks/release.js
  10. +9 −0 tasks/tag.js
  11. +36 −0 tasks/version.js
View
@@ -13,16 +13,5 @@
"undef": true,
"unused": true,
- "regexdash": true,
- "sub": true,
-
- "browser": true,
- "wsh": true,
-
- "globals": {
- "define": true,
- "module": true,
- "require": true,
- "jQuery": true
- }
-}
+ "node": true
+}
View
@@ -1,18 +1,11 @@
module.exports = function( grunt ) {
-
- // Load dev dependencies
- require( "load-grunt-tasks" )( grunt );
-
"use strict";
- var gzip = require("gzip-js"),
- exec = require("child_process").exec,
- fatal = grunt.fail.fatal,
- rpreversion = /(\d\.\d+\.\d+)-pre/;
+ var gzip = require( "gzip-js" );
// Project configuration.
grunt.initConfig({
- pkg: grunt.file.readJSON("package.json"),
+ pkg: grunt.file.readJSON( "package.json" ),
qunit: {
files: [ "test/index.html" ]
},
@@ -55,11 +48,11 @@ module.exports = function( grunt ) {
source: {
src: [ "src/sizzle.js" ],
options: {
- jshintrc: ".jshintrc"
+ jshintrc: "src/.jshintrc"
}
},
grunt: {
- src: [ "Gruntfile.js" ],
+ src: [ "Gruntfile.js", "tasks/*" ],
options: {
jshintrc: ".jshintrc"
}
@@ -97,149 +90,16 @@ module.exports = function( grunt ) {
tasks: "default"
}
});
+
+ // Integrate Sizzle specific tasks
+ grunt.loadTasks( "tasks" );
- grunt.registerMultiTask(
- "compile",
- "Compile sizzle.js to the dist directory. Embed date/version.",
- function() {
- var data = this.data,
- dest = data.dest,
- src = data.src,
- version = grunt.config("pkg.version"),
- compiled = grunt.file.read( src );
-
- // Embed version and date
- compiled = compiled
- .replace( /@VERSION/g, version )
- .replace( "@DATE", function () {
- var date = new Date();
-
- // YYYY-MM-DD
- return [
- date.getFullYear(),
- ( "0" + ( date.getMonth() + 1 ) ).slice( -2 ),
- ( "0" + date.getDate() ).slice( -2 )
- ].join( "-" );
- });
-
- // Write source to file
- grunt.file.write( dest, compiled );
-
- grunt.log.ok( "File written to " + dest );
- }
- );
-
- // Process files for distribution
- grunt.registerTask( "dist", function() {
- var files = grunt.file.expand( { filter: "isFile" }, "dist/*" ),
- fs = require("fs");
-
- files.forEach(function( filename ) {
- var map,
- text = fs.readFileSync( filename, "utf8" );
-
- // Modify map/min so that it points to files in the same folder;
- // see https://github.com/mishoo/UglifyJS2/issues/47
- if ( /\.map$/.test( filename ) ) {
- text = text.replace( /"dist\//g, "\"" );
- fs.writeFileSync( filename, text, "utf-8" );
- } else if ( /\.min\.js$/.test( filename ) ) {
- // Wrap sourceMap directive in multiline comments (#13274)
- text = text.replace( /\n?(\/\/@\s*sourceMappingURL=)(.*)/,
- function( _, directive, path ) {
- map = "\n" + directive + path.replace( /^dist\//, "" );
- return "";
- });
- if ( map ) {
- text = text.replace( /(^\/\*[\w\W]*?)\s*\*\/|$/,
- function( _, comment ) {
- return ( comment || "\n/*" ) + map + "\n*/";
- });
- }
- fs.writeFileSync( filename, text, "utf-8" );
- }
- });
- });
-
- // Commit and tag the specified version
- grunt.registerTask( "tag", function( version ) {
- exec( "git tag " + version, this.async() );
- });
-
- grunt.registerTask( "commit", function( message ) {
- // Always add dist directory
- exec( "git add dist && git commit -m " + message, this.async() );
- });
-
- // Commit a new version
- grunt.registerTask( "version", function( version ) {
- if ( !/\d\.\d+\.\d+(?:-pre)?/.test(version) ) {
- fatal( "Version must follow semver release format: " + version );
- return;
- }
-
- var done = this.async(),
- files = grunt.config( "version.files" ),
- rversion = /("version":\s*")[^"]+/;
-
- // Update version in specified files
- files.forEach(function( filename ) {
- var text = grunt.file.read( filename );
- text = text.replace( rversion, "$1" + version );
- grunt.file.write( filename, text );
- });
-
- // Add files to git index
- exec( "git add -A", function( err ) {
- if ( err ) {
- fatal( err );
- return;
- }
- // Commit next pre version
- grunt.config( "pkg.version", version );
- grunt.task.run([ "build", "uglify", "dist", "commit:'Update version to " + version + "'" ]);
- done();
- });
- });
-
- // Release a version of sizzle
- // Updates a pre version to released
- // Inserts `next` as the new pre version
- grunt.registerTask( "release", function( next ) {
- if ( !rpreversion.test(next) ) {
- fatal( "Next version should be a -pre version (x.x.x-pre): " + next );
- return;
- }
- var done,
- version = grunt.config( "pkg.version" );
- if ( !rpreversion.test(version) ) {
- fatal( "Existing version is not a pre version: " + version );
- return;
- }
- version = version.replace( rpreversion, "$1" );
-
- done = this.async();
- exec( "git diff --quiet HEAD", function( err ) {
- if ( err ) {
- fatal( "The working directory should be clean when releasing. Commit or stash changes." );
- return;
- }
- // Build to dist directories along with a map and tag the release
- grunt.task.run([
- // Commit new version
- "version:" + version,
- // Tag new version
- "tag:" + version,
- // Commit next version
- "version:" + next
- ]);
- done();
- });
- });
+ // Load dev dependencies
+ require( "load-grunt-tasks" )( grunt );
grunt.registerTask( "build", [ "jsonlint", "jshint", "compile", "uglify", "dist" ] );
grunt.registerTask( "default", [ "build", "qunit", "compare_size" ] );
// Task aliases
- grunt.registerTask( "lint", ["jshint"] );
+ grunt.registerTask( "lint", [ "jshint" ] );
};
View
@@ -0,0 +1,64 @@
+// Karma configuration
+module.exports = function(config) {
+ config.set({
+
+ // base path, that will be used to resolve files and exclude
+ basePath: "",
+
+
+ // frameworks to use
+ frameworks: [ "qunit" ],
+
+
+ // list of files / patterns to load in the browser
+ files: [
+ "test/data/testinit.js",
+ "test/jquery.js",
+ "dist/sizzle.js",
+ "test/unit/selector.js",
+ "test/unit/utilities.js",
+ "test/unit/extending.js",
+
+ {
+ pattern: "test/fixtures/fixture.html",
+ watched: true,
+ served: true,
+ included: false
+ }
+ ],
+
+ // test results reporter to use
+ // possible values: "dots", "progress", "junit", "growl", "coverage"
+ reporters: [ "dots" ],
+
+ // web server port
+ port: 9876,
+
+ // enable / disable colors in the output (reporters and logs)
+ colors: true,
+
+ // level of logging
+ // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
+ logLevel: config.LOG_INFO,
+
+ // enable / disable watching file and executing tests whenever any file changes
+ // autoWatch: true,
+
+ // Start these browsers, currently available:
+ // - Chrome
+ // - ChromeCanary
+ // - Firefox
+ // - Opera (has to be installed with `npm install karma-opera-launcher`)
+ // - Safari (only Mac; has to be installed with `npm install karma-safari-launcher`)
+ // - PhantomJS
+ // - IE (only Windows; has to be installed with `npm install karma-ie-launcher`)
+ browsers: [ "Chrome" ],
+
+ // If browser does not capture in given timeout [ms], kill it
+ captureTimeout: 60000,
+
+ // Continuous Integration mode
+ // if true, it capture browsers, run tests and exit
+ singleRun: false
+ });
+};
View
@@ -24,16 +24,24 @@
"dependencies": {},
"devDependencies": {
"grunt": "0.4.1",
- "grunt-compare-size": "~0.4.0-rc.3",
+
"grunt-contrib-jshint": "~0.7.1",
"grunt-contrib-qunit": "~0.3.0",
"grunt-contrib-uglify": "~0.2.7",
"grunt-contrib-watch": "~0.5.3",
+
+ "grunt-compare-size": "~0.4.0-rc.3",
"grunt-git-authors": "~1.2.0",
"grunt-jsonlint": "~1.0.2",
+
"gzip-js": "~0.3.2",
"load-grunt-tasks": "~0.2.0",
- "testswarm": "~1.1.0"
+ "testswarm": "~1.1.0",
+
+ "grunt-karma": "0.6.2",
+ "karma": "0.10",
+ "karma-qunit": "0.1.1",
+ "karma-browserstack-launcher": "0.0.7"
},
"keywords": [
"sizzle",
View
@@ -0,0 +1,28 @@
+{
+ "boss": true,
+ "curly": true,
+ "eqeqeq": true,
+ "eqnull": true,
+ "expr": true,
+ "immed": true,
+ "noarg": true,
+ "onevar": true,
+ "quotmark": "double",
+ "smarttabs": true,
+ "trailing": true,
+ "undef": true,
+ "unused": true,
+
+ "regexdash": true,
+ "sub": true,
+
+ "browser": true,
+ "wsh": true,
+
+ "globals": {
+ "define": true,
+ "module": true,
+ "require": true,
+ "jQuery": true
+ }
+}
View
@@ -0,0 +1,10 @@
+"use strict";
+
+var exec = require( "child_process" ).exec;
+
+module.exports = function( grunt ) {
+ grunt.registerTask( "commit", "Add and commit changes", function( message ) {
+ // Always add dist directory
+ exec( "git add dist && git commit -m " + message, this.async() );
+ });
+};
View
@@ -0,0 +1,34 @@
+"use strict";
+
+module.exports = function( grunt ) {
+ grunt.registerMultiTask(
+ "compile",
+ "Compile sizzle.js to the dist directory. Embed date/version.",
+ function() {
+ var data = this.data,
+ dest = data.dest,
+ src = data.src,
+ version = grunt.config( "pkg.version" ),
+ compiled = grunt.file.read( src );
+
+ // Embed version and date
+ compiled = compiled
+ .replace( /@VERSION/g, version )
+ .replace( "@DATE", function () {
+ var date = new Date();
+
+ // YYYY-MM-DD
+ return [
+ date.getFullYear(),
+ ( "0" + ( date.getMonth() + 1 ) ).slice( -2 ),
+ ( "0" + date.getDate() ).slice( -2 )
+ ].join( "-" );
+ });
+
+ // Write source to file
+ grunt.file.write( dest, compiled );
+
+ grunt.log.ok( "File written to " + dest );
+ }
+ );
+};
Oops, something went wrong.

0 comments on commit cff78bc

Please sign in to comment.