How to allow for webpack-dev-server to allow entry points from react-router

后端 未结 9 1974
無奈伤痛
無奈伤痛 2020-12-04 05:15

I\'m creating an app that uses webpack-dev-server in development alongside react-router.

It seems that webpack-dev-server is built around the assumption that you wil

9条回答
  •  自闭症患者
    2020-12-04 06:03

    For a more recent answer, the current version of webpack (4.1.1) you can just set this in your webpack.config.js like such:

    const webpack = require('webpack');
    
    module.exports = {
        entry: [
          'react-hot-loader/patch',
          './src/index.js'
        ],
        module: {
            rules: [
                {
                    test: /\.(js|jsx)$/,
                    exclude: /node_modules/,
                    use: ['babel-loader']
                },
                {
                    test: /\.css$/,
                    exclude: /node_modules/,
                    use: ['style-loader','css-loader']
                }
            ]
        },
        resolve: {
          extensions: ['*', '.js', '.jsx']  
        },
        output: {
          path: __dirname + '/dist',
          publicPath: '/',
          filename: 'bundle.js'
        },
        plugins: [
          new webpack.HotModuleReplacementPlugin()
        ],
        devServer: {
          contentBase: './dist',
          hot: true,
          historyApiFallback: true
        }
      };
    

    The important part is historyApiFallback: true. No need to run a custom server, just use the cli:

    "scripts": {
        "start": "webpack-dev-server --config ./webpack.config.js --mode development"
      },
    

提交回复
热议问题