efcdde5f by Adam Heath

Requirejs.

1 parent 3e9e2242
...@@ -7,6 +7,43 @@ ...@@ -7,6 +7,43 @@
7 7
8 module.exports = function (grunt) { 8 module.exports = function (grunt) {
9 var _ = require('lodash'); 9 var _ = require('lodash');
10
11 grunt.registerTask('revconfig', function revconfig() {
12 var prefix = grunt.template.process('dist/scripts/');
13 var pattern = prefix + '**/*.{js,html}';
14 var files = grunt.file.expand(pattern);
15 var lines = [
16 '(function(container) {',
17 ];
18 _.each(files, function(file) {
19 file = file.substring(prefix.length);
20 var res = file.match(/^(.*\/)?([0-9a-f]+)\.([^\/]+)\.([^\.]+)$/);
21 if (!res) {
22 return;
23 }
24 //grunt.log.oklns(JSON.stringify(res));
25 var dir = res[1] || '';
26 //var hash = res[2];
27 var base = res[3];
28 var ext = res[4];
29 var id;
30 if (ext === 'js') {
31 id = dir + base;
32 file = file.substring(0, file.length - ext.length - 1);
33 } else if (ext === 'html') {
34 id = 'text!' + dir + base + '.' + ext;
35 }
36 grunt.log.oklns('map: ' + id + ' -> ' + file);
37 lines.push('container[' + JSON.stringify(id) + ']=' + JSON.stringify(file) + ';\n');
38 });
39 lines.push('})(require.paths);');
40 if (false && lines.length == 2) {
41 lines = [];
42 }
43 grunt.file.write('.grunt/holygrail/scripts/revconfig.js', lines.join(''));
44 });
45
46
10 function createConfig(mpConfig) { 47 function createConfig(mpConfig) {
11 48
12 var holyGrailTmp = '.grunt/holygrail'; 49 var holyGrailTmp = '.grunt/holygrail';
...@@ -23,6 +60,7 @@ module.exports = function (grunt) { ...@@ -23,6 +60,7 @@ module.exports = function (grunt) {
23 }] 60 }]
24 }, 61 },
25 }, 62 },
63 concat: {},
26 copy: { 64 copy: {
27 dist: { 65 dist: {
28 files: [{ 66 files: [{
...@@ -40,7 +78,7 @@ module.exports = function (grunt) { ...@@ -40,7 +78,7 @@ module.exports = function (grunt) {
40 }, 78 },
41 }, 79 },
42 }; 80 };
43 if (common.useRev) { 81 if (mpConfig.useRev) {
44 initConfig.rev = { 82 initConfig.rev = {
45 dist: { 83 dist: {
46 files: [], 84 files: [],
...@@ -118,7 +156,7 @@ module.exports = function (grunt) { ...@@ -118,7 +156,7 @@ module.exports = function (grunt) {
118 } 156 }
119 157
120 var htmlSourceDir; 158 var htmlSourceDir;
121 if (common.punch) { 159 if (mpConfig.punch) {
122 grunt.registerMultiTask('punch', 'run punch externally to create html files', function() { 160 grunt.registerMultiTask('punch', 'run punch externally to create html files', function() {
123 var done = this.async(); 161 var done = this.async();
124 var options = this.options(); 162 var options = this.options();
...@@ -201,7 +239,7 @@ module.exports = function (grunt) { ...@@ -201,7 +239,7 @@ module.exports = function (grunt) {
201 initConfig.clean.dist.files[0].src.push('.grunt/usemin'); 239 initConfig.clean.dist.files[0].src.push('.grunt/usemin');
202 initConfig.htmlmin = { 240 initConfig.htmlmin = {
203 dist: { 241 dist: {
204 //options: _.extend({}, common.htmlmin.options), 242 //options: _.extend({}, mpConfig.htmlmin.options),
205 files: [{ 243 files: [{
206 expand: true, 244 expand: true,
207 cwd: 'dist', 245 cwd: 'dist',
...@@ -212,26 +250,26 @@ module.exports = function (grunt) { ...@@ -212,26 +250,26 @@ module.exports = function (grunt) {
212 }; 250 };
213 } 251 }
214 252
215 if (common.css) { 253 if (mpConfig.css) {
216 initConfig.copy.css = { 254 initConfig.copy.css = {
217 files: [{ 255 files: [{
218 expand: true, 256 expand: true,
219 dot: true, 257 dot: true,
220 cwd: 'src', 258 cwd: 'src',
221 dest: '.grunt/usemin-css/', 259 dest: '.grunt/usemin-css/',
222 src: _.map(common.css.options.dirs, function (value) { 260 src: _.map(mpConfig.css.options.dirs, function (value) {
223 return value + '/{,**/}*.css'; 261 return value + '/{,**/}*.css';
224 }), 262 }),
225 }] 263 }]
226 }; 264 };
227 initConfig.clean.dist.files[0].src.push('.grunt/usemin-css'); 265 initConfig.clean.dist.files[0].src.push('.grunt/usemin-css');
228 initConfig.usemin.css = _.map(common.css.options.dirs, function (value) { 266 initConfig.usemin.css = _.map(mpConfig.css.options.dirs, function (value) {
229 return 'dist/' + value + '/{,**/}*.css'; 267 return 'dist/' + value + '/{,**/}*.css';
230 }); 268 });
231 if (common.useRev) { 269 if (mpConfig.useRev) {
232 initConfig.rev.css = { 270 initConfig.rev.css = {
233 files: { 271 files: {
234 src: _.map(common.css.options.dirs, function (value) { 272 src: _.map(mpConfig.css.options.dirs, function (value) {
235 return 'dist/' + value + '/{,**/}*.css'; 273 return 'dist/' + value + '/{,**/}*.css';
236 }), 274 }),
237 } 275 }
...@@ -239,10 +277,10 @@ module.exports = function (grunt) { ...@@ -239,10 +277,10 @@ module.exports = function (grunt) {
239 } 277 }
240 } 278 }
241 279
242 if (common.images) { 280 if (mpConfig.images) {
243 initConfig.imagemin = { 281 initConfig.imagemin = {
244 dist: { 282 dist: {
245 files: _.map(common.images.options.dirs, function (value) { 283 files: _.map(mpConfig.images.options.dirs, function (value) {
246 return { 284 return {
247 expand: true, 285 expand: true,
248 cwd: 'src/' + value, 286 cwd: 'src/' + value,
...@@ -254,7 +292,7 @@ module.exports = function (grunt) { ...@@ -254,7 +292,7 @@ module.exports = function (grunt) {
254 }; 292 };
255 initConfig.svgmin = { 293 initConfig.svgmin = {
256 dist: { 294 dist: {
257 files: _.map(common.images.options.dirs, function (value) { 295 files: _.map(mpConfig.images.options.dirs, function (value) {
258 return { 296 return {
259 expand: true, 297 expand: true,
260 cwd: 'src/' + value, 298 cwd: 'src/' + value,
...@@ -270,15 +308,15 @@ module.exports = function (grunt) { ...@@ -270,15 +308,15 @@ module.exports = function (grunt) {
270 dot: true, 308 dot: true,
271 cwd: 'src', 309 cwd: 'src',
272 dest: 'dist', 310 dest: 'dist',
273 src: _.map(common.images.options.dirs, function (value) { 311 src: _.map(mpConfig.images.options.dirs, function (value) {
274 return value + '/{,**/}*.*'; 312 return value + '/{,**/}*.*';
275 }), 313 }),
276 }] 314 }]
277 }; 315 };
278 if (common.useRev) { 316 if (mpConfig.useRev) {
279 initConfig.rev.img = { 317 initConfig.rev.img = {
280 files: { 318 files: {
281 src: _.map(common.images.options.dirs, function (value) { 319 src: _.map(mpConfig.images.options.dirs, function (value) {
282 return 'dist/' + value + '/{,**/}*.{gif,jpeg,jpg,png,webp}'; 320 return 'dist/' + value + '/{,**/}*.{gif,jpeg,jpg,png,webp}';
283 }), 321 }),
284 } 322 }
...@@ -286,49 +324,144 @@ module.exports = function (grunt) { ...@@ -286,49 +324,144 @@ module.exports = function (grunt) {
286 } 324 }
287 } 325 }
288 326
289 if (common.bower) { 327 if (mpConfig.bower) {
290 externalMultiTask('bower', 'run bower externally', 'bower', function(target, options, configFile) { 328 externalMultiTask('bower', 'run bower externally', 'bower', function(target, options, configFile) {
291 return ['--config.directory=' + options.directory, target]; 329 return ['--config.directory=' + options.directory, target];
292 }); 330 });
293 initConfig.bower = { 331 initConfig.bower = {
294 options: { 332 options: {
295 directory: 'src/' + common.bower.directory, 333 directory: 'src/' + mpConfig.bower.directory,
296 }, 334 },
297 install: {}, 335 install: {},
298 update: {}, 336 update: {},
299 }; 337 };
300 if (common.useRev) { 338 if (mpConfig.useRev) {
301 initConfig.rev.dist.files.push({src: 'dist/' + common.bower.directory}); 339 initConfig.rev.dist.files.push({
340 src: [
341 'dist/' + mpConfig.bower.directory + '/**/*.js',
342 '!dist/' + mpConfig.bower.directory + '/requirejs/require.js',
343 'dist/scripts/**/*.js',
344 '!dist/scripts/config.js',
345 '!dist/scripts/defaults/**/*.js',
346 ],
347 });
348 initConfig.rev.requirejs = {
349 files: {
350 src: [
351 'dist/' + mpConfig.bower.directory + '/requirejs/require.js',
352 ],
353 },
354 };
302 } 355 }
356 initConfig.concat.requirejs = {
357 files: [{
358 dest: 'dist/' + mpConfig.bower.directory + '/requirejs/require.js',
359 src: [
360 'src/scripts/config.js',
361 '.grunt/holygrail/scripts/revconfig.js',
362 '.grunt/holygrail/scripts/bundles.js',
363 'src/' + mpConfig.bower.directory + '/requirejs/require.js',
364 ],
365 }],
366 };
367 initConfig.requirejs = {
368 dist: {
369 options: {
370
371 done: function onDone(next) {
372 /* jshint devel:true */
373 var target = grunt.task.current.target;
374 var requireModules = grunt.config('requireModules.' + target) || {};
375 var lines = [
376 '(function(container, propName) {',
377 'container = (container[propName] = container[propName] || {});',
378 ];
379 for (var key in requireModules) {
380 var keyS = JSON.stringify(key);
381 var value = requireModules[key];
382 var included = [];
383 for (var i = 0; i < value.length; i++) {
384 var file = value[i];
385 if (file.match(/\.js$/)) {
386 included.push(file.substring(0, file.length - 3));
387 }
388 }
389 lines.push('container[' + keyS + '] = ' + JSON.stringify(included) + ';');
390 }
391 //lines.push('return bundles;');
392 lines.push('})(require, \'bundles\');');
393 if (lines.length == 2) {
394 lines = [];
395 }
396 grunt.file.write('.grunt/holygrail/scripts/bundles.js', lines.join('\n'));
397 next();
398 },
399
400 onModuleBundleComplete: function onModuleBundleComplete(data) {
401 /* jshint devel:true */
402 var target = grunt.task.current.target;
403 grunt.config('requireModules.' + target + '.' + grunt.config.escape(data.name), data.included);
404 },
405
406 baseUrl: 'src/scripts',
407 mainConfigFile: [
408 'src/scripts/config.js',
409 ],
410
411 wrapShim: true,
412 keepBuildDir: true,
413 optimize: 'none',
414 removeCombined: true,
415 skipDirOptimize: true,
416 //generateSourceMaps: true,
417
418 dir: 'dist/scripts',
419 locale: 'en-us',
420 paths: {
421 'defaults': 'empty:',
422 },
423 modules: mpConfig.modules,
424 },
425 }
426 };
303 } 427 }
304 428
305 buildList.push('useminPrepare'); // html 429 buildList.push('useminPrepare'); // html
306 //buildList.push('usemin:views' 430 //buildList.push('usemin:views'
307 //buildList.push('requirejs'); // script 431 if (mpConfig.bower) {
432 buildList.push('requirejs'); // script
433 }
308 buildList.push('copy:css'); // css 434 buildList.push('copy:css'); // css
309 if (common.images) { 435 if (mpConfig.images) {
310 buildList.push('imagemin'); // image 436 buildList.push('imagemin'); // image
311 buildList.push('svgmin'); // image 437 buildList.push('svgmin'); // image
312 if (common.useRev) { 438 if (mpConfig.useRev) {
313 buildList.push('rev:img'); // image 439 buildList.push('rev:img'); // image
314 } 440 }
315 } 441 }
316 //buildList.push('autoprefixer'); // css 442 //buildList.push('autoprefixer'); // css
317 //buildList.push('concat:requirejs'); // montyPython
318 buildList.push('concat:generated'); // montyPython 443 buildList.push('concat:generated'); // montyPython
319 buildList.push('cssmin'); // montyPython 444 buildList.push('cssmin'); // montyPython
320 //'uglify:dist'); 445 //'uglify:dist');
321 buildList.push('copy:dist'); 446 buildList.push('copy:dist');
322 //'modernizr'); 447 //'modernizr');
323 //buildList.push('uglify:generated'); // montyPython 448 //buildList.push('uglify:generated'); // montyPython
324 if (common.useRev) { 449 if (mpConfig.useRev) {
325 buildList.push('rev:dist'); // montyPython 450 buildList.push('rev:dist'); // montyPython
326 buildList.push('rev:css'); // montyPython 451 buildList.push('rev:css'); // montyPython
452 buildList.push('revconfig'); // montyPython
453 }
454 if (mpConfig.bower) {
455 buildList.push('concat:requirejs'); // montyPython
456 }
457 if (mpConfig.useRev) {
458 buildList.push('rev:requirejs');
327 } 459 }
328 //buildList.push('revconfig'); // montyPython
329 //buildList.push('uglify:bower'); // montyPython 460 //buildList.push('uglify:bower'); // montyPython
330 //buildList.push('uglify:requireconfig'); // montyPython 461 //buildList.push('uglify:requireconfig'); // montyPython
331 //buildList.push('rev:requireconfig'); // montyPython 462 if (mpConfig.useRev && mpConfig.bower) {
463 //buildList.push('rev:requireconfig'); // montyPython
464 }
332 buildList.push('usemin:html'); // montyPython 465 buildList.push('usemin:html'); // montyPython
333 buildList.push('usemin:css'); // montyPython 466 buildList.push('usemin:css'); // montyPython
334 buildList.push('htmlmin'); // montyPython 467 buildList.push('htmlmin'); // montyPython
......
...@@ -24,6 +24,7 @@ ...@@ -24,6 +24,7 @@
24 "grunt-contrib-jshint": "~0", 24 "grunt-contrib-jshint": "~0",
25 "grunt-contrib-htmlmin": "~0", 25 "grunt-contrib-htmlmin": "~0",
26 "grunt-contrib-imagemin": "~0", 26 "grunt-contrib-imagemin": "~0",
27 "grunt-contrib-requirejs": "~0",
27 "grunt-rev": "~0", 28 "grunt-rev": "~0",
28 "grunt-svgmin": "~0", 29 "grunt-svgmin": "~0",
29 "grunt-usemin": "~2", 30 "grunt-usemin": "~2",
......