一个gulp插件可以接收一个文件并返回多个文件吗?

我试图将RequireJS优化器封装在一个gulp插件中。 还有其他一些 尝试提供这种function的吞咽,但没有一个满足我的需求。

插件开发的一个常见方法是把文件join,处理它们,然后把它们交出来。 但是这种方法与RequireJS优化器的工作方式并不完全一致。

虽然它不符合gulp插件的指导原则 ,但我正在研究一种采用优化器构buildconfiguration ,处理它并交付结果文件的不同方法。

// 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插件可以接收一个文件并返回多个文件吗? 如果是这样,怎么样?

  1. 是的,只需发出多个文件。 输出只需要一个或多个乙烯基文件。
  2. 插件只接收pipe道(发射)给他们的东西。 这就是使用asynchronouspipe道的重点。

看一下插件文档 ,了解它们是如何工作的。

我结束了使用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() ); 
Interesting Posts