gpt4 book ai didi

babeljs - 使用带有 AVA 的 babel-register 和 .babelrc 的 `"ignore": false`, 但 node_modules 没有被转译

转载 作者:行者123 更新时间:2023-12-04 15:19:59 24 4
gpt4 key购买 nike

我试图在运行 AVA 测试时转换源文件(及其在 node_modules 中的依赖项)。我已将 AVA 配置为需要 babel-register并继承我的.babelrcpackage.json 中包含以下内容的文件:

"ava": {
"require": "babel-register",
"babel": "inherit"
}

这在 .babelrc :
{
"presets": [ "es2015" ],
"ignore": false
}

我有 a test spec that imports a source filethat 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 配置。

最佳答案

这与 babel 本身的问题有关 – https://phabricator.babeljs.io/T6726

但是你可以把 babel-register需要在单独的文件中(让我们称之为 .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"

关于babeljs - 使用带有 AVA 的 babel-register 和 .babelrc 的 `"ignore": false`, 但 node_modules 没有被转译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36857210/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com