我正在尝试在运行AVA测试时转换源文件(以及它们在node_modules中的依赖项)。我已经将AVA配置为需要babel-register,并在package.json中使用以下内容继承我的.babelrc文件
"ava": {
"require": "babel-register",
"babel": "inherit"
}这是.babelrc格式的
{
"presets": [ "es2015" ],
"ignore": false
}我有a test spec that imports a source file with和that source file imports an ES2015 dependency from node_modules
但是,在运行ava时,我看到:
/Users/me/code/esri-rollup-example/node_modules/capitalize-word/index.js:2
export default input => input.replace(regexp, match => match.charAt(0).toUpperCase() + match.substr(1));
^^^^^^
SyntaxError: Unexpected token export这告诉我源文件(src/app/utils.js)可以转换,但它在node_modules (capitalize-string/index)中的依赖项不能。
当我使用babel CLI时,源模块和依赖项都可以正常转换,所以看起来.babelrc的"ignore": false设置没有被传递给babel-register。我可以从babel文档中看到你可以explicitly pass an ignore option to babel-register,但我不知道你如何从AVA配置中做到这一点。我甚至尝试在我的测试文件中添加以下代码到它导入源文件的行前,但我仍然看到相同的错误:
require("babel-register")({
ignore: false
});我想我可以在测试之前添加一个transpile步骤,但我想确保我首先没有错过一些AVA或babel配置。
发布于 2016-07-29 02:24:59
这与巴别塔本身的问题有关- https://phabricator.babeljs.io/T6726
但是您可以将babel-register require放在单独的文件中(让我们称之为.setup.js):
require('babel-register')({
ignore: /node_modules\/(?!capitalize\-word)/i
});
const noop = function () {};
require.extensions['.css'] = noop; // If you want to ignore some CSS imports然后将"require": "babel-register"更改为"require": "./.setup.js"
https://stackoverflow.com/questions/36857210
复制相似问题