javascript Node.js:如何将全局变量传递到通过 require() 插入的文件中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27007445/
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 06:49:09  来源:igfitidea点击:

Node.js: how do I pass global variable into a file being inserted through require()?

javascriptnode.jsnpmrequiregulp

提问by BuildTester1

I've split my gulpfile.jsinto several files in a /gulpfolder to organize the code a little better. But now I want to pass a variable debug (boolean)into the files that will switch behaviour of the gulp command being included (eventually I will use command-line arguments, but for now I just want to make it work with a variable).

我已将我的gulpfile.js文件拆分为一个/gulp文件夹中的几个文件,以便更好地组织代码。但是现在我想将一个变量传递debug (boolean)到文件中,该文件将切换包含的 gulp 命令的行为(最终我将使用命令行参数,但现在我只想让它与变量一起工作)。

The way I've got this setup, using a method I saw in a yeoman angular/gulp package, is using an npm module called require-dir(which loads all *.js files in the /gulpfolder, where each has a set of gulp tasks that are set).

我使用我在 yeoman angular/gulp 包中看到的方法进行此设置的方式是使用名为的 npm 模块require-dir(它加载文件/gulp夹中的所有 *.js 文件,其中每个文件都有一组 gulp 任务放)。

gulpfile.js:

gulpfile.js:

var gulp = require('gulp'),
  run = require('run-sequence'),
  debug = true;

require('require-dir')('./gulp');

gulp.task('default', ['build', 'server', 'watch', '...']);

Which would load something like...

哪个会加载类似...

gulp/build.js:

吞咽/build.js:

var gulp = require('gulp'), 
  plumber = require('gulp-plumber'),
  ...;

gulp.task('build', function () {
  console.log(debug);
});

So when I run command gulp, which runs the defaulttask, it will load build.js and then execute the buildgulp task. But unfortunately, it seems that debugreturns undefined.

因此,当我运行gulp运行default任务的command 时,它将加载 build.js 然后执行 gulpbuild任务。但不幸的是,似乎debug返回未定义。

How could I get this to work?

我怎么能让这个工作?

I was looking at using just module.exports()and node's require()method, but I couldn't figure out how to get the gulp task inside the included file to declare, so that it could then be run from the main gulpfile.jsfile (in the desired sequence).

我正在考虑使用 justmodule.exports()和 node 的require()方法,但我无法弄清楚如何在包含的文件中声明 gulp 任务,以便它可以从主gulpfile.js文件中运行(以所需的顺序)。

Can anyone offer some assistance? Thanks

任何人都可以提供一些帮助吗?谢谢

采纳答案by Mike 'Pomax' Kamermans

The normal module way, really. Just change that gulp/build.jsfile from not actually exporting anything to a proper require-able module:

正常的模块方式,真的。只需将该gulp/build.js文件从不实际导出任何内容更改为适当的可要求模块:

module.exports = function(debug) {
  "use strict";

  var gulp = require('gulp'), 
      plumber = require('gulp-plumber'),
      ...;

  gulp.task('build', function () {
    console.log(debug);
  });
};

and then call it in your main file as:

然后在你的主文件中调用它:

...
var loadGulp = require('require-dir/gulp');
...
var debug = true;
loadGulp(debug);

回答by Andrew Miner

Node.js offers a single global variable named globalwhich is, in fact, the same instance in all modules (unlike modulewhich is different in each module). By setting values on globalthey become truly global. As an added bonus, you don't need the prefix access to global variables with global.. Both global.fooand just fooare equivalent so long as you don't have another variable named fooin scope.

Node.js 提供了一个名为的全局变量global,它实际上在所有模块中都是相同的实例(不同于module每个模块中的不同)。通过设定价值,global它们变得真正全球化。作为额外的好处,您不需要使用global.. 只要您没有在作用域中命名的另一个变量,两者global.foo和 justfoo都是等价的foo

回答by Barryman9000

I just create one object with all variables in it that I export as a module so I can use it throughout all my task files. For example

我只是创建了一个包含所有变量的对象,并将其导出为一个模块,这样我就可以在我的所有任务文件中使用它。例如

js/tasks/variables.js

js/任务/变量.js

module.exports = {
    myBool: true
};

js/tasks/sass.js

js/任务/sass.js

var gulp = require('gulp'),
    $ = require('gulp-load-plugins')(),
    log = $.util.log,
    vars = require('./variables');

gulp.task('sass', function () {
    log('myBool: ' + vars.myBool); // logs "myBool: true"
    vars.myBool = false;
});

js/tasks/build.js

js/任务/build.js

var gulp = require('gulp'),
    $ = require('gulp-load-plugins')(),
    log = $.util.log,
    vars = require('./variables');

gulp.task('build', function () {
    log('myBool: ' + vars.myBool); // logs "myBool: false"
});

/gulpfile.js

/gulpfile.js

Then you can get/set those variables from anywhere:

然后你可以从任何地方获取/设置这些变量:

var gulp = require('gulp'),
    $ = require('gulp-load-plugins')(),
    runSequence = require('run-sequence'),
    vars = require('./js/tasks/variables'),
    requireDir = require('require-dir')('./js/tasks');

gulp.task('production', function(){
    runSequence('sass', 'build');
});