1

我正在使用 Gruntjs 为我的 Joomla! 处理模板!扩展名。

我的主目录中有 3 个目录和一个 Grunt 文件:componenttmpl和.tasksGruntfile.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 的新手。

4

1 回答 1

1

我的感觉是您通过定义 compile 两次覆盖了任务名称。我使用默认任务测试了您的代码并得到了这个:

$ grunt
Running "compile:model" (compile) task
Warning: Arguments to path.join must be strings Use --force to continue.

您正在传递一个数组var src,如果您想将一个数组传递给您的自定义函数,您可能需要遍历这些数组。否则,如果您只使用一条路径,请执行var src = file.src.toString();.

然后更改您的 registerTask 以使用不同的名称,您应该全部设置好:

grunt.registerTask('docompile', ['compile']);
于 2013-09-28T15:46:38.490 回答