【问题标题】:npx babel not reading configuration from babel.config.jsnpx babel 没有从 babel.config.js 读取配置
【发布时间】:2020-06-12 03:00:12
【问题描述】:

从命令行运行npx babel index.js 时,我希望看到我的 babel 配置是从 babel.config.js 应用的

但似乎并非如此,因为想知道为什么会这样?

// 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 文件?

干杯

【问题讨论】:

  • 通天塔版本?
  • npx babel --version 给了 6.26.0 (babel-core 6.26.3)

标签: javascript react-native babeljs npx babel-cli


【解决方案1】:

babel.config.js config change 在 babel 7 中引入;所以如果你使用的是 babel 6.*,它还不能理解 project wide configuration;使用.babelrcupgrade to babel 7 可以使用新功能;我已经完成了非常顺利和无痛的升级,只要确保你有干净的 git 目录(以防紧急情况:)然后去做。

【讨论】:

    猜你喜欢
    • 2019-05-27
    • 1970-01-01
    • 2023-02-06
    • 2017-11-03
    • 2010-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多