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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 07:13:22  来源:igfitidea点击:

Is it possible to assign a variable in a gulp task before running dependencies?

javascriptnode.jsgulp

提问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 buildtask that calls a number of other tasks as dependencies (including this scriptstask for instance). I want this buildtask 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 .startmethod 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.runwas 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.envthis way.

我不同意@Justin。用任务定义环境变量是一个想法的黑客工作。用gutil.env这种方式更好。

gulp --env prod task

gulp.task( 'myTask', () => { console.log( gutil.env.env ) } )

Now from this point, you have gulp.env.envset.

从现在开始,您已经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-productiontask may be run after the other tasks. You can solve this problem with the run-sequencepackage.

这里的问题是您的任务将并行运行,这意味着该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-productiontask first, then run the scripts, styles, and otherstufftasks in parallel.

这将运行set-production第一个任务,然后再运行scriptsstyles以及otherstuff在并行任务。