javascript 是否可以在运行依赖项之前在 gulp 任务中分配变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27253597/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible to assign a variable in a gulp task before running dependencies?
提问by wisew
I'm trying to conditionally pipe a file stream based on the value of a variable, as a way to define two separate build environments (ie. development and production).
我正在尝试根据变量的值有条件地通过管道传输文件流,作为定义两个独立构建环境(即开发和生产)的一种方式。
Some tasks can be run individually with a command-line flag like so:
gulp scripts --env production
某些任务可以使用命令行标志单独运行,如下所示:
gulp scripts --env production
And will then do some production-only pipeline steps:
然后将执行一些仅限生产的管道步骤:
gulp.task('scripts', function() {
var jsFilter = filter(['*.js']),
appFiles;
return gulp.src(appFiles)
.pipe(jsFilter)
.pipe(concat('application-build.js'))
.pipe(gulpif(env === 'production', uglify()))
.pipe(size())
.pipe(gulpif(env === 'production', gulp.dest('dist/js'), gulp.dest('tmp/js')))
.pipe(browserSync.reload({ stream: true }));
});
I have a build
task that calls a number of other tasks as dependencies (including this scripts
task for instance). I want this build
task to assign a variable (env
, in this case) beforerunning task dependencies. Which means that this:
我有一个build
任务将许多其他任务称为依赖项(例如,包括此scripts
任务)。我希望此build
任务在运行任务依赖项之前分配一个变量(env
在本例中为 )。这意味着:
gulp.task('build', ['scripts', 'styles', 'otherstuff'], function() {
env = 'production';
}
doesn't work, because the dependencies are run before the body of the task.
不起作用,因为依赖项在任务主体之前运行。
I currently have it implemented with gulp.start
:
我目前已经实现了gulp.start
:
gulp.task('build', function() {
env = 'production';
gulp.start('scripts');
});
But the .start
method isn't actually part of gulp's public API- it comes from Orchestrator- and isn't intended to be used for anything. Plus, the equivalent method gulp.run
was deprecated from the API awhile ago.
但该.start
方法实际上并不是 gulp 公共 API 的一部分——它来自 Orchestrator——并且不打算用于任何事情。另外,等价的方法gulp.run
不久前已从 API 中弃用。
So I'm wondering - is there another way I could assign a variable in a task before running its dependencies?
所以我想知道 - 有没有另一种方法可以在运行依赖项之前在任务中分配变量?
(Or maybe there's a better way to to implement something like build environments in gulp?)
(或者也许有更好的方法来实现诸如在 gulp 中构建环境之类的东西?)
采纳答案by Evan Carroll
THE RIGHT WAY
正确的方式
I disagree with @Justin. Defining an environmental variable with a task is a hackjob of an idea. This is better done with gutil.env
this way.
我不同意@Justin。用任务定义环境变量是一个想法的黑客工作。用gutil.env
这种方式更好。
gulp --env prod task
gulp.task( 'myTask', () => { console.log( gutil.env.env ) } )
Now from this point, you have gulp.env.env
set.
从现在开始,您已经gulp.env.env
设置了。
Or, alternatively you can do like this example in this ticket..which addresses this from the developers of Gulp which first suggest to use an environmental variable, but provide this idiom..
或者,你也可以像这张票中的这个例子一样......它从 Gulp 的开发人员那里解决了这个问题,他们首先建议使用环境变量,但提供了这个习惯用法......
function js(shouldMinify) {
return gulp.src('./js/*.js')
.pipe(concat('app.js'))
.pipe(gulpif(shouldMinify, uglify()))
.pipe(gulp.dest('js'));
});
gulp.task('develop', function () {
shouldMinify = false;
return js(shouldMinify);
});
gulp.task('build', function () {
shouldMinify = true;
return js(shouldMinify);
});
That same developer (phated)always says to use env...
这同开发商(phated)总是说,使用ENV ...
Not to mention, you should control this type of logic with environment variables or command line flags. - phated
更不用说,您应该使用环境变量或命令行标志来控制这种类型的逻辑。- 已化
Presumably, he's referring to the use of gutil.noop()
in gulp-util
's docs:
据推测,他指的是使用gutil.noop()
ingulp-util
的文档:
// gulp should be called like this :
// $ gulp --type production
gulp.task('scripts', function() {
gulp.src('src/**/*.js')
.pipe(concat('script.js'))
// LOOK BELOW: if we don't send to uglify, we push to noop stream.
.pipe(gutil.env.type === 'production' ? uglify() : gutil.noop())
.pipe(gulp.dest('dist/'));
});
回答by Justin Howard
You could create a task specifically to set the environment and run it before your other tasks.
您可以专门创建一个任务来设置环境并在其他任务之前运行它。
gulp.task('set-production', function() {
env = 'production';
});
// Doesn't quite work because tasks are run in parallel
gulp.task('build', ['set-production', 'scripts', 'styles', 'otherstuff']);
The problem here is that your tasks will be run in parallel, meaning the set-production
task may be run after the other tasks. You can solve this problem with the run-sequence
package.
这里的问题是您的任务将并行运行,这意味着该set-production
任务可能会在其他任务之后运行。你可以用run-sequence
包来解决这个问题。
var runSequence = require('run-sequence');
gulp.task('build', function(callback) {
runSequence('set-production', ['scripts', 'styles', 'otherstuff'], callback);
});
This will run the set-production
task first, then run the scripts
, styles
, and otherstuff
tasks in parallel.
这将运行set-production
第一个任务,然后再运行scripts
,styles
以及otherstuff
在并行任务。