32d88eb1 by Adam Heath

requirejs-config now contains a single function.

1 parent 1c48ec40
...@@ -44,16 +44,14 @@ module.exports = function (grunt) { ...@@ -44,16 +44,14 @@ module.exports = function (grunt) {
44 grunt.registerTask('fixRequireConfig', function fixRequireConfig() { 44 grunt.registerTask('fixRequireConfig', function fixRequireConfig() {
45 var config = grunt.config('montyPython.config'); 45 var config = grunt.config('montyPython.config');
46 var featureFlags = grunt.config('montyPython.featureFlags'); 46 var featureFlags = grunt.config('montyPython.featureFlags');
47 var lines, segments = []; 47 var segment = {}, segments = {};
48 48
49 // --- 49 // ---
50 if (config.useRev) { 50 if (config.useRev) {
51 var prefix = grunt.template.process('dist/build/scripts/'); 51 var prefix = grunt.template.process('dist/build/scripts/');
52 var pattern = prefix + '**/*.{js,html}'; 52 var pattern = prefix + '**/*.{js,html}';
53 var files = grunt.file.expand(pattern); 53 var files = grunt.file.expand(pattern);
54 lines = [ 54 if (files.length) {
55 '(function(container) {',
56 ];
57 _.each(files, function(file) { 55 _.each(files, function(file) {
58 file = file.substring(prefix.length); 56 file = file.substring(prefix.length);
59 var res = file.match(/^(.*\/)?([0-9a-f]+)\.([^\/]+)\.([^\.]+)$/); 57 var res = file.match(/^(.*\/)?([0-9a-f]+)\.([^\/]+)\.([^\.]+)$/);
...@@ -73,21 +71,21 @@ module.exports = function (grunt) { ...@@ -73,21 +71,21 @@ module.exports = function (grunt) {
73 id = 'text!' + dir + base + '.' + ext; 71 id = 'text!' + dir + base + '.' + ext;
74 } 72 }
75 grunt.log.oklns('map: ' + id + ' -> ' + file); 73 grunt.log.oklns('map: ' + id + ' -> ' + file);
76 lines.push('container[' + JSON.stringify(id) + ']=' + JSON.stringify(file) + ';\n'); 74 segment[id] = file;
77 }); 75 });
78 if (lines.length > 1) { 76 segments.paths = segment;
79 lines.push('})(require.paths);'); 77 segment = {};
80 segments = segments.concat(lines);
81 lines = null;
82 } 78 }
83 } 79 }
84 80
85 // --- 81 // ---
86 var requireModules = grunt.config('requireModules.requirejs') || {}; 82 var requireModules = grunt.config('requireModules.requirejs') || {};
83 /*
87 lines = [ 84 lines = [
88 '(function(container, propName) {', 85 '(function(container, propName) {',
89 'container = (container[propName] = container[propName] || {});', 86 'container = (container[propName] = container[propName] || {});',
90 ]; 87 ];
88 */
91 for (var key in requireModules) { 89 for (var key in requireModules) {
92 var keyS = JSON.stringify(key); 90 var keyS = JSON.stringify(key);
93 var value = requireModules[key]; 91 var value = requireModules[key];
...@@ -98,28 +96,41 @@ module.exports = function (grunt) { ...@@ -98,28 +96,41 @@ module.exports = function (grunt) {
98 included.push(file.substring(0, file.length - 3)); 96 included.push(file.substring(0, file.length - 3));
99 } 97 }
100 } 98 }
101 lines.push('container[' + keyS + '] = ' + JSON.stringify(included) + ';'); 99 segment[key] = included;
102 }
103 if (lines.length > 2) {
104 lines.push('})(require, \'bundles\');');
105 segments = segments.concat(lines);
106 lines = null;
107 } 100 }
101 segments.bundles = segment;
108 102
109 // --- 103 // ---
104 segments['config.featureFlags'] = featureFlags;
105 var notEmptyCount = 0;
106 _.each(segments, function(value, key) {
107 if (!_.isEmpty(value)) {
108 notEmptyCount++;
109 }
110 });
111 var lines;
112 if (notEmptyCount) {
110 lines = [ 113 lines = [
111 '(function(container, propName) {', 114 '(function(require, overlay) {',
112 'container = (container[propName] = container[propName] || {});', 115 ' var prop, value, ptr, i, list, container;',
116 ' for (prop in overlay) {',
117 ' value = overlay[prop];',
118 ' ptr = require;',
119 ' list = split(".", prop);',
120 ' for (i = 0; i < list.length; i++) {',
121 ' if (!ptr[list[i]]) ptr[list[i]] = {};',
122 ' ptr = ptr[list[i]];',
123 ' }',
124 ' for (prop in value) {',
125 ' ptr[prop] = value[prop];',
126 ' }',
127 ' }',
128 '})(require, ' + JSON.stringify(segments) + ');',
113 ]; 129 ];
114 for (var flag in featureFlags) { 130 } else {
115 lines.push('container[' + JSON.stringify(flag) + ']=' + JSON.stringify(featureFlags[flag]) + ';'); 131 lines = [];
116 }
117 if (lines.length > 2) {
118 lines.push('})(require.config, \'featureFlags\');');
119 segments = segments.concat(lines);
120 lines = null;
121 } 132 }
122 grunt.file.write('.grunt/holygrail/scripts/requirejs-config.js', segments.join('\n')); 133 grunt.file.write('.grunt/holygrail/scripts/requirejs-config.js', lines.join('\n'));
123 return true; 134 return true;
124 }); 135 });
125 136
......