添加反应本机元素以反应本机时出错

时间:2020-06-07 10:30:59

标签: javascript react-native webpack babeljs react-native-web

我已经将React Native与React Native Web项目结合在一起了,我想使用React Native元素,但是在向项目中添加React Native元素时出现错误。

./node_modules/react-native-elements/src/avatar/Avatar.js
SyntaxError: /home/hamidreza/Desktop/reactElement/node_modules/react-native-elements/src/avatar/Avatar.js: Unexpected token (72:4)

  70 | 
  71 |   const Utils = showAccessory && (
> 72 |     <TouchableHighlight
     |     ^
  73 |       style={StyleSheet.flatten([
  74 |         styles.accessory,
  75 |         {

我的web/webpack.config.js

const path = require('path');
const webpack = require('webpack');

const appDirectory = path.resolve(__dirname, '../');
const babelLoaderConfiguration = {
  test: /\.js$/,
  include: [
    path.resolve(appDirectory, 'index.web.js'),
    path.resolve(appDirectory, 'src'),
    path.resolve(appDirectory, 'node_modules/react-native-uncompiled'),
    path.resolve(appDirectory, 'node_modules/react-native-vector-icons'),
    path.resolve(appDirectory, 'node_modules/react-native-elements'),
  ],
  use: {
    loader: 'babel-loader',
    options: {
      cacheDirectory: true,
      presets: ['react-native'],
      plugins: ['react-native-web'],
    },
  },
};

const imageLoaderConfiguration = {
  test: /\.(gif|jpe?g|png|svg)$/,
  use: {
    loader: 'url-loader',
    options: {
      name: '[name].[ext]',
    },
  },
};

module.exports = {
  entry: [path.resolve(appDirectory, 'index.web.js')],

  output: {
    filename: 'bundle.web.js',
    path: path.resolve(appDirectory, 'dist'),
  },
  module: {
    rules: [babelLoaderConfiguration, imageLoaderConfiguration],
  },

  resolve: {
    alias: {
      'react-native$': 'react-native-web',
    },
    extensions: ['.web.js', '.js'],
  },
};

我的package.json

{
  "name": "ReactNativeWebApp",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "android": "react-native run-android",
    "ios": "react-native run-ios",
    "start": "react-native start",
    "web": "react-scripts start",
    "test": "jest",
    "lint": "eslint ."
  },
  "dependencies": {
    "@react-native-community/masked-view": "^0.1.10",
    "@react-navigation/native": "^5.5.1",
    "@react-navigation/stack": "^5.4.2",
    "react": "16.11.0",
    "react-dom": "^16.13.1",
    "react-native": "0.62.2",
    "react-native-elements": "^2.0.2",
    "react-native-gesture-handler": "^1.6.1",
    "react-native-reanimated": "^1.9.0",
    "react-native-safe-area-context": "^3.0.2",
    "react-native-screens": "^2.8.0",
    "react-native-vector-icons": "^6.6.0",
    "react-native-web": "^0.12.3"
  },
  "devDependencies": {
    "@babel/core": "^7.10.2",
    "@babel/plugin-proposal-class-properties": "^7.10.1",
    "@babel/runtime": "^7.10.2",
    "@react-native-community/eslint-config": "^1.1.0",
    "metro-react-native-babel-preset": "^0.59.0",
    "react-scripts": "^3.4.1",
    "react-test-renderer": "16.11.0"
  },
  "jest": {
    "preset": "react-native"
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  }
}

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

由于“本机”软件包不包含编译后的代码(babel和jsx转换后的es5),因此要在网络中使用它们,必须将库添加到webpack配置中。

所以我必须将path.resolve(paths.appNodeModules, 'react-native-elements'),path.resolve(paths.appNodeModules, 'react-native-rating'),添加到webpack,这样才能解决问题。

参考link