Created
December 2, 2013 20:13
-
-
Save rxaviers/7757969 to your computer and use it in GitHub Desktop.
Gruntfile: Build jquery-ui.js using requirejs - Gruntfile: Build jquery-ui.js using requirejs. Bundle jquery-ui.js have UMD wrappers removed. Rename s/all/bundleJs or s/bundle/bundleJs when appropriate to avoid confusion with bundleI18n;
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
From 515f704f243bd38da9553c462bd180b6b4e10c26 Mon Sep 17 00:00:00 2001 | |
From: Rafael Xavier de Souza <rxaviers@gmail.com> | |
Date: Fri, 12 Jul 2013 15:21:28 -0300 | |
Subject: [PATCH] Gruntfile: Build jquery-ui.js using requirejs | |
- Gruntfile: Build jquery-ui.js using requirejs. Bundle jquery-ui.js have UMD | |
wrappers removed. Rename s/all/bundleJs or s/bundle/bundleJs when appropriate | |
to avoid confusion with bundleI18n; | |
--- | |
.bundlejshintrc | 22 +++++++ | |
Gruntfile.js | 175 +++++++++++++++++++++++++++++++++++++++++++-------- | |
build/tasks/build.js | 35 +++++++++++ | |
package.json | 15 +++-- | |
4 files changed, 214 insertions(+), 33 deletions(-) | |
create mode 100644 .bundlejshintrc | |
diff --git a/.bundlejshintrc b/.bundlejshintrc | |
new file mode 100644 | |
index 0000000..dad7ca1 | |
--- /dev/null | |
+++ b/.bundlejshintrc | |
@@ -0,0 +1,22 @@ | |
+{ | |
+ "boss": true, | |
+ "curly": true, | |
+ "eqeqeq": true, | |
+ "eqnull": true, | |
+ "expr": true, | |
+ "immed": true, | |
+ "noarg": true, | |
+ "quotmark": "double", | |
+ "smarttabs": true, | |
+ "trailing": true, | |
+ "undef": true, | |
+ "unused": true, | |
+ | |
+ "browser": true, | |
+ "jquery": true, | |
+ | |
+ "globals": { | |
+ "define": false, | |
+ "Globalize": false | |
+ } | |
+} | |
diff --git a/Gruntfile.js b/Gruntfile.js | |
index 9780c2f..c9ca16f 100644 | |
--- a/Gruntfile.js | |
+++ b/Gruntfile.js | |
@@ -50,7 +50,7 @@ var | |
options: { | |
preserveComments: false | |
}, | |
- main: { | |
+ bundleJs: { | |
options: { | |
banner: createBanner( uiFiles ) | |
}, | |
@@ -58,7 +58,7 @@ var | |
"dist/jquery-ui.min.js": "dist/jquery-ui.js" | |
} | |
}, | |
- i18n: { | |
+ bundleI18n: { | |
options: { | |
banner: createBanner( allI18nFiles ) | |
}, | |
@@ -68,6 +68,19 @@ var | |
} | |
}, | |
+ minifyCSS = { | |
+ options: { | |
+ keepSpecialComments: 0 | |
+ }, | |
+ bundle: { | |
+ options: { | |
+ keepSpecialComments: "*" | |
+ }, | |
+ src: "dist/jquery-ui.css", | |
+ dest: "dist/jquery-ui.min.css" | |
+ } | |
+ }, | |
+ | |
compareFiles = { | |
all: [ | |
"dist/jquery-ui.js", | |
@@ -101,15 +114,18 @@ uiFiles.forEach(function( file ) { | |
}); | |
// grunt plugins | |
-grunt.loadNpmTasks( "grunt-contrib-jshint" ); | |
-grunt.loadNpmTasks( "grunt-contrib-uglify" ); | |
+grunt.loadNpmTasks( "grunt-compare-size" ); | |
+grunt.loadNpmTasks( "grunt-contrib-clean" ); | |
grunt.loadNpmTasks( "grunt-contrib-concat" ); | |
-grunt.loadNpmTasks( "grunt-contrib-qunit" ); | |
grunt.loadNpmTasks( "grunt-contrib-csslint" ); | |
-grunt.loadNpmTasks( "grunt-jscs-checker" ); | |
+grunt.loadNpmTasks( "grunt-contrib-jshint" ); | |
+grunt.loadNpmTasks( "grunt-contrib-qunit" ); | |
+grunt.loadNpmTasks( "grunt-contrib-requirejs" ); | |
+grunt.loadNpmTasks( "grunt-contrib-uglify" ); | |
grunt.loadNpmTasks( "grunt-html" ); | |
-grunt.loadNpmTasks( "grunt-compare-size" ); | |
+grunt.loadNpmTasks( "grunt-jscs-checker" ); | |
grunt.loadNpmTasks( "grunt-git-authors" ); | |
+grunt.loadNpmTasks( "grunt-html" ); | |
// local testswarm and build tasks | |
grunt.loadTasks( "build/tasks" ); | |
@@ -135,23 +151,6 @@ grunt.initConfig({ | |
}, | |
compare_size: compareFiles, | |
concat: { | |
- ui: { | |
- options: { | |
- banner: createBanner( uiFiles ), | |
- stripBanners: { | |
- block: true | |
- } | |
- }, | |
- src: uiFiles, | |
- dest: "dist/jquery-ui.js" | |
- }, | |
- i18n: { | |
- options: { | |
- banner: createBanner( allI18nFiles ) | |
- }, | |
- src: allI18nFiles, | |
- dest: "dist/i18n/jquery-ui-i18n.js" | |
- }, | |
css: { | |
options: { | |
banner: createBanner( cssFiles ), | |
@@ -185,8 +184,37 @@ grunt.initConfig({ | |
}) | |
}, | |
jshint: { | |
- options: { | |
- jshintrc: true | |
+ bundleJs: { | |
+ options: { | |
+ jshintrc: ".bundlejshintrc" | |
+ }, | |
+ files: { | |
+ src: "dist/jquery-ui.js" | |
+ } | |
+ }, | |
+ bundleI18n: { | |
+ options: { | |
+ jshintrc: ".bundlejshintrc" | |
+ }, | |
+ files: { | |
+ src: "dist/i18n/jquery-ui-i18n.js" | |
+ } | |
+ }, | |
+ ui: { | |
+ options: { | |
+ jshintrc: "ui/.jshintrc" | |
+ }, | |
+ files: { | |
+ src: "ui/*.js" | |
+ } | |
+ }, | |
+ grunt: { | |
+ options: { | |
+ jshintrc: ".jshintrc" | |
+ }, | |
+ files: { | |
+ src: [ "Gruntfile.js", "build/**/*.js" ] | |
+ } | |
}, | |
all: [ | |
"ui/*.js", | |
@@ -202,13 +230,106 @@ grunt.initConfig({ | |
csslintrc: ".csslintrc" | |
} | |
} | |
+ }, | |
+ preRequirejs: { | |
+ bundle: { | |
+ components: uiFiles.map(function( file ) { | |
+ return file.replace( /ui\//, "" ); | |
+ }), | |
+ dest: "dist/tmp/main.js" | |
+ }, | |
+ bundleI18n: { | |
+ components: allI18nFiles.map(function( file ) { | |
+ return file.replace( /ui\//, "" ); | |
+ }), | |
+ dest: "dist/tmp/mainI18n.js" | |
+ } | |
+ }, | |
+ requirejs: { | |
+ options: { | |
+ dir: "dist/build", | |
+ appDir: "ui", | |
+ baseUrl: ".", | |
+ optimize: "none", | |
+ optimizeCss: "none", | |
+ paths: { | |
+ jquery: "../jquery-1.10.2", | |
+ jqueryui: ".", | |
+ tmp: "../dist/tmp" | |
+ }, | |
+ wrap: { | |
+ start: createBanner() + "(function( $ ) {", | |
+ end: "})( jQuery );" | |
+ }, | |
+ onBuildWrite: function ( id, path, contents ) { | |
+ if ( (/define\([\s\S]*?factory/).test( contents ) ) { | |
+ // Remove UMD wrapper | |
+ contents = contents.replace( /\(function\( factory[\s\S]*?\(function\( \$ \) \{/, "" ); | |
+ contents = contents.replace( /\}\)\);\s*?$/, "" ); | |
+ } | |
+ else if ( (/^require\(\[/).test( contents ) ) { | |
+ // Replace require with comment `//` instead of null string, because of the mysterious semicolon | |
+ contents = contents.replace( /^require[\s\S]*?\]\);$/, "// mysterious semicolon: " ); | |
+ } | |
+ return contents; | |
+ } | |
+ }, | |
+ bundle: { | |
+ options: { | |
+ modules: [{ | |
+ name: "../jquery-ui", | |
+ include: [ "tmp/main" ], | |
+ exclude: [ "jquery" ], | |
+ create: true | |
+ }] | |
+ } | |
+ }, | |
+ bundleI18n: { | |
+ options: { | |
+ modules: [{ | |
+ name: "../i18n/jquery-ui-i18n", | |
+ include: [ "tmp/mainI18n" ], | |
+ exclude: [ "jquery", "jqueryui/core", "jqueryui/datepicker" ], | |
+ create: true | |
+ }] | |
+ } | |
+ } | |
+ }, | |
+ postRequirejs: { | |
+ bundle: [ "dist/build/jquery-ui.js" ], | |
+ bundleI18n: [ "dist/build/jquery-ui-i18n.js" ] | |
+ }, | |
+ clean: { | |
+ dist: [ "dist" ], | |
+ distGarbage: [ "dist/build", "dist/tmp" ] | |
} | |
}); | |
+grunt.registerMultiTask( "preRequirejs", "Create require that will include appropriate components' dependencies", function() { | |
+ if ( this.data.components.length ) { | |
+ grunt.file.write( this.data.dest, "require([\n\t\"jqueryui/" + this.data.components.map(function( file ) { | |
+ return file.replace( /\.js/, "" ); | |
+ }).join( "\",\n\t\"jqueryui/" ) + "\"\n]);" ); | |
+ } | |
+}); | |
+ | |
+grunt.registerMultiTask( "postRequirejs", "Strip define call from dist file", function() { | |
+ this.filesSrc.forEach(function( filepath ) { | |
+ // Remove `define("main" ...)` and `define("jquery-ui" ...)` | |
+ var contents = grunt.file.read( filepath ).replace( /define\("(main|jquery-ui)", function\(\)\{\}\);/g, "" ); | |
+ | |
+ // Remove the mysterious semicolon `;` character left from require([...]); | |
+ contents = contents.replace( /\/\/ mysterious semicolon.*/g, "" ); | |
+ | |
+ grunt.file.write( filepath, contents ); | |
+ }); | |
+}); | |
+ | |
grunt.registerTask( "default", [ "lint", "test" ] ); | |
grunt.registerTask( "lint", [ "asciilint", "jshint", "jscs", "csslint", "htmllint" ] ); | |
grunt.registerTask( "test", [ "qunit" ] ); | |
-grunt.registerTask( "sizer", [ "concat:ui", "uglify:main", "compare_size:all" ] ); | |
+grunt.registerTask( "sizer", [ "concat:ui", "uglify:bundle", "compare_size:bundle" ] ); | |
grunt.registerTask( "sizer_all", [ "concat:ui", "uglify", "compare_size" ] ); | |
+grunt.registerTask( "build", [ "clean", "preRequirejs", "requirejs", "postRequirejs", "jshint:bundleJs", "uglify:bundleJs", "uglify:bundleI18n", "concat:css" ] ); | |
}; | |
diff --git a/build/tasks/build.js b/build/tasks/build.js | |
index cdc03bd..9899543 100644 | |
--- a/build/tasks/build.js | |
+++ b/build/tasks/build.js | |
@@ -85,6 +85,41 @@ grunt.registerTask( "clean", function() { | |
require( "rimraf" ).sync( "dist" ); | |
}); | |
+grunt.registerMultiTask( "copy", "Copy files to destination folder and replace @VERSION with pkg.version", function() { | |
+ function replaceVersion( source ) { | |
+ return source.replace( /@VERSION/g, grunt.config( "pkg.version" ) ); | |
+ } | |
+ function copyFile( src, dest ) { | |
+ if ( /(js|css)$/.test( src ) ) { | |
+ grunt.file.copy( src, dest, { | |
+ process: replaceVersion | |
+ }); | |
+ } else { | |
+ grunt.file.copy( src, dest ); | |
+ } | |
+ } | |
+ var files = expandFiles( this.filesSrc ), | |
+ target = this.data.dest + "/", | |
+ strip = this.data.strip, | |
+ renameCount = 0, | |
+ fileName; | |
+ if ( typeof strip === "string" ) { | |
+ strip = new RegExp( "^" + grunt.template.process( strip, grunt.config() ).replace( /[\-\[\]{}()*+?.,\\\^$|#\s]/g, "\\$&" ) ); | |
+ } | |
+ files.forEach(function( fileName ) { | |
+ var targetFile = strip ? fileName.replace( strip, "" ) : fileName; | |
+ copyFile( fileName, target + targetFile ); | |
+ }); | |
+ grunt.log.writeln( "Copied " + files.length + " files." ); | |
+ for ( fileName in this.data.renames ) { | |
+ renameCount += 1; | |
+ copyFile( fileName, target + grunt.template.process( this.data.renames[ fileName ], grunt.config() ) ); | |
+ } | |
+ if ( renameCount ) { | |
+ grunt.log.writeln( "Renamed " + renameCount + " files." ); | |
+ } | |
+}); | |
+ | |
grunt.registerTask( "asciilint", function() { | |
var valid = true, | |
files = grunt.file.expand({ filter: "isFile" }, "ui/*.js" ); | |
diff --git a/package.json b/package.json | |
index a802617..c216a8a 100644 | |
--- a/package.json | |
+++ b/package.json | |
@@ -57,15 +57,18 @@ | |
"dependencies": {}, | |
"devDependencies": { | |
"grunt": "0.4.1", | |
- "grunt-contrib-jshint": "0.7.1", | |
- "grunt-contrib-uglify": "0.1.1", | |
+ "grunt-compare-size": "0.4.0-rc.3", | |
+ "grunt-contrib-clean": "0.4.1", | |
"grunt-contrib-concat": "0.1.3", | |
- "grunt-contrib-qunit": "0.2.0", | |
"grunt-contrib-csslint": "0.1.1", | |
- "grunt-compare-size": "0.4.0-rc.3", | |
- "grunt-jscs-checker": "0.2.0", | |
- "grunt-html": "0.3.3", | |
+ "grunt-contrib-jshint": "0.7.1", | |
+ "grunt-contrib-qunit": "0.2.0", | |
+ "grunt-contrib-requirejs": "0.4.1", | |
+ "grunt-contrib-uglify": "0.1.1", | |
"grunt-git-authors": "1.2.0", | |
+ "grunt-html": "0.3.3", | |
+ "grunt-jscs-checker": "0.2.0", | |
+ "requirejs": "2.1.8", | |
"rimraf": "2.1.4", | |
"testswarm": "1.1.0" | |
}, | |
-- | |
1.8.1.2 | |
Author
rxaviers
commented
Dec 2, 2013
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment