32d88eb1 by Adam Heath

requirejs-config now contains a single function.

1 parent 1c48ec40
......@@ -44,16 +44,14 @@ module.exports = function (grunt) {
grunt.registerTask('fixRequireConfig', function fixRequireConfig() {
var config = grunt.config('montyPython.config');
var featureFlags = grunt.config('montyPython.featureFlags');
var lines, segments = [];
var segment = {}, segments = {};
// ---
if (config.useRev) {
var prefix = grunt.template.process('dist/build/scripts/');
var pattern = prefix + '**/*.{js,html}';
var files = grunt.file.expand(pattern);
lines = [
'(function(container) {',
];
if (files.length) {
_.each(files, function(file) {
file = file.substring(prefix.length);
var res = file.match(/^(.*\/)?([0-9a-f]+)\.([^\/]+)\.([^\.]+)$/);
......@@ -73,21 +71,21 @@ module.exports = function (grunt) {
id = 'text!' + dir + base + '.' + ext;
}
grunt.log.oklns('map: ' + id + ' -> ' + file);
lines.push('container[' + JSON.stringify(id) + ']=' + JSON.stringify(file) + ';\n');
segment[id] = file;
});
if (lines.length > 1) {
lines.push('})(require.paths);');
segments = segments.concat(lines);
lines = null;
segments.paths = segment;
segment = {};
}
}
// ---
var requireModules = grunt.config('requireModules.requirejs') || {};
/*
lines = [
'(function(container, propName) {',
'container = (container[propName] = container[propName] || {});',
];
*/
for (var key in requireModules) {
var keyS = JSON.stringify(key);
var value = requireModules[key];
......@@ -98,28 +96,41 @@ module.exports = function (grunt) {
included.push(file.substring(0, file.length - 3));
}
}
lines.push('container[' + keyS + '] = ' + JSON.stringify(included) + ';');
}
if (lines.length > 2) {
lines.push('})(require, \'bundles\');');
segments = segments.concat(lines);
lines = null;
segment[key] = included;
}
segments.bundles = segment;
// ---
segments['config.featureFlags'] = featureFlags;
var notEmptyCount = 0;
_.each(segments, function(value, key) {
if (!_.isEmpty(value)) {
notEmptyCount++;
}
});
var lines;
if (notEmptyCount) {
lines = [
'(function(container, propName) {',
'container = (container[propName] = container[propName] || {});',
'(function(require, overlay) {',
' var prop, value, ptr, i, list, container;',
' for (prop in overlay) {',
' value = overlay[prop];',
' ptr = require;',
' list = split(".", prop);',
' for (i = 0; i < list.length; i++) {',
' if (!ptr[list[i]]) ptr[list[i]] = {};',
' ptr = ptr[list[i]];',
' }',
' for (prop in value) {',
' ptr[prop] = value[prop];',
' }',
' }',
'})(require, ' + JSON.stringify(segments) + ');',
];
for (var flag in featureFlags) {
lines.push('container[' + JSON.stringify(flag) + ']=' + JSON.stringify(featureFlags[flag]) + ';');
}
if (lines.length > 2) {
lines.push('})(require.config, \'featureFlags\');');
segments = segments.concat(lines);
lines = null;
} else {
lines = [];
}
grunt.file.write('.grunt/holygrail/scripts/requirejs-config.js', segments.join('\n'));
grunt.file.write('.grunt/holygrail/scripts/requirejs-config.js', lines.join('\n'));
return true;
});
......