83da0e2d by Adam Heath

fixRequireConfig.

1 parent e958f8b5
...@@ -40,9 +40,91 @@ module.exports = function (grunt) { ...@@ -40,9 +40,91 @@ module.exports = function (grunt) {
40 grunt.file.write('.grunt/holygrail/scripts/revconfig.js', lines.join('')); 40 grunt.file.write('.grunt/holygrail/scripts/revconfig.js', lines.join(''));
41 }); 41 });
42 42
43 grunt.registerTask('fixRequireConfig', function fixRequireConfig() {
44 var config = grunt.config('montyPython.config');
45 var featureFlags = grunt.config('montyPython.featureFlags');
46 var lines, segments = [];
47
48 // ---
49 if (config.useRev) {
50 var prefix = grunt.template.process('dist/scripts/');
51 var pattern = prefix + '**/*.{js,html}';
52 var files = grunt.file.expand(pattern);
53 lines = [
54 '(function(container) {',
55 ];
56 _.each(files, function(file) {
57 file = file.substring(prefix.length);
58 var res = file.match(/^(.*\/)?([0-9a-f]+)\.([^\/]+)\.([^\.]+)$/);
59 if (!res) {
60 return;
61 }
62 //grunt.log.oklns(JSON.stringify(res));
63 var dir = res[1] || '';
64 //var hash = res[2];
65 var base = res[3];
66 var ext = res[4];
67 var id;
68 if (ext === 'js') {
69 id = dir + base;
70 file = file.substring(0, file.length - ext.length - 1);
71 } else if (ext === 'html') {
72 id = 'text!' + dir + base + '.' + ext;
73 }
74 grunt.log.oklns('map: ' + id + ' -> ' + file);
75 lines.push('container[' + JSON.stringify(id) + ']=' + JSON.stringify(file) + ';\n');
76 });
77 if (lines.length > 1) {
78 lines.push('})(require.paths);');
79 segments = segments.concat(lines);
80 lines = null
81 }
82 }
83
84 // ---
85 var requireModules = grunt.config('requireModules.requirejs') || {};
86 lines = [
87 '(function(container, propName) {',
88 'container = (container[propName] = container[propName] || {});',
89 ];
90 for (var key in requireModules) {
91 var keyS = JSON.stringify(key);
92 var value = requireModules[key];
93 var included = [];
94 for (var i = 0; i < value.length; i++) {
95 var file = value[i];
96 if (file.match(/\.js$/)) {
97 included.push(file.substring(0, file.length - 3));
98 }
99 }
100 lines.push('container[' + keyS + '] = ' + JSON.stringify(included) + ';');
101 }
102 if (lines.length > 2) {
103 lines.push('})(require, \'bundles\');');
104 segments = segments.concat(lines);
105 lines = null;
106 }
107
108 // ---
109 lines = [
110 '(function(container, propName) {',
111 'container = (container[propName] = container[propName] || {});',
112 ];
113 for (var flag in featureFlags) {
114 lines.push('container[' + JSON.stringify(flag) + ']=' + JSON.stringify(featureFlags[flag]) + ';');
115 }
116 if (lines.length > 2) {
117 lines.push('})(require.config, \'featureFlags\');');
118 segments = segments.concat(lines);
119 lines = null;
120 }
121 grunt.file.write('.grunt/holygrail/scripts/requirejs-config.js', segments.join('\n'));
122 return true;
123 });
43 124
44 function createConfig(mpConfig) { 125 function createConfig(mpConfig) {
45 126
127 var featureFlags = {};
46 var holyGrailTmp = '.grunt/holygrail'; 128 var holyGrailTmp = '.grunt/holygrail';
47 var initConfig = { 129 var initConfig = {
48 clean: { 130 clean: {
...@@ -369,8 +451,7 @@ module.exports = function (grunt) { ...@@ -369,8 +451,7 @@ module.exports = function (grunt) {
369 dest: 'dist/' + mpConfig.bower.directory + '/requirejs/require.js', 451 dest: 'dist/' + mpConfig.bower.directory + '/requirejs/require.js',
370 src: [ 452 src: [
371 'src/scripts/config.js', 453 'src/scripts/config.js',
372 '.grunt/holygrail/scripts/revconfig.js', 454 '.grunt/holygrail/scripts/requirejs-config.js',
373 '.grunt/holygrail/scripts/bundles.js',
374 'src/' + mpConfig.bower.directory + '/requirejs/require.js', 455 'src/' + mpConfig.bower.directory + '/requirejs/require.js',
375 ], 456 ],
376 }], 457 }],
...@@ -378,34 +459,7 @@ module.exports = function (grunt) { ...@@ -378,34 +459,7 @@ module.exports = function (grunt) {
378 initConfig.requirejs = { 459 initConfig.requirejs = {
379 dist: { 460 dist: {
380 options: { 461 options: {
381 done: function onDone(next) { 462 ///* -jshint devel:true */
382 /* jshint devel:true */
383 var target = grunt.task.current.target;
384 var requireModules = grunt.config('requireModules.' + target) || {};
385 var lines = [
386 '(function(container, propName) {',
387 'container = (container[propName] = container[propName] || {});',
388 ];
389 for (var key in requireModules) {
390 var keyS = JSON.stringify(key);
391 var value = requireModules[key];
392 var included = [];
393 for (var i = 0; i < value.length; i++) {
394 var file = value[i];
395 if (file.match(/\.js$/)) {
396 included.push(file.substring(0, file.length - 3));
397 }
398 }
399 lines.push('container[' + keyS + '] = ' + JSON.stringify(included) + ';');
400 }
401 //lines.push('return bundles;');
402 lines.push('})(require, \'bundles\');');
403 if (lines.length == 3) {
404 lines = [];
405 }
406 grunt.file.write('.grunt/holygrail/scripts/bundles.js', lines.join('\n'));
407 next();
408 },
409 463
410 onModuleBundleComplete: function onModuleBundleComplete(data) { 464 onModuleBundleComplete: function onModuleBundleComplete(data) {
411 /* jshint devel:true */ 465 /* jshint devel:true */
...@@ -437,6 +491,7 @@ module.exports = function (grunt) { ...@@ -437,6 +491,7 @@ module.exports = function (grunt) {
437 } 491 }
438 492
439 grunt.initConfig(initConfig); 493 grunt.initConfig(initConfig);
494 grunt.config('montyPython.config', mpConfig);
440 495
441 grunt.registerTask('check', checkList); 496 grunt.registerTask('check', checkList);
442 497
...@@ -454,23 +509,8 @@ module.exports = function (grunt) { ...@@ -454,23 +509,8 @@ module.exports = function (grunt) {
454 for (var i in arguments) { 509 for (var i in arguments) {
455 featureFlags['BUILD_' + arguments[i].toUpperCase()] = true; 510 featureFlags['BUILD_' + arguments[i].toUpperCase()] = true;
456 } 511 }
457 grunt.registerTask('writeFeatureFlags', 'write feature flags', function() {
458 var lines = [
459 '(function(container, propName) {',
460 'container = (container[propName] = container[propName] || {});',
461 ];
462 for (var flag in featureFlags) {
463 lines.push('container[' + JSON.stringify(flag) + ']=' + JSON.stringify(featureFlags[flag]) + ';');
464 }
465 //lines.push('return bundles;');
466 lines.push('})(require.config, \'featureFlags\');');
467 if (lines.length == 3) {
468 lines = [];
469 }
470 grunt.file.write('.grunt/holygrail/scripts/featureFlags.js', lines.join('\n'));
471 });
472 buildList.push('writeFeatureFlags');
473 } 512 }
513 grunt.config('montyPython.featureFlags', featureFlags);
474 514
475 if (mpConfig.punch) { 515 if (mpConfig.punch) {
476 buildList.push('punch:generate'); 516 buildList.push('punch:generate');
...@@ -500,8 +540,8 @@ module.exports = function (grunt) { ...@@ -500,8 +540,8 @@ module.exports = function (grunt) {
500 if (mpConfig.useRev) { 540 if (mpConfig.useRev) {
501 buildList.push('rev:dist'); // montyPython 541 buildList.push('rev:dist'); // montyPython
502 buildList.push('rev:css'); // montyPython 542 buildList.push('rev:css'); // montyPython
503 buildList.push('revconfig'); // montyPython
504 } 543 }
544 buildList.push('fixRequireConfig');
505 if (mpConfig.bower) { 545 if (mpConfig.bower) {
506 buildList.push('concat:requirejs'); // montyPython 546 buildList.push('concat:requirejs'); // montyPython
507 } 547 }
......