gpt4 book ai didi

javascript - gulp 任务可以组合起来更快吗?

转载 作者:行者123 更新时间:2023-11-28 01:03:24 24 4
gpt4 key购买 nike

我有一个刚刚写的 gulp 文件,我对此是新手。它有效,但我的任务比我认为需要的要多。

任何人都可以帮我将 javascript 任务串在一起作为一个任务吗?

完成后我需要四个单独的 js 文件,所有这些文件都被丑化了。

gulpfile.js 中的相关代码片段:

    var gulp = require('gulp')
uglify = require('gulp-uglify'),
rename = require('gulp-rename'),
concat = require('gulp-concat'),
notify = require('gulp-notify'),
cache = require('gulp-cache'),
del = require('del');


gulp.task('clean', function(cb) {
del(['css/*', 'js/min/*'], cb)
});

gulp.task('featuretest', function() {
return gulp.src('js/feature-test.js')
.pipe(rename({suffix: '.min'}))
.pipe(uglify())
.pipe(gulp.dest('js/min'))
});

// This file: '/js/excanvas.min.js' is only loaded via lte IE8 conditional statement
gulp.task('excanvas', function() {
return gulp.src('js/polyfills/excanvas.js')
.pipe(rename({suffix: '.min'}))
.pipe(uglify())
.pipe(gulp.dest('js/min'))
});

// This file: '/js/charts.min.js' is only used on very few pages
gulp.task('charts', function() {
return gulp.src(['js/highcharts-4.0.1.js', 'js/usfa-theme.js'])
.pipe(concat('charts.js'))
.pipe(rename({suffix: '.min'}))
.pipe(uglify())
.pipe(gulp.dest('js/min'))
});

// This file: '/js/main.min.js' is a concat of 'libs/jquery', a few polyfills (except excanvas.js and the highcharts)
gulp.task('scripts', function() {
return gulp.src(['js/libs/*.js', 'js/plugins/*.js', 'js/polyfills/*.js', '!js/excanvas.js', '!js/highcharts-4.0.1.js', 'js/custom.js'])
.pipe(concat('main.js'))
.pipe(rename({suffix: '.min'}))
.pipe(uglify())
.pipe(gulp.dest('js/min'))

.pipe(notify({ message: 'Scripts task complete' }));
});

gulp.task('watch', function() {

// Watch .js files
gulp.watch('js/**/*.js', ['featuretest', 'excanvas', 'charts', 'scripts']);

});



gulp.task('default', ['clean'], function() {
gulp.start('featuretest', 'excanvas', 'charts', 'scripts');
});

我想做什么:

    var gulp = require('gulp')
uglify = require('gulp-uglify'),
rename = require('gulp-rename'),
concat = require('gulp-concat'),
notify = require('gulp-notify'),
cache = require('gulp-cache'),
del = require('del');


gulp.task('clean', function(cb) {
del(['css/*', 'js/min/*'], cb)
});

gulp.task('scripts', function() {
// This file: '/js/feature-test.js' is loaded in the doc <head>
return gulp.src('js/feature-test.js')
.pipe(rename({suffix: '.min'}))
.pipe(uglify())
.pipe(gulp.dest('js/min'))

// This file: '/js/excanvas.min.js' is only loaded via lte IE8 conditional statement at the end of the doc
return gulp.src('js/polyfills/excanvas.js')
.pipe(rename({suffix: '.min'}))
.pipe(uglify())
.pipe(gulp.dest('js/min'))

// This file: '/js/charts.min.js' is only used on very few pages and is loaded only when needed at the end of the doc
return gulp.src(['js/highcharts-4.0.1.js', 'js/usfa-theme.js'])
.pipe(concat('charts.js'))
.pipe(rename({suffix: '.min'}))
.pipe(uglify())
.pipe(gulp.dest('js/min'))

// This file: '/js/main.min.js' is a concat of 'libs/jquery', a few polyfills (except excanvas.js and the high charts), it is loaded at the end of every doc

return gulp.src(['js/libs/*.js', 'js/plugins/*.js', 'js/polyfills/*.js', '!js/excanvas.js', '!js/highcharts-4.0.1.js', 'js/custom.js'])
.pipe(concat('main.js'))
.pipe(rename({suffix: '.min'}))
.pipe(uglify())
.pipe(gulp.dest('js/min'))

.pipe(notify({ message: 'Scripts task complete' }));
});

gulp.task('watch', function() {

// Watch .js files
gulp.watch('js/**/*.js', ['scripts']);

});



gulp.task('default', ['clean'], function() {
gulp.start('scripts');
});

最佳答案

如果您问的问题如何将单独的操作组合到单个任务中,您可以使用 gulp-util 组合流。

var gulp = require('gulp')
uglify = require('gulp-uglify'),
util = require('gulp-util');

gulp.task('scripts', function() {
var featureTest = gulp.src('js/feature-test.js')...

var excanvas = gulp.src('js/polyfills/excanvas.js')...

var charts = gulp.src(['js/highcharts-4.0.1.js', 'js/usfa-theme.js'])...

var scripts = gulp.src(['js/libs/*.js', 'js/plugins/*.js', 'js/polyfills/*.js', '!js/excanvas.js', '!js/highcharts-4.0.1.js', 'js/custom.js'])...

// combine streams
return util.combine(featureTest, excanvas, charts, scripts);
});

这将为您提供一个任务,但速度并没有更快。如果你不强制事情按顺序进行,那么 gulp 将尽可能快。

关于javascript - gulp 任务可以组合起来更快吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25356057/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com