npx babel无法从babel.config.js中读取配置

问题描述 投票:0回答:1

从命令行运行npx babel index.js时,我希望可以从babel.config.js中应用我的babel配置

但是似乎不知道为什么会这样吗?

// babel.config.js
module.exports = function babel(api) {
 api.cache(true);
   return {
     presets: ['module:metro-react-native-babel-preset'],
     plugins: [
       [
         'babel-plugin-root-import',
         {
           rootPathSuffix: './src',
           rootPathPrefix: '~/',
         },
       ],
     ],
   };
 };

// index.js
import { AppRegistry } from 'react-native';
import App from '~/App';
AppRegistry.registerComponent("App Name", () => App)

// Expected output from npx babel index.js
import { AppRegistry } from 'react-native';
import App from './src/App'; // Note the change from '~' to './src' using babel-plugin-root-import
AppRegistry.registerComponent("App Name", () => App)

我在npx babel中注意到了--help,它指出--no-babelrc标志会忽略.babelrc和.babelignore文件中的配置。这是否表明调用此命令时不考虑babel.config.js文件?

欢呼声

javascript react-native babel npx babel-cli
1个回答
0
投票

babel.config.js config change在babel 7中引入;因此,如果您使用的是babel 6. *,则尚不了解project wide configuration;使用.babelrcupgrade to babel 7可以使用新功能;我已经完成了升级,升级过程非常顺利且轻松,只需确保您具有干净的git目录(以防万一:),然后执行即可。

© www.soinside.com 2019 - 2024. All rights reserved.