React app在组件文件夹中查找bundle.js而不是项目根目录

时间:2018-04-20 21:42:18

标签: reactjs webpack react-router-v4 react-router-dom

我在嵌套路线上刷新后,在控制台中显示我得到的错误(注册/电子邮件确认)。而非嵌套路由不会出现此错误。

我认为主要的问题是它在嵌套路径路径中搜索bundle.js和中的图像,而不是根路径。

我的控制台中的错误:

GET http://localhost:3002/register/bundle.js net::ERR_ABORTED

Refused to execute script from 'http://localhost:3002/register/bundle.js' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled.

GET http://localhost:3002/register/a5e694be93a1c3d22b85658bdc30008b.png 404 (Not Found)

我的webpack.config.js:

const webpack = require('webpack');
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');

const BUILD_PATH = path.resolve( __dirname, "./client/build" );
const SOURCE_PATH = path.resolve( __dirname, "./client/src" );
const PUBLIC_PATH = "/";

...

module.exports = {
  devtool: 'eval-source-map',

  context: SOURCE_PATH,

  entry: ['babel-polyfill', SOURCE_PATH + '/index.jsx'],

  module: {
    rules: [
      {
        test: /\.jsx?$/,
        exclude: [/node_modules/, /server/],
        use: {
          loader: 'babel-loader',
          options: {
            presets: ['env', 'es2015', 'react', 'stage-1', 'stage-0', 'stage-2'],
            plugins: [
              'transform-decorators-legacy',
              'transform-es2015-destructuring',
              'transform-es2015-parameters',
              'transform-object-rest-spread'
            ]
          }
        }
      },
      {
        test: /\.scss$/,
          use: [{
            loader: "style-loader"
          }, {
            loader: "css-loader"
          }, {
            loader: "sass-loader"
          }]
      },
      {
        test: /\.(png|jpg|gif)$/,
        use: [
          {
            loader: 'file-loader',
            options: {}
          }
        ]
      },
    ],
  },

  output: {
    path: BUILD_PATH,
    filename: "bundle.js",
  },

  devServer: {
    compress: true,
    port: 3002,
    historyApiFallback: true,
    contentBase: BUILD_PATH,
    publicPath: PUBLIC_PATH,
  },

  plugins: [
    new webpack.DefinePlugin(appConstants),
    new HtmlWebpackPlugin({
      filename: 'index.html',
      template: path.resolve(__dirname, 'client/src/index.html'),
      inject: true
    }),
  ],

  watch: true,
}

1 个答案:

答案 0 :(得分:2)

我不知道这个错误,但强烈建议您使用fuse-box

fuse-box是构建系统的未来,在几分钟之内,您将以高速热重载和许多其他方式运行您的项目......

检查这个react example seed,这太棒了......