Skip to content

Instantly share code, notes, and snippets.

@hkdobrev
Forked from noahgrant/Dynamically loading tasks from useminPrepare
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hkdobrev/9560651 to your computer and use it in GitHub Desktop.
Save hkdobrev/9560651 to your computer and use it in GitHub Desktop.

However, grunt build will not complete because it no concat targets are found (nor uglify targets, for that matter). If it were working properly, the concat:generated and uglify:generated tasks would have been added to the loaded grunt tasks, which would then be executed by usemin in the last step of my build.

Hopefully this is all clear!

'use strict';
var path = require('path');
module.exports = function (grunt) {
// Load externally-defined tasks
grunt.loadTasks('tasks/grunt');
// Load grunt tasks automatically
require('load-grunt-config')(grunt, {
configPath: path.join(process.cwd(), 'tasks/grunt/options'),
init: true,
config: {
config: {
app: 'app',
dist: 'dist',
tmp: '.tmp',
test: 'test'
}
},
loadGruntTasks: {
pattern: 'grunt-*'
}
});
// Time how long tasks take. Can help when optimizing build times
require('time-grunt')(grunt);
};
// tasks/grunt/options/useminPrepare.js:
module.exports = {
// Reads HTML for usemin blocks to enable smart builds that automatically
// concat, minify and revision files. Creates configurations in memory so
// additional tasks can operate on them
options: {
dest: '<%= config.dist %>'
},
html: '<%= config.app %>/index.html'
};
// tasks/grunt/options/usemin.js:
module.exports = {
// Performs rewrites based on rev and the useminPrepare configuration
options: {
assetsDirs: ['<%= config.dist %>', '<%= config.dist %>/images']
},
html: ['<%= config.dist %>/{,*/}*.html']
};
// here is my build task. useminPrepare is included in 'concurrent:dist'
module.exports = function(grunt) {
grunt.registerTask('build', [
'clean:dist',
'includereplace',
'jshint',
'modernizr',
'concurrent:dist',
'copy',
'concat',
'uglify',
'usemin'
]);
};
// The preceding configuration gives me the correct useminPrepare output:
// Running "useminPrepare:html" (useminPrepare) task
// Going through app/index.html to update the config
// Looking for build script HTML comment blocks
// Configuration is now:
{
concat: {
files: {
'.tmp/concat/scripts/head-scripts.js': [
'app/scripts/vendor/modernizr.js'
],
'.tmp/concat/scripts/app-prod.js': [
'app/scripts/main.js',
'app/scripts/app.js'
]
},
generated: {
files: [
{
dest: '.tmp/concat/scripts/head-scripts.js',
src: [
'app/scripts/vendor/modernizr.js'
]
},
{
dest: '.tmp/concat/scripts/app-prod.js',
src: [
'app/scripts/main.js',
'app/scripts/app.js'
]
}
]
}
}
uglify: {
files: {
'<%= config.dist %>/concat/scripts/head-scripts.js': [
'.tmp/concat/scripts/head-scripts.js'
],
'<%= config.dist %>/concat/scripts/app-prod.js': [
'.tmp/scripts/app-prod.js'
]
},
generated: {
files: [
{
dest: 'dist/scripts/head-scripts.js',
src: [
'.tmp/concat/scripts/head-scripts.js'
]
},
{
dest: 'dist/scripts/app-prod.js',
src: [
'.tmp/concat/scripts/app-prod.js'
]
}
]
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment