Browserify api:如何将高级选项传递给脚本

Browserify api: how to pass advanced option to script

我正在使用 Browserify API 编写一个小脚本并遵循自述文件 here

一切正常,除了当我需要传递 advanced options 下列出的标志时,API 似乎没有覆盖它。我想传递的是 --node 标志。

var b = browserify({
entries: ['src/index.js'],
  cache: {},
  ignoreWatch: ['**/node_modules/**', './dist/'],
  packageCache: {},
  plugin: [watchify, babelify],
  debug: true,
  node: true // => this options does not actually exist, so it does nothing
});

b.on('update', bundle);

function bundle() {
  b.bundle()
    .pipe(fs.createWriteStream('bundle.js'));
}

在命令行中,这将转换为 watchify src/index.js --node -o bundle.js(并且有效)。

我认为文档中的那一行是:

All other options are forwarded along to module-deps and browser-pack directly.

可能包含一些帮助,但我不清楚如何帮助。对此有任何指示吗?

在查看了 Browserify 的源代码后,我找到了自己问题的答案。

我 运行 watchify src/index.js --node -o bundle.js 命令并记录传递给 Browserify.prototype._createPipeline 函数的选项。

在我的例子中,代码将变为:

var b = browserify({
entries: ['src/index.js'],
  cache: {},
  ignoreWatch: ['**/node_modules/**', './dist/'],
  packageCache: {},
  plugin: [watchify, babelify],
  debug: true,
  fullPaths: false,
  builtins: false,
  commondir: false,
  bundleExternal: true,
  basedir: undefined,
  browserField: false,
  detectGlobals: true,
  insertGlobals: false,
  insertGlobalVars:
   { process: undefined,
     global: undefined,
     'Buffer.isBuffer': undefined,
     Buffer: undefined },
  ignoreMissing: false,
  standalone: undefined
});

我最终从我的脚本中删除了其中一些选项,因为大多数只是默认值,但我会将它们留在答案中以供将来参考,希望其他人无论如何都会从中受益。