为什么我的 babel 配置忽略了这个插件?

问题描述

这是我的 babe.config.js :

module.exports = {
  presets: [
    '@babel/preset-react',[
      '@babel/preset-env',{
        modules: false,},],plugins: [
    '@babel/plugin-proposal-optional-chaining','styled-components','@babel/plugin-proposal-class-properties','@babel/plugin-Syntax-dynamic-import',env: {
    production: {
      only: ['app'],plugins: [
        'lodash','transform-react-remove-prop-types','@babel/plugin-transform-react-constant-elements',test: {
      plugins: [
        '@babel/plugin-transform-modules-commonjs','dynamic-import-node',};

现在我收到此错误

Support for the experimental Syntax 'optionalChaining' isn't currently enabled (39:37):

  37 | 
  38 | try {
> 39 |   console.log('APP VERSION',process?.env?.VERSION,process?.env?.ENVIRONMENT);
     |                                     ^
  40 | } catch (e) {
  41 |   console.error(e);
  42 | }

Add @babel/plugin-proposal-optional-chaining (https://git.io/vb4Sk) to the 'plugins' section of your Babel config to enable transformation.

所以基本上 babel 忽略了 @babel/plugin-proposal-optional-chaining 应该处理 optionalChaining 的插件

解决方法

我通过简单地将 @babel/core 升级到最新版本解决了这个问题。