webpack React应用程序在组件文件夹而不是项目根目录中查找React. js

but5z9lq  于 11个月前  发布在  Webpack
关注(0)|答案(2)|浏览(110)

下面我得到的错误在我刷新嵌套路由(register/email-confirmation)后显示在控制台中。而非嵌套路由不会得到此错误。
我认为主要的问题是它在嵌套的路由路径中搜索weble.js和图像,而不是根路径。

我的控制台错误:

第一个月
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,
}

字符串

ufj5ltwl

ufj5ltwl1#

我不知道这个bug,但我强烈建议使用fuse-box
fuse-box是构建系统的未来,在几分钟内,您将通过高速热重载和许多其他实用程序运行您的项目。
看看这个react example seed,真是太神奇了。

yqhsw0fo

yqhsw0fo2#

在HTMLWebPackPlugin中使用base选项。

new HtmlWebpackPlugin({
            template: path.join(__dirname, 'public', 'index.html'),
            title: 'my title',
            static: 'static',
            base: '/',
        }),

字符串
它是在标头中附加标签基础。然后工作正常。

<base href='/'>


https://github.com/jantimon/html-webpack-plugin#options

相关问题