npm install gulp-multi-thread-task --save-dev
gulp-multi-thread-task is a simple and easy way to transform a gulp task into a task which is runs on multiple threads
Benefits
- Blazing Fast - non blocking file processing, threads will always be provided files until all files have been processed.
- Optimized - seamlessly add multi-core support to a gulp task, will only spawn a thread once and reuse the same thread unlike gulp-multi-process which spawns a new thread per task.
- Simple - Easy to transform a existing task into a multi-threaded task.
node is a single thread, and almost all machines have multiple cores, this plugin allows your gulp task run time to be drastically improved by magnitudes by the number of threads used.
const gulp = require('gulp')
const imagemin = require('gulp-imagemin');
const texturePacker = require('gulp-free-tex-packer');
const {GulpMultiThreadTask} = require('gulp-multi-thread-task');
function buildSprites(done, src) {
return gulp.src(src)
.pipe(texturePacker()
.pipe(imagemin())
.pipe(gulp.dest());
}
function gulpBuildSprites(done, gameName = userOptionGameName) {
return GulpMultiThreadTask(['assets/**/*.*(svg|png|jpg|jpeg)'], buildSprites);
}
// example multiple glob array to batch file process
// you can batch a group of files together to create a single stream process
function gulpBuildSpritesGames {
return GulpMultiThreadTask('build:sprites', [
['assets/**/game1/**/*.*(svg|png|jpg|jpeg)'],
['assets/**/game1/**/*.*(svg|png|jpg|jpeg)']
], buildSprites
);
}
gulp.task('build:sprites', gulpBuildSpritesGames)
GulpMultiThreadTask(taskName, globArray, gulpFunction, options);
Type: string
The registered task to multi-thread
Type: Array
The glob pattern of file that will be processed by gulpFunction
Type: Function
gulp style function which will run on multiple threads, required parameters gulpFunction(done, src)
Type: number
Default: Math.min(os.cpus().length / 2, processedGlobArray.length)
The number of threads to use.
Type: boolean
Default: false
Whether or not to send output to parent's stdio