【问题标题】:Can a gulp plugin take in one file and return many files?gulp 插件可以接收一个文件并返回多个文件吗?
【发布时间】:2014-05-21 13:51:57
【问题描述】:

我正在尝试将RequireJS Optimizer 包装在一个 gulp 插件中。有 have been other attempts 在 gulp 中提供此功能,但它们都不能满足我的需求。

插件开发的一种常见方法是获取文件,处理它们,然后将它们分发出去;但是,这种方法与 RequireJS 优化器的工作方式并不真正兼容。

虽然它不符合 gulp 插件 guidelines,但我正在研究一种不同的方法来接收 optimizer build configuration、处理它并分发结果文件。

// Read in the r.js configuration
gulp.src( './build.js' )
    // Optimize the files according to the config
    .pipe( optimize() )
    // Return the resulting files for additional processing
    .pipe( size() );

gulp 插件可以接收一个文件并返回多个文件吗?如果有,怎么做?

【问题讨论】:

    标签: javascript node.js gulp r.js


    【解决方案1】:
    1. 是的,只需发出多个文件。输出只需一个或多个 Vinyl 文件即可。
    2. 插件只接收通过管道(发送)到它们的内容。这就是使用异步管道的全部意义所在。

    查看the plugin docs 以了解它们的工作原理。

    【讨论】:

    • 谢谢。我会调查的。
    【解决方案2】:

    我最终使用vinyl-fs 传递优化过程的输出。

    var requirejs   = require( 'requirejs' ),
        through     = require( 'through2' ),
        vfs         = require( 'vinyl-fs' );
    
    return through.obj( function( file, encoding, done ){
    
    // Parse config from file
    
    requirejs.optimize( config,
    
        // Success callback
        function( buildResponse ){
            vfs.src( config.dir + '/**/*.js' )
                .on( 'data', this.push.bind( this ) )
                .on( 'error', this.emit.bind( this, 'error' ) )
                .on( 'end', done );
        }.bind( this ),
    
        // Error callback
        function( buildError ){
            // Handle error
        } );
    

    用法最终看起来像......

    // Read in build configuration
    gulp.src( 'build.js' )
        // Run files from the config through the optimizer
        .pipe( optimize( settings ) )
        // Measure the size of the optimized files
        .pipe( size() );
    

    【讨论】:

    • 我正在努力解决同样的问题。你碰巧有更具体的例子吗?就像这是如何通过管道传输到 fe。 gulp.dest() 处理程序?另外,“回调”指的是什么?
    • 我添加了更多细节。鉴于插件的作用,我不需要使用gulp.dest(),但我看不出你不能使用的原因。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-26
    • 1970-01-01
    • 2011-07-02
    • 1970-01-01
    • 1970-01-01
    • 2020-06-01
    • 1970-01-01
    相关资源
    最近更新 更多