我知道babel-node
忽略 node_modules
默认情况下,所以我运行了三种不同的方式来覆盖它,但都失败了:
跑了
babel-node app.js
与.babelrc
:{ "presets": ["es2015", "react"], "only": [ "app", "node_modules/react-components" ] }
结果:
SyntaxError: Unexpected token <
对于所需的 jsx Node 模块跑了
babel-node app.js
与.babelrc
:{ "presets": ["es2015", "react"], "ignore": "node_modules\/(?!react-components)" }
结果:
SyntaxError: Unexpected token <
对于 require jsx Node 模块跑了
babel-node ./bin/www --ignore '/node_modules/(?!react-components)
与.babelrc
:{ "presets": ["es2015", "react"] }
结果:
[project]/node_modules/babel-preset-react/node_modules/babel-plugin-transform-react-jsx/lib/index.js:12 var visitor = require("babel-helper-builder-react-jsx")({ ^ TypeError: object is not a function
使用带有忽略选项的注册 Hook 可以正常工作。
跑了 node app.js
在 app.js
的开头使用此代码
require('babel-core/register')({
ignore: /node_modules\/(?!react-components)/
});
即使这行得通,我仍然想知道为什么我的实现使用 babel-node
不起作用。谢谢。
引用资料:
最佳答案
这是 babel 中的一个已知错误,它会忽略 only
和 ignore
。
相关错误是T6726 , 这一直是 fixed in babel 6.14.0 .
关于javascript - babel-node 无法在 node_modules 中要求 jsx 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35002195/