Error on type Props in react flow, converting create-react app to webpack

孤街醉人 提交于 2021-01-28 08:06:38

问题


I am trying to convert a create-react app to a webpack app. The context behind this is that I originally have a app that was made using create-react. I wanted to run that project using webpack. I took an app that I had that was running on webpack, and then I delete all its source files, replacing the with the source files from the create react app. After that, I took package.json dependencies and moved them over from the package.json of the create-react app. I think perhaps I need some extra configuration in webpack to handle flow. I need help in troubleshooting. I get this type of error:

ERROR in ./src/js/constants/Routes.jsx
Module build failed (from ./node_modules/babel-loader/lib/index.js):
SyntaxError: Unexpected token, expected ";" (7:5)

   5 | import { reduxConnect } from '../hoc';
   6 | 
>  7 | type Props = {

This is my webpack.config.js:

const path = require('path');

const config = {
    entry: __dirname + '/src/js/index.js',
    output: {
        path: __dirname + '/grails-app/assets/javascripts',
        publicPath: '/assets/',
        filename: 'bundle.js'
    },
    module: {
        rules: [
            {
                test: /\.(js|jsx)$/,
                use: 'babel-loader',
                exclude: /node_modules/
            },
            {
                test: /\.css$/,
                use: [
                    'style-loader',
                    'css-loader'
                ]
            }
        ]
    },
    resolve: {
        extensions: [
            '.js',
            '.jsx'
        ]
    }
};

module.exports = config;    

This is my .babelrc

  "presets": [
    "@babel/preset-env",
    "@babel/preset-react"
  ]
}

package.json (I run in prod mode so ignore devDependencies):

{
  "name": "KahuDealerWeb",
  "version": "1.0.0",
  "description": "Vehicle Finance 3.0 ====================",
  "main": "index.js",
  "directories": {
    "lib": "lib",
    "test": "test"
  },
  "scripts": {
    "watch": "webpack --watch --colors --progress",
    "bundle": "webpack"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/ProconGPS/KahuDealerWeb.git"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "bugs": {
    "url": "https://github.com/ProconGPS/KahuDealerWeb/issues"
  },
  "homepage": "https://github.com/ProconGPS/KahuDealerWeb#readme",
  "dependencies": {
    "@babel/core": "^7.0.1",
    "@babel/preset-env": "^7.0.0",
    "@babel/preset-flow": "^7.10.1",
    "@babel/preset-react": "^7.0.0",
    "@material-ui/core": "^4.10.1",
    "@material-ui/icons": "^4.9.1",
    "axios": "^0.19.0",
    "babel-loader": "^8.0.2",
    "core-js": "^3.1.3",
    "css-loader": "^3.5.3",
    "lodash": "^4.17.11",
    "material-ui-icons": "^1.0.0-beta.36",
    "node-sass": "^4.10.0",
    "react": "^16.13.1",
    "react-cookies": "^0.1.0",
    "react-dom": "^16.13.1",
    "react-modal": "^3.11.1",
    "react-redux": "^5.0.7",
    "react-router-dom": "^4.2.2",
    "react-select": "^2.2.0",
    "react-to-print": "^2.5.1",
    "recharts": "^1.8.5",
    "redux": "^3.7.2",
    "redux-observable": "^0.18.0",
    "rxjs": "^5.5.6",
    "rxjs-compat": "^6.3.3",
    "style-loader": "^1.2.1",
    "webpack": "^4.18.1",
    "webpack-cli": "^3.1.0",
    "webpack-dev-server": "^3.1.8"
  },
  "devDependencies": {
    "babel-core": "^6.26.3",
    "babel-loader": "^8.1.0",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-react": "^6.24.1",
    "webpack": "^4.43.0",
    "webpack-cli": "^3.3.11"
  }
}


回答1:


Flow syntax is not real JS. so things like type Props will fall apart when transpiling or bundling. You need to add a babel plugin/preset to handle these cases.

This is what you're looking to add: https://babeljs.io/docs/en/babel-preset-flow

But specifically, Install @babel/preset-flow with either yarn or npm

Then in your .babelrc update your config to add the new preset

{
  "presets": [
    "@babel/preset-env",
    "@babel/preset-react",
    "@babel/preset-flow"
  ]
}


来源:https://stackoverflow.com/questions/62459771/error-on-type-props-in-react-flow-converting-create-react-app-to-webpack

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!