首页 > 解决方案 > react-native 包本身抛出 babel 错误

问题描述

我正在尝试构建一个简单的 react-native 应用程序,并且我已经安装了所有依赖项。这是我的 webpack 配置文件

module: {
    rules: [
      {
        test: /\.js?$/,
        // exclude: /(node_modules)/,
        use: {
          loader: "babel-loader",
          options: {
            presets: [["@babel/preset-env"], ["@babel/preset-react"],[
              require.resolve('babel-preset-react-app/dependencies'),
              { helpers: true },
            ]],
            "plugins": [
              [
                
                "@babel/plugin-proposal-class-properties",
                {
                  "loose": true
                }
              ]
            ]
          }
        }
      }
    ],
  },
const commonResolveBlock = {
  alias: {
    /**
     * When using react-native-web, this statement tells webpack to resolve react-native to react-native-web at all the
     * places inside your application.
     */
   
    //"react-native$": "react-native-web",
    'react-native': path.join(__dirname, 'node_modules/react-native'),
  },
};

当我尝试编译相同的 babel 在 node_modules 内的 react-native 包中抛出错误(安装 react-native 时下载的那个)

/**
 * Copyright (c) Facebook, Inc. and its affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 *
 * @format
 * @flow
 */

'use strict';

import typeof AccessibilityInfo from './Libraries/Components/AccessibilityInfo/AccessibilityInfo';

错误显示在 type of 之后,并显示在所有行中。显示的错误是

ERROR in ./node_modules/react-native/index.js
Module build failed (from /Volumes/workplace/SudhirsmUIDev/node_modules/babel-loader/lib/index.js):
SyntaxError: /Volumes/workplace/SudhirsmUIDev/src/ECAPPExperimentalHEXReactAssets/node_modules/react-native/index.js: Unexpected token, expected "{" (13:7)

  11 | 'use strict';
  12 | 
> 13 | import typeof AccessibilityInfo from './Libraries/Components/AccessibilityInfo/AccessibilityInfo';
     |        ^
  14 | import typeof ActivityIndicator from './Libraries/Components/ActivityIndicator/ActivityIndicator';
  15 | import typeof Button from './Libraries/Components/Button';
  16 | import typeof CheckBox from './Libraries/Components/CheckBox/CheckBox';
    at Object._raise (/Volumes/workplace/SudhirsmUIDev/node_modules/@babel/parser/src/parser/error.js:60:45)

代码中单独使用 react-native 的另一个问题,模块是从 /user/library/caches 导入的,而其他所有模块都是从 node_modules 中正确获取的。任何帮助将不胜感激。

标签: reactjsreact-nativebabeljs

解决方案


这是使用您未启用的 Flow 语法。添加'@babel/flow'到 Babel 预设。


推荐阅读