efcdde5f by Adam Heath

Requirejs.

1 parent 3e9e2242
......@@ -7,6 +7,43 @@
module.exports = function (grunt) {
var _ = require('lodash');
grunt.registerTask('revconfig', function revconfig() {
var prefix = grunt.template.process('dist/scripts/');
var pattern = prefix + '**/*.{js,html}';
var files = grunt.file.expand(pattern);
var 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');
});
lines.push('})(require.paths);');
if (false && lines.length == 2) {
lines = [];
}
grunt.file.write('.grunt/holygrail/scripts/revconfig.js', lines.join(''));
});
function createConfig(mpConfig) {
var holyGrailTmp = '.grunt/holygrail';
......@@ -23,6 +60,7 @@ module.exports = function (grunt) {
}]
},
},
concat: {},
copy: {
dist: {
files: [{
......@@ -40,7 +78,7 @@ module.exports = function (grunt) {
},
},
};
if (common.useRev) {
if (mpConfig.useRev) {
initConfig.rev = {
dist: {
files: [],
......@@ -118,7 +156,7 @@ module.exports = function (grunt) {
}
var htmlSourceDir;
if (common.punch) {
if (mpConfig.punch) {
grunt.registerMultiTask('punch', 'run punch externally to create html files', function() {
var done = this.async();
var options = this.options();
......@@ -201,7 +239,7 @@ module.exports = function (grunt) {
initConfig.clean.dist.files[0].src.push('.grunt/usemin');
initConfig.htmlmin = {
dist: {
//options: _.extend({}, common.htmlmin.options),
//options: _.extend({}, mpConfig.htmlmin.options),
files: [{
expand: true,
cwd: 'dist',
......@@ -212,26 +250,26 @@ module.exports = function (grunt) {
};
}
if (common.css) {
if (mpConfig.css) {
initConfig.copy.css = {
files: [{
expand: true,
dot: true,
cwd: 'src',
dest: '.grunt/usemin-css/',
src: _.map(common.css.options.dirs, function (value) {
src: _.map(mpConfig.css.options.dirs, function (value) {
return value + '/{,**/}*.css';
}),
}]
};
initConfig.clean.dist.files[0].src.push('.grunt/usemin-css');
initConfig.usemin.css = _.map(common.css.options.dirs, function (value) {
initConfig.usemin.css = _.map(mpConfig.css.options.dirs, function (value) {
return 'dist/' + value + '/{,**/}*.css';
});
if (common.useRev) {
if (mpConfig.useRev) {
initConfig.rev.css = {
files: {
src: _.map(common.css.options.dirs, function (value) {
src: _.map(mpConfig.css.options.dirs, function (value) {
return 'dist/' + value + '/{,**/}*.css';
}),
}
......@@ -239,10 +277,10 @@ module.exports = function (grunt) {
}
}
if (common.images) {
if (mpConfig.images) {
initConfig.imagemin = {
dist: {
files: _.map(common.images.options.dirs, function (value) {
files: _.map(mpConfig.images.options.dirs, function (value) {
return {
expand: true,
cwd: 'src/' + value,
......@@ -254,7 +292,7 @@ module.exports = function (grunt) {
};
initConfig.svgmin = {
dist: {
files: _.map(common.images.options.dirs, function (value) {
files: _.map(mpConfig.images.options.dirs, function (value) {
return {
expand: true,
cwd: 'src/' + value,
......@@ -270,15 +308,15 @@ module.exports = function (grunt) {
dot: true,
cwd: 'src',
dest: 'dist',
src: _.map(common.images.options.dirs, function (value) {
src: _.map(mpConfig.images.options.dirs, function (value) {
return value + '/{,**/}*.*';
}),
}]
};
if (common.useRev) {
if (mpConfig.useRev) {
initConfig.rev.img = {
files: {
src: _.map(common.images.options.dirs, function (value) {
src: _.map(mpConfig.images.options.dirs, function (value) {
return 'dist/' + value + '/{,**/}*.{gif,jpeg,jpg,png,webp}';
}),
}
......@@ -286,49 +324,144 @@ module.exports = function (grunt) {
}
}
if (common.bower) {
if (mpConfig.bower) {
externalMultiTask('bower', 'run bower externally', 'bower', function(target, options, configFile) {
return ['--config.directory=' + options.directory, target];
});
initConfig.bower = {
options: {
directory: 'src/' + common.bower.directory,
directory: 'src/' + mpConfig.bower.directory,
},
install: {},
update: {},
};
if (common.useRev) {
initConfig.rev.dist.files.push({src: 'dist/' + common.bower.directory});
if (mpConfig.useRev) {
initConfig.rev.dist.files.push({
src: [
'dist/' + mpConfig.bower.directory + '/**/*.js',
'!dist/' + mpConfig.bower.directory + '/requirejs/require.js',
'dist/scripts/**/*.js',
'!dist/scripts/config.js',
'!dist/scripts/defaults/**/*.js',
],
});
initConfig.rev.requirejs = {
files: {
src: [
'dist/' + mpConfig.bower.directory + '/requirejs/require.js',
],
},
};
}
initConfig.concat.requirejs = {
files: [{
dest: 'dist/' + mpConfig.bower.directory + '/requirejs/require.js',
src: [
'src/scripts/config.js',
'.grunt/holygrail/scripts/revconfig.js',
'.grunt/holygrail/scripts/bundles.js',
'src/' + mpConfig.bower.directory + '/requirejs/require.js',
],
}],
};
initConfig.requirejs = {
dist: {
options: {
done: function onDone(next) {
/* jshint devel:true */
var target = grunt.task.current.target;
var requireModules = grunt.config('requireModules.' + target) || {};
var lines = [
'(function(container, propName) {',
'container = (container[propName] = container[propName] || {});',
];
for (var key in requireModules) {
var keyS = JSON.stringify(key);
var value = requireModules[key];
var included = [];
for (var i = 0; i < value.length; i++) {
var file = value[i];
if (file.match(/\.js$/)) {
included.push(file.substring(0, file.length - 3));
}
}
lines.push('container[' + keyS + '] = ' + JSON.stringify(included) + ';');
}
//lines.push('return bundles;');
lines.push('})(require, \'bundles\');');
if (lines.length == 2) {
lines = [];
}
grunt.file.write('.grunt/holygrail/scripts/bundles.js', lines.join('\n'));
next();
},
onModuleBundleComplete: function onModuleBundleComplete(data) {
/* jshint devel:true */
var target = grunt.task.current.target;
grunt.config('requireModules.' + target + '.' + grunt.config.escape(data.name), data.included);
},
baseUrl: 'src/scripts',
mainConfigFile: [
'src/scripts/config.js',
],
wrapShim: true,
keepBuildDir: true,
optimize: 'none',
removeCombined: true,
skipDirOptimize: true,
//generateSourceMaps: true,
dir: 'dist/scripts',
locale: 'en-us',
paths: {
'defaults': 'empty:',
},
modules: mpConfig.modules,
},
}
};
}
buildList.push('useminPrepare'); // html
//buildList.push('usemin:views'
//buildList.push('requirejs'); // script
if (mpConfig.bower) {
buildList.push('requirejs'); // script
}
buildList.push('copy:css'); // css
if (common.images) {
if (mpConfig.images) {
buildList.push('imagemin'); // image
buildList.push('svgmin'); // image
if (common.useRev) {
if (mpConfig.useRev) {
buildList.push('rev:img'); // image
}
}
//buildList.push('autoprefixer'); // css
//buildList.push('concat:requirejs'); // montyPython
buildList.push('concat:generated'); // montyPython
buildList.push('cssmin'); // montyPython
//'uglify:dist');
buildList.push('copy:dist');
//'modernizr');
//buildList.push('uglify:generated'); // montyPython
if (common.useRev) {
if (mpConfig.useRev) {
buildList.push('rev:dist'); // montyPython
buildList.push('rev:css'); // montyPython
buildList.push('revconfig'); // montyPython
}
if (mpConfig.bower) {
buildList.push('concat:requirejs'); // montyPython
}
if (mpConfig.useRev) {
buildList.push('rev:requirejs');
}
//buildList.push('revconfig'); // montyPython
//buildList.push('uglify:bower'); // montyPython
//buildList.push('uglify:requireconfig'); // montyPython
if (mpConfig.useRev && mpConfig.bower) {
//buildList.push('rev:requireconfig'); // montyPython
}
buildList.push('usemin:html'); // montyPython
buildList.push('usemin:css'); // montyPython
buildList.push('htmlmin'); // montyPython
......
......@@ -24,6 +24,7 @@
"grunt-contrib-jshint": "~0",
"grunt-contrib-htmlmin": "~0",
"grunt-contrib-imagemin": "~0",
"grunt-contrib-requirejs": "~0",
"grunt-rev": "~0",
"grunt-svgmin": "~0",
"grunt-usemin": "~2",
......