根据NODE_ENV设置gulp任务

时间:2021-10-09 23:06:06

Is there a way to specify a gulp task depending on the NODE_ENV that is set?

有没有办法根据设置的NODE_ENV指定gulp任务?

For example in my package.json file, I have something like:

例如在我的package.json文件中,我有类似的东西:

"scripts": {
    "start": "gulp"
 }

And I have multiple gulp tasks

我有多个gulp任务

gulp.task('development', function () {
   // run dev related tasks like watch 
});

gulp.task('production', function () {
   // run prod related tasks
});

If I set NODE_ENV=production npm start, can I specify to only run gulp production? Or is there a better way to do this?

如果我设置NODE_ENV =生产npm start,我可以指定只运行gulp生产吗?或者有更好的方法吗?

4 个解决方案

#1


13  

Using a single ternary in your default gulp task, you can have something like:

在默认的gulp任务中使用单个三元组,您可以使用以下内容:

gulp.task('default',
  [process.env.NODE_ENV === 'production' ? 'production' : 'development']
);

You will then be able to keep the single gulp command in your package.json and using this like you said:

然后,您将能够在package.json中保留单个gulp命令,并像您说的那样使用它:

NODE_ENV=production npm start

Any other value of your NODE_ENV variable will launch the development task.

NODE_ENV变量的任何其他值都将启动开发任务。


You could of course do an advanced usage using an object allowing for multiple tasks and avoiding if trees hell:

您当然可以使用允许多个任务的对象进行高级使用,并避免树木地狱:

var tasks = {
  development: 'development',
  production: ['git', 'build', 'publish'],
  preprod: ['build:preprod', 'publish:preprod'],
  ...
}

gulp.task('default', tasks[process.env.NODE_ENV] || 'fallback')

Keep in mind that when giving an array of tasks, they will be run in parallel.

请记住,在提供一系列任务时,它们将并行运行。

#2


2  

Have your first gulp task run other gulp tasks based on the process.env.NODE_ENV value.

让您的第一个gulp任务根据process.env.NODE_ENV值运行其他gulp任务。

gulp.task('launcher', function(){
  switch (process.env.NODE_ENV){
    case 'development':
      // Run dev tasks from here
      break;
    case 'production':
      // Run prod tasks
      break;
  }
});

#3


1  

The other simple way could be

另一种简单的方法可能是

gulp.task('set-dev-env', function () {
    return process.env.NODE_ENV = 'development';
});

gulp.task('set-prod-env', function () {
    return process.env.NODE_ENV = 'production';
});

gulp.task('development', ['set-dev-env'], function () {
    // your code
});

gulp.task('production', ['set-prod-env'], function () {
    // your code
});

Run gulp production or gulp development.

运行gulp生产或gulp开发。

#4


-3  

if (process.env.NODE_ENV === "production")
  // whatever

#1


13  

Using a single ternary in your default gulp task, you can have something like:

在默认的gulp任务中使用单个三元组,您可以使用以下内容:

gulp.task('default',
  [process.env.NODE_ENV === 'production' ? 'production' : 'development']
);

You will then be able to keep the single gulp command in your package.json and using this like you said:

然后,您将能够在package.json中保留单个gulp命令,并像您说的那样使用它:

NODE_ENV=production npm start

Any other value of your NODE_ENV variable will launch the development task.

NODE_ENV变量的任何其他值都将启动开发任务。


You could of course do an advanced usage using an object allowing for multiple tasks and avoiding if trees hell:

您当然可以使用允许多个任务的对象进行高级使用,并避免树木地狱:

var tasks = {
  development: 'development',
  production: ['git', 'build', 'publish'],
  preprod: ['build:preprod', 'publish:preprod'],
  ...
}

gulp.task('default', tasks[process.env.NODE_ENV] || 'fallback')

Keep in mind that when giving an array of tasks, they will be run in parallel.

请记住,在提供一系列任务时,它们将并行运行。

#2


2  

Have your first gulp task run other gulp tasks based on the process.env.NODE_ENV value.

让您的第一个gulp任务根据process.env.NODE_ENV值运行其他gulp任务。

gulp.task('launcher', function(){
  switch (process.env.NODE_ENV){
    case 'development':
      // Run dev tasks from here
      break;
    case 'production':
      // Run prod tasks
      break;
  }
});

#3


1  

The other simple way could be

另一种简单的方法可能是

gulp.task('set-dev-env', function () {
    return process.env.NODE_ENV = 'development';
});

gulp.task('set-prod-env', function () {
    return process.env.NODE_ENV = 'production';
});

gulp.task('development', ['set-dev-env'], function () {
    // your code
});

gulp.task('production', ['set-prod-env'], function () {
    // your code
});

Run gulp production or gulp development.

运行gulp生产或gulp开发。

#4


-3  

if (process.env.NODE_ENV === "production")
  // whatever