Reputation: 15319
Is it possible to do the following in a grunt task?
grunt.registerTask('build', 'Building app', function () {
grunt.task.run([
'clean:dist',
'wiredep',
'useminPrepare',
'ngtemplates',
'concat:generated',
'ngAnnotate',
'copy:dist',
'cdnify',
'cssmin'
]);
// Replace "vendor-*.js" references to "vendor.js"
require('./custom_modules/changeref/changeref')(grunt, this.async, {
filePath: './dist/*.html',
find: /(vendor-).*\.js/ig,
replaceBy: 'vendor.js'
});
grunt.task.run([
'uglify',
'filerev',
'usemin',
'htmlmin'
]);
});
Basically, I require a node module with a function that load some .html
files and replace some references inside them. The idea is to be able to do this between the two groups of tasks. I have tested it and it seems my custom function gets executed BEFORE the grunt tasks are run.
This is the changeref.js
module:
'use strict';
var path = require('path');
module.exports = function(grunt, async, options) {
var files;
grunt.verbose.writeln('Checking options...');
options = options || {
filePath: options.filePath || './*.html',
find: options.find,
replaceBy: options.replaceBy || ''
};
if ( !options ) { throw new Error('options is undefined'); }
if ( !options.find ) { throw new Error('options.find is undefined'); }
grunt.verbose.writeflags(options, 'Running changeref with options: ');
files = grunt.file.expand({ filter: 'isFile' }, options.filePath);
files = files.map(function (fp) {
return { path: fp, body: grunt.file.read(fp) };
});
if ( files.length ) {
grunt.verbose.writeln('Your "filePath" pattern has found ' + files.length + ' file(s).');
} else {
grunt.verbose.warn('Not a single file was found.');
}
// File iteration
// files.forEach(function (file, index) {
grunt.util.async.forEach(files, function (file, cbInner) {
grunt.verbose.writeln('Processing ' + file.path + '...');
var fileContent,
strFound = function () {
var match;
// The find patter is a REGEXP
if ( typeof options.find === "object" ) { match = file.body.match(options.find); }
// The find pattern is a string
else { match = file.body.indexOf(options.find); }
if ( match && match.length ) {
return ((match.length !== 0) || (match !== -1));
}
return false;
};
if ( !strFound() ) {
grunt.verbose.warn("Your pattern hasn't match anything and the current file will remain untouched.");
return;
}
fileContent = file.body.replace(options.find, options.replaceBy);
grunt.verbose.writeln('Preparing to write file ' + file.path);
// Write the destination file.
grunt.file.write(file.path, fileContent);
cbInner();
}, async());
};
How can I follow the order described in my example?
Upvotes: 1
Views: 1513
Reputation: 2121
Your issue is that grunt.task.run does NOT run the tasks, it just adds them to the stack of tasks to be executed once the current task is finished. So your code gets executed as part of the current task, and only then do all the other tasks run.
To achieve your goal, just turn your code into your own task (it's pretty painless) and just call them in sequence:
grunt.registerTask("yourReplace", function() {
// Replace "vendor-*.js" references to "vendor.js"
require('./custom_modules/changeref/changeref')(grunt, this.async, {
filePath: './dist/*.html',
find: /(vendor-).*\.js/ig,
replaceBy: 'vendor.js'
});
});
grunt.registerTask("build", ['clean:dist', 'cssmin', 'yourReplace', 'uglify']);
Upvotes: 4