Skip to content

Commit 6794879

Browse files
committed
Gruntfile: Build jquery-ui.js using requirejs
- Bundle jquery-ui.js have UMD wrappers removed
1 parent 849c399 commit 6794879

File tree

4 files changed

+119
-24
lines changed

4 files changed

+119
-24
lines changed

.bundlejshintrc

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"boss": true,
3+
"curly": true,
4+
"eqeqeq": true,
5+
"eqnull": true,
6+
"expr": true,
7+
"immed": true,
8+
"noarg": true,
9+
"quotmark": "double",
10+
"smarttabs": true,
11+
"trailing": true,
12+
"undef": true,
13+
"unused": true,
14+
15+
"browser": true,
16+
"jquery": true,
17+
18+
"globals": {
19+
"define": false,
20+
"Globalize": false
21+
}
22+
}

Gruntfile.js

Lines changed: 94 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,13 @@ uiFiles.forEach(function( file ) {
124124

125125
// grunt plugins
126126
grunt.loadNpmTasks( "grunt-compare-size" );
127+
grunt.loadNpmTasks( "grunt-contrib-clean" );
127128
grunt.loadNpmTasks( "grunt-contrib-concat" );
128129
grunt.loadNpmTasks( "grunt-contrib-csslint" );
129130
grunt.loadNpmTasks( "grunt-contrib-cssmin" );
130131
grunt.loadNpmTasks( "grunt-contrib-jshint" );
131132
grunt.loadNpmTasks( "grunt-contrib-qunit" );
133+
grunt.loadNpmTasks( "grunt-contrib-requirejs" );
132134
grunt.loadNpmTasks( "grunt-contrib-uglify" );
133135
grunt.loadNpmTasks( "grunt-git-authors" );
134136
grunt.loadNpmTasks( "grunt-html" );
@@ -157,23 +159,6 @@ grunt.initConfig({
157159
},
158160
compare_size: compareFiles,
159161
concat: {
160-
ui: {
161-
options: {
162-
banner: createBanner( uiFiles ),
163-
stripBanners: {
164-
block: true
165-
}
166-
},
167-
src: uiFiles,
168-
dest: "dist/jquery-ui.js"
169-
},
170-
i18n: {
171-
options: {
172-
banner: createBanner( allI18nFiles )
173-
},
174-
src: allI18nFiles,
175-
dest: "dist/i18n/jquery-ui-i18n.js"
176-
},
177162
css: {
178163
options: {
179164
banner: createBanner( cssFiles ),
@@ -194,6 +179,11 @@ grunt.initConfig({
194179
})
195180
},
196181
copy: {
182+
dist_bundle_js: {
183+
src: "dist/build/jquery-ui.js",
184+
strip: /^dist\/build\//,
185+
dest: "dist/"
186+
},
197187
dist_units_images: {
198188
src: "themes/base/images/*",
199189
strip: /^themes\/base\//,
@@ -208,6 +198,14 @@ grunt.initConfig({
208198
})
209199
},
210200
jshint: {
201+
dist_bundle_js: {
202+
options: {
203+
jshintrc: ".bundlejshintrc"
204+
},
205+
files: {
206+
src: "dist/jquery-ui.js"
207+
}
208+
},
211209
ui: {
212210
options: {
213211
jshintrc: "ui/.jshintrc"
@@ -240,16 +238,92 @@ grunt.initConfig({
240238
csslintrc: ".csslintrc"
241239
}
242240
}
241+
},
242+
"pre-requirejs" : {
243+
all: {
244+
components: coreFiles.concat( uiFiles.map(function( file ) {
245+
return file.replace( /ui\//, "" );
246+
}) ),
247+
dest: "dist/tmp/main.js"
248+
}
249+
},
250+
requirejs: {
251+
all: {
252+
options: {
253+
dir: "dist/build",
254+
appDir: "ui",
255+
baseUrl: ".",
256+
optimize: "none",
257+
optimizeCss: "none",
258+
paths: {
259+
"jquery": "../jquery-1.9.1",
260+
"jqueryui": ".",
261+
"main" : "../dist/tmp/main" // FIXME replace to <%= %>
262+
},
263+
/* try use include: [] instead adding dist/tmp/main with require(...) */
264+
modules : [{
265+
name : "jquery-ui",
266+
include : [ "main" ],
267+
exclude: [ "jquery" ],
268+
create : true
269+
}],
270+
wrap: {
271+
start: createBanner() + "(function( $ ) {",
272+
end: "})( jQuery );"
273+
},
274+
onBuildWrite: function ( id, path, contents ) {
275+
if ( (/define\([\s\S]*?factory/).test( contents ) ) {
276+
// Remove UMD wrapper
277+
contents = contents.replace( /\(function\( factory[\s\S]*?\(function\( \$ \) \{/, "" );
278+
contents = contents.replace( /\}\)\);\s*?$/, "" );
279+
}
280+
else if ( (/^require\(\[/).test( contents ) ) {
281+
// Replace require with comment `//` instead of null string, because of the mysterious semicolon
282+
contents = contents.replace( /^require[\s\S]*?\]\);$/, "// mysterious semicolon: " );
283+
}
284+
285+
return contents;
286+
}
287+
}
288+
}
289+
},
290+
"post-requirejs": {
291+
all: [
292+
"dist/build/jquery-ui.js"
293+
]
294+
},
295+
clean: {
296+
dist_garbage: [ "dist/build", "dist/tmp" ]
243297
}
244298
});
245299

300+
grunt.registerMultiTask( "pre-requirejs", "Create require that will include appropriate components' dependencies", function() {
301+
if ( this.data.components.length ) {
302+
grunt.file.write( this.data.dest, "require([\n\t\"jqueryui/" + this.data.components.map(function( file ) {
303+
return file.replace( /\.js/, "" );
304+
}).join( "\",\n\t\"jqueryui/" ) + "\"\n]);" );
305+
}
306+
});
307+
308+
grunt.registerMultiTask('post-requirejs', "Strip define call from dist file", function() {
309+
this.filesSrc.forEach(function( filepath ) {
310+
// Remove `define("main" ...)` and `define("jquery-ui" ...)`
311+
var contents = grunt.file.read( filepath ).replace( /define\("(main|jquery-ui)", function\(\)\{\}\);/g, "" );
312+
313+
// Remove the mysterious semicolon `;` character left from require([...]);
314+
contents = contents.replace( /\/\/ mysterious semicolon.*/g, "" );
315+
316+
grunt.file.write( filepath, contents );
317+
});
318+
});
319+
246320
grunt.registerTask( "default", [ "lint", "test" ] );
247321
grunt.registerTask( "lint", [ "jshint", "csslint", "htmllint" ] );
248-
grunt.registerTask( "test", [ "qunit" ] );
322+
grunt.registerTask( "test", [ "copy:dist_units_images", "qunit" ] );
249323
grunt.registerTask( "sizer", [ "concat:ui", "uglify:main", "compare_size:all" ] );
250324
grunt.registerTask( "sizer_all", [ "concat:ui", "uglify", "compare_size" ] );
251325

252-
// "copy:dist_units_images" is used by unit tests
253-
grunt.registerTask( "build", [ "concat", "uglify", "cssmin", "copy:dist_units_images" ] );
326+
grunt.registerTask( "build", [ "clean", "pre-requirejs", "requirejs", "post-requirejs", "copy:dist_bundle_js", "clean:dist_garbage", "jshint:dist_bundle_js" ] );
327+
254328

255329
};

build/tasks/build.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,4 @@ grunt.registerMultiTask( "copy", "Copy files to destination folder and replace @
131131
}
132132
});
133133

134-
grunt.registerTask( "clean", function() {
135-
require( "rimraf" ).sync( "dist" );
136-
});
137-
138134
};

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,17 @@
5858
"devDependencies": {
5959
"grunt": "0.4.1",
6060
"grunt-compare-size": "0.3.1",
61+
"grunt-contrib-clean": "0.4.1",
6162
"grunt-contrib-concat": "0.1.3",
6263
"grunt-contrib-csslint": "0.1.1",
6364
"grunt-contrib-cssmin": "0.4.2",
6465
"grunt-contrib-jshint": "0.6.0",
6566
"grunt-contrib-qunit": "0.2.0",
67+
"grunt-contrib-requirejs": "0.4.1",
6668
"grunt-contrib-uglify": "0.1.1",
6769
"grunt-git-authors": "1.2.0",
6870
"grunt-html": "0.3.3",
71+
"requirejs": "2.1.8",
6972
"rimraf": "2.1.4",
7073
"testswarm": "1.1.0"
7174
},

0 commit comments

Comments
 (0)