32d88eb1 by Adam Heath

requirejs-config now contains a single function.

1 parent 1c48ec40
......@@ -44,50 +44,48 @@ 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) {',
];
_.each(files, function(file) {
file = file.substring(prefix.length);
var res = file.match(/^(.*\/)?([0-9a-f]+)\.([^\/]+)\.([^\.]+)$/);
if (!res) {
return;
}
//grunt.log.oklns(JSON.stringify(res));
var dir = res[1] || '';
//var hash = res[2];
var base = res[3];
var ext = res[4];
var id;
if (ext === 'js') {
id = dir + base;
file = file.substring(0, file.length - ext.length - 1);
} else if (ext === 'html') {
id = 'text!' + dir + base + '.' + ext;
}
grunt.log.oklns('map: ' + id + ' -> ' + file);
lines.push('container[' + JSON.stringify(id) + ']=' + JSON.stringify(file) + ';\n');
});
if (lines.length > 1) {
lines.push('})(require.paths);');
segments = segments.concat(lines);
lines = null;
if (files.length) {
_.each(files, function(file) {
file = file.substring(prefix.length);
var res = file.match(/^(.*\/)?([0-9a-f]+)\.([^\/]+)\.([^\.]+)$/);
if (!res) {
return;
}
//grunt.log.oklns(JSON.stringify(res));
var dir = res[1] || '';
//var hash = res[2];
var base = res[3];
var ext = res[4];
var id;
if (ext === 'js') {
id = dir + base;
file = file.substring(0, file.length - ext.length - 1);
} else if (ext === 'html') {
id = 'text!' + dir + base + '.' + ext;
}
grunt.log.oklns('map: ' + id + ' -> ' + file);
segment[id] = file;
});
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;
// ---
lines = [
'(function(container, propName) {',
'container = (container[propName] = container[propName] || {});',
];
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;
segments['config.featureFlags'] = featureFlags;
var notEmptyCount = 0;
_.each(segments, function(value, key) {
if (!_.isEmpty(value)) {
notEmptyCount++;
}
});
var lines;
if (notEmptyCount) {
lines = [
'(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) + ');',
];
} 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;
});
......