Grunt 在任务执行之间执行自定义脚本
Grunt execute custom script between task execution
是否可以在 g运行t 任务中执行以下操作?
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'
]);
});
基本上,我需要一个节点模块,该模块具有加载一些 .html
文件并替换其中一些引用的功能。这个想法是能够在两组任务之间做到这一点。我已经测试过了,似乎我的自定义函数在 g运行t 任务 运行.
之前执行
这是 changeref.js
模块:
'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());
};
如何按照示例中描述的顺序进行操作?
你的问题是 grunt.task.run 没有 运行 任务,它只是将它们添加到当前任务完成后要执行的任务堆栈中。所以你的代码作为当前任务的一部分被执行,然后才执行所有其他任务 运行.
为了实现你的目标,只需将你的代码变成你自己的任务(这很轻松),然后按顺序调用它们:
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']);
是否可以在 g运行t 任务中执行以下操作?
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'
]);
});
基本上,我需要一个节点模块,该模块具有加载一些 .html
文件并替换其中一些引用的功能。这个想法是能够在两组任务之间做到这一点。我已经测试过了,似乎我的自定义函数在 g运行t 任务 运行.
这是 changeref.js
模块:
'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());
};
如何按照示例中描述的顺序进行操作?
你的问题是 grunt.task.run 没有 运行 任务,它只是将它们添加到当前任务完成后要执行的任务堆栈中。所以你的代码作为当前任务的一部分被执行,然后才执行所有其他任务 运行.
为了实现你的目标,只需将你的代码变成你自己的任务(这很轻松),然后按顺序调用它们:
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']);