Babel 7中的外部进口不会被转移 [英] External imports in Babel 7 do not get transpiled
问题描述
我目前正在将代码库从Babel 6迁移到7.该代码由具有各自配置的多个独立项目组成.
I'm currently migrating a codebase from Babel 6 to 7. The code is made up of multiple individual projects with their own configs.
main
项目从external
导入文件,但是没有编译由main
从external
导入的脚本,并且在意外令牌导入"时失败.直接位于main
中的脚本可以正确地进行翻译.
The main
project imports files from external
however the scripts being imported from external
by main
aren't being transpiled and fails on "Unexpected token import". Scripts located directly in main
do transpile correctly.
我正在main
项目中使用以下命令来转译脚本:
I'm using the following command within the main
project to transpile the scripts:
babel-node ./index.js
另一个项目使用Webpack来做同样的事情,并正确处理所有事情.
Another project uses Webpack to do the same thing and handles everything correctly.
此设置在Babel 6上也可以正常工作.
This setup also worked fine with Babel 6.
.babelrc
表示main
{
"ignore": [
"node_modules"
],
"presets": [
["@babel/preset-env", {
"targets": {
"node": "current"
},
"useBuiltIns": "entry"
}]
],
"plugins": [
[
"module-resolver", {
"alias": {
"External": "../external"
}
}
],
"@babel/plugin-proposal-decorators",
"@babel/plugin-proposal-object-rest-spread",
"@babel/plugin-proposal-export-default-from",
"@babel/plugin-proposal-export-namespace-from",
"@babel/plugin-proposal-class-properties"
]}
.babelrc
{
"presets": [
"@babel/preset-react"
],
"plugins": [
"@babel/plugin-proposal-class-properties",
"@babel/plugin-proposal-object-rest-spread",
"@babel/plugin-transform-runtime"
]}
我创建了一个示例来详细说明我的问题,
I've created an example to detail my problem at:
https://gitlab.com/nerdyman/babel-7-external-导入中断
TL; DR我正在尝试从项目的根目录之外导入脚本,但是Babel不会对它们进行转译,而在项目 do 中进行转译的脚本.
TL;DR I'm trying to import scripts from outside of a project's root directory but they don't get transpiled by Babel, the scripts from within the project do transpile.
推荐答案
我已通过遵循解决方案是:
- 将主项目中的
.babelrc
移至babel.config.js
并使其成为CommonJS模块
从主项目运行 - 添加
--ignore=node_modules
babel-node
时- Move
.babelrc
in the main project tobabel.config.js
and make it a CommonJS module - Add
--ignore=node_modules
when runningbabel-node
from the main project
这似乎仍然很hacky,Babel似乎并没有承认babel.config.js
中的ignore
属性,它必须必须指定为标志.
This still seems pretty hacky and Babel doesn't seem to acknowledge the ignore
property within babel.config.js
it must be specified as a flag.
Babel 7似乎仅允许导入babel配置所在目录中的内容,但是显式设置--ignore
标志会对此进行覆盖.
Babel 7 appears to only allow imports within the directory the babel config is in, however explicitly setting the --ignore
flag overrides this.
You can view my working demo and the diff of what I changed to get it working.
问题仍在GitHub上开放,因此可能存在更好的解决方案未来.
The issue is still open on GitHub so there may be a better solution in the future.
这篇关于Babel 7中的外部进口不会被转移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!