gpt4 book ai didi

Gulp:将参数从 watch 声明传递给任务

转载 作者:行者123 更新时间:2023-12-03 11:29:46 25 4
gpt4 key购买 nike

问题:我想维护文件的“集合”。这将有助于缩短构建时间和灵活性。例如,每次我编辑 app.js 文件时,我都不想重新编译我所有的 twitter bootstrap 文件。

我当然可以通过 2 个任务和 2 个监视声明来实现这一点 - 问题是除了文件数组之外,这些任务是相同的。理想情况下,我想在 watch 声明中将这些作为参数传递有没有办法做类似以下伪代码的事情?:

var files = {
scripts: [
'www/assets/scripts/plugins/**/*.js',
'www/assets/scripts/main.js',
],
vendor: [
'vendor/jquery/dist/jquery.js',
'vendor/jqueryui/ui/jquery.ui.widget.js',
'vendor/holderjs/holder.js'
],
};
...


gulp.task('js', ['lint'], function (files, output) {
return gulp.src(files)
.pipe(debug())
.pipe(concat(output))
.pipe(uglify({outSourceMap: true}))
.pipe(gulp.dest(targetJSDir))
.pipe(notify('JS minified'))
.on('error', gutil.log)
});

...

gulp.watch('scripts/**/*.js', ['lint', 'js'], files.scripts, 'app.min.js');
gulp.watch('vendor/**/*.js', ['lint', 'js'], files.vendor, 'vendor.min.js');

另一种方式:是命名空间调用任务的 watch 声明?这样我就可以检查哪个 watch 触发了任务,并在任务本身中对这些事情进行了条件化。

最佳答案

the problem is that the tasks are identical save for the files array.



我相信 lazypipe (see its gh page)很好
适合您的需求。这是一个有趣的问题。我将尝试回答我认为您要问的问题(lazypipe 对此感到满意)以及我认为您可能正在考虑或最终会考虑的问题,如果您通过了管道的参数化问题。

我们想要的一方面是我们不想在未更改的文件上重新运行 jshint。此外,我们希望保持 DRY,并且除了更改的文件之外,我们还希望获取新文件。

这已经过测试并且对我有用:
var gulp = require('gulp');
var $ = require('gulp-load-plugins')();
var es = require('event-stream');
var lazypipe = require('lazypipe');
var gutil = require('gulp-util');
var path = require('path');

var files = {
scripts: ['src/**/*.js'],
vendor: ['vendor/**/*.js']
};

// sets up a lazy pipe that does jshint related stuff
function getJsMultiPipe(name) {
return lazypipe()
.pipe($.jshint)
.pipe($.jshint.reporter, 'jshint-stylish')

// if you don't want to fail on style errors remove/comment this out:
.pipe($.jshint.reporter, 'fail');
}

// sets up a lazy pipe that does concat and post-concat stuff
function getJsCombinedPipe(groupName, outfile) {
return lazypipe()
.pipe($.concat, outfile)
.pipe($.uglify, {outSourceMap: true})
.pipe(gulp.dest, 'build')
.pipe($.notify, {message: groupName + ' JS minified', onLast: true});
}

// sets up a pipe for the initial build task, combining the above two pipes
function getBuildPipe(groupName, outfile) {
return gulp.src(files[groupName])
.pipe(getJsMultiPipe(groupName)())
.pipe(getJsCombinedPipe(groupName, outfile)());
}

// sets up a watch pipe, such that only the changed file is jshinted,
// but all files are included in the concat steps
function setWatchPipe(groupName, outfile) {
return $.watch({
glob: files[groupName],
name: groupName,
emitOnGlob: false,
emit: 'one'
}, function(file, done) {
return file
.pipe($.debug({title: 'watch -- changed file'}))
.pipe(getJsMultiPipe(groupName)())
// switch context
.pipe(gulp.src(files[groupName]))
.pipe($.debug({title: 'watch -- entire group'}))
.pipe(getJsCombinedPipe(groupName, outfile)())
.pipe($.debug({title: 'watch -- concatted/source-mapped'}))
.pipe($.notify({message: 'JS minified', onLast: true}));
});
}

// task to do an initial full build
gulp.task('build', function() {
return es.merge(
getBuildPipe('scripts', 'app.min.js'),
getBuildPipe('vendor', 'vendor.min.js')
)
.pipe($.notify({message: 'JS minified', onLast: true}));
});


// task to do an initial full build and then set up watches for
// incremental change
gulp.task('watch', ['build'], function(done) {
setWatchPipe('scripts', 'app.min.js');
setWatchPipe('vendor', 'vendor.min.js');
done();
});

我的依赖项如下所示:
  "devDependencies": {
"jshint-stylish": "^0.1.5",
"gulp-concat": "^2.2.0",
"gulp-uglify": "^0.2.1",
"gulp-debug": "^0.3.0",
"gulp-notify": "^1.2.5",
"gulp-jshint": "^1.5.3",
"gulp": "^3.6.0",
"gulp-load-plugins": "^0.5.0",
"lazypipe": "^0.2.1",
"event-stream": "^3.1.1",
"gulp-util": "^2.2.14",
"gulp-watch": "^0.5.3"
}

编辑 :我只是再次瞥了一眼,我注意到以下几行:
    // switch context
.pipe(gulp.src(files[groupName]))

请注意,我相信 gulp.src API 自从我写这篇文章以来已经发生了变化,并且当您将内容通过管道传输到 gulp.src 时,它目前不会切换上下文,因此该位置可能需要更改。对于较新版本的 gulp,我认为将会发生的情况是您将添加到上下文中,而可能会降低一点效率。

关于Gulp:将参数从 watch 声明传递给任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22563539/

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