1

Gruntjs を使用して Joomla のテンプレートを処理しています! 拡張機能。

メイン ディレクトリ内に 3 つのディレクトリと Grunt ファイルがあります: componenttmpltasksおよび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 の初心者です。

4

1 に答える 1

1

compile を 2 回定義してタスク名を上書きしたような気がします。デフォルトのタスクを使用してコードをテストしたところ、次のようになりました。

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

に配列をvar src渡しています。カスタム関数に配列を渡したい場合は、それらを反復処理することができます。それ以外の場合は、1 つのパスのみを使用している場合は、var src = file.src.toString();.

次に、registerTask を変更して別の名前を使用すると、すべての設定が完了します。

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