1
我正在使用Gruntjs來處理我的Joomla模板!擴展。如何創建Grunt Multitask來處理我的模板
我在我的主目錄中有3個目錄和一個Grunt文件:component
,tmpl
,tasks
和Gruntfile.js
。
裏面的tasks
目錄我有一個文件名爲compile.js
:
module.exports = function(grunt) {
grunt.registerMultiTask('compile', 'Compiles Joomla! extension templates', function() {
// Iterate over all specified file groups.
this.files.forEach(function(file) {
var template, phpcode;
var src = file.src;
var dest = file.dest;
if (!grunt.file.exists(src)) {
grunt.log.warn('Source file "' + src + '" not found.');
return false;
}
template = grunt.file.read(src);
phpcode = grunt.template.process(template);
// Write the destination file.
grunt.file.write(dest, phpcode);
// Print a success message.
grunt.log.writeln('File "' + dest + '" created.');
});
});
};
我Gruntfile具有下面的代碼:
module.exports = function(grunt) {
grunt.initConfig({
compile: {
model: {
files: [
{src: 'tmpl/model.tmpl', dest: 'component/models/user.php'},
{src: 'tmpl/model.tmpl', dest: 'component/models/company.php'}
]
}
}
});
// Actually load this plugin's task.
grunt.loadTasks('tasks');
grunt.registerTask('compile', ['compile']);
};
當我運行命令grunt compile
node.js:893
var fn = runInThisContext(source, this.filename, true);
^
TypeError: undefined is not a function
at createWritableStdioStream (node.js:555:18)
at process.stdout (node.js:612:16)
at write (/home/qawe/Desktop/code/compile/node_modules/grunt/lib/grunt/log.js:78:12)
at writeln (/home/qawe/Desktop/code/compile/node_modules/grunt/lib/grunt/log.js:85:3)
at Object.log.writeln (/home/qawe/Desktop/code/compile/node_modules/grunt/lib/grunt/log.js:96:3)
at writeln (/home/qawe/Desktop/code/compile/node_modules/grunt/lib/grunt/fail.js:39:13)
at Object.fail.fatal (/home/qawe/Desktop/code/compile/node_modules/grunt/lib/grunt/fail.js:55:3)
at process.uncaughtHandler (/home/qawe/Desktop/code/compile/node_modules/grunt/lib/grunt.js:123:10)
at process.EventEmitter.emit (events.js:95:17)
at process._fatalException (node.js:272:26)
是有我在做的事情錯了嗎?請幫忙,我還是Grunt的新手。
感謝奔你是一個生命的救星,但我不明白爲什麼src爲陣列,同時在dest爲一個字符串。在我的Grantfile中,它們以相同的方式定義。 –
我認爲它與Grunt如何處理src/dest映射有關:通常的模式是一個/多個infiles,一個outfile(concat基本上是內置的)。 – Ben