32d88eb1 by Adam Heath

requirejs-config now contains a single function.

1 parent 1c48ec40
...@@ -44,50 +44,48 @@ module.exports = function (grunt) { ...@@ -44,50 +44,48 @@ 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) {', 55 _.each(files, function(file) {
56 ]; 56 file = file.substring(prefix.length);
57 _.each(files, function(file) { 57 var res = file.match(/^(.*\/)?([0-9a-f]+)\.([^\/]+)\.([^\.]+)$/);
58 file = file.substring(prefix.length); 58 if (!res) {
59 var res = file.match(/^(.*\/)?([0-9a-f]+)\.([^\/]+)\.([^\.]+)$/); 59 return;
60 if (!res) { 60 }
61 return; 61 //grunt.log.oklns(JSON.stringify(res));
62 } 62 var dir = res[1] || '';
63 //grunt.log.oklns(JSON.stringify(res)); 63 //var hash = res[2];
64 var dir = res[1] || ''; 64 var base = res[3];
65 //var hash = res[2]; 65 var ext = res[4];
66 var base = res[3]; 66 var id;
67 var ext = res[4]; 67 if (ext === 'js') {
68 var id; 68 id = dir + base;
69 if (ext === 'js') { 69 file = file.substring(0, file.length - ext.length - 1);
70 id = dir + base; 70 } else if (ext === 'html') {
71 file = file.substring(0, file.length - ext.length - 1); 71 id = 'text!' + dir + base + '.' + ext;
72 } else if (ext === 'html') { 72 }
73 id = 'text!' + dir + base + '.' + ext; 73 grunt.log.oklns('map: ' + id + ' -> ' + file);
74 } 74 segment[id] = file;
75 grunt.log.oklns('map: ' + id + ' -> ' + file); 75 });
76 lines.push('container[' + JSON.stringify(id) + ']=' + JSON.stringify(file) + ';\n'); 76 segments.paths = segment;
77 }); 77 segment = {};
78 if (lines.length > 1) {
79 lines.push('})(require.paths);');
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 // ---
110 lines = [ 104 segments['config.featureFlags'] = featureFlags;
111 '(function(container, propName) {', 105 var notEmptyCount = 0;
112 'container = (container[propName] = container[propName] || {});', 106 _.each(segments, function(value, key) {
113 ]; 107 if (!_.isEmpty(value)) {
114 for (var flag in featureFlags) { 108 notEmptyCount++;
115 lines.push('container[' + JSON.stringify(flag) + ']=' + JSON.stringify(featureFlags[flag]) + ';'); 109 }
116 } 110 });
117 if (lines.length > 2) { 111 var lines;
118 lines.push('})(require.config, \'featureFlags\');'); 112 if (notEmptyCount) {
119 segments = segments.concat(lines); 113 lines = [
120 lines = null; 114 '(function(require, overlay) {',
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) + ');',
129 ];
130 } else {
131 lines = [];
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
......