簡體   English   中英

在任務執行之間仔細執行自定義腳本

[英]Grunt execute custom script between task execution

在艱巨的任務中可以執行以下操作嗎?

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文件並替換其中的一些引用。 想法是能夠在兩組任務之間執行此操作。 我已經測試過了,看來我的自定義功能在運行grunt任務之前就已經執行了。

這是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']);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM