如何创buildGrunt Multitask来处理我的模板
我正在使用Gruntjs为我的Joomla处理模板! 扩展。
我有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的新手。
我的感觉是,你通过定义两次编译来覆盖任务名称。 我使用默认的任务testing了你的代码,得到这个:
$ grunt Running "compile:model" (compile) task Warning: Arguments to path.join must be strings Use --force to continue.
你在var src
传递一个数组,如果你想传递一个数组到你的自定义函数,你可能需要遍历这些数组。 否则,如果你只是使用一个path,做var src = file.src.toString();
。
然后改变你的registerTask使用不同的名字,你应该全部设置:
grunt.registerTask('docompile', ['compile']);
- 如何运行grunt docular-server?
- Bootstrap LESS – Grunt手表输出到特定的目录
- Grunt-connect-proxy,转发文件不起作用
- Grunt – 同时执行和观看应用程序
- Grunt – 如何更新js文件中使用gruntcaching胸围的HTML文件引用?
- 无法使用Grunt编译Bootstrap 3 LESS主文件(bootstrap.less)?
- grunt-newer将无法正常使用Watchify
- npm WARN弃用lodash@2.4.2:lodash @ <3.0.0不再维护
- gruntjs minify / uglify后,在html中更改链接或脚本文件名