【发布时间】:2017-02-21 07:52:53
【问题描述】:
我使用的一个工具以编程方式使用 Babel。它只需要很少的插件,一切正常。
我想将选项传递给 Babel 变换。我意识到我还没有这样做,而且看起来我正在尝试的方法不起作用。
具体来说,我想包含babel-transform-strict-mode 并通过strict:false 来禁用全局严格模式。
文档解释了在拥有.babelrc 文件时如何使用它:
// with options
{
"plugins": [
["transform-strict-mode", {
"strict": true
}]
]
}
在我的代码中:
const babelify = require("babelify")
, es2015 = require("babel-preset-es2015")
, reactify = require("babel-preset-react")
, strictMode = require("babel-plugin-transform-strict-mode")
;
...
this.browserify.transform(babelify, {
global: true,
babelrc: false,
presets: [
reactify
, es2015
, [
strictMode
, { strict: false }
]
]
});
虽然es2015 和reactify 在presets 数组中工作得很好,但将strictMode 与{ strict: false } 相加是行不通的。
错误是:
ReferenceError: [BABEL] /home/.../index.js: Unknown option: foreign.visitor.
Check out http://babeljs.io/docs/usage/options/ for more
information about options.
A common cause of this error is the presence of a
configuration options object without the corresponding
preset name. Example:
Invalid:
`{ presets: [{option: value}] }`
Valid:
`{ presets: [['presetName', {option: value}]] }`
For more detailed information on preset configuration,
please see
http://babeljs.io/docs/plugins/#pluginpresets-options.
如果我使用转换名称 (["transform-strict-mode", { strict: false }]) 而不是 strictMode,它显然找不到模块,因为这是不同模块的一部分。
如何以编程方式(没有babelrc)将选项传递给required 模块(在本例中为strictMode)?
【问题讨论】:
标签: javascript node.js babeljs strict