当前位置: 首页 > 知识库问答 >
问题:

react-hot-loader和webpack不工作

龚盛
2023-03-14

在花了几天的时间之后,我把它抛出来寻求帮助。

const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const path = require('path');
const sourcePath = path.join(__dirname, './src');
const publicPath = path.join(__dirname, './public')
const distPath = path.join(__dirname, './public/dist/');
const ExtractTextPlugin = require("extract-text-webpack-plugin");

//for the code shared amongst modules
const extractCommons = new webpack.optimize.CommonsChunkPlugin({
  names: ['vendor', 'manifest'], //specify the common bundle's name
  minChunks: function (module) {  //accept only vendor libraries 
    // this assumes your vendor imports exist in the node_modules directory
    return module.context && module.context.indexOf('node_modules') !== -1;
  }
});


module.exports = env => {
  const isProd = env.prod ? true : false;

  return {
    cache: false,
    entry: [
       'babel-polyfill',
       'react-hot-loader/patch',
       // 'webpack-dev-server/client?http://localhost:3000',
       // 'webpack/hot/only-dev-server',
       sourcePath + '/entry.js'
    ],

    output: {
      filename: '[name].js',  //don't use hash in dev
      path: publicPath,  //where to store the bundled files
      publicPath: '/'
    },

    devtool: 'inline-source-map',

    module: {
    rules: [
     {
      test: /(\.js|\.jsx)$/,
        loaders: ['react-hot-loader/webpack', 'babel'],
        exclude: /node_modules/
      },
      {
        test: /\.html$/,
        exclude: /node_modules/,
        loader: 'file-loader',
        query: {
          name: '[name].[ext]'
        }
      },

      {
          test: /\.svg/,
          use: {
              loader: 'svg-url-loader',
              options: {}
          }
      },
      {
        test: /\.(jpg|png)$/,
        loader: 'url-loader',
        options: {
          limit: 25000,
        },
      },

      {
        test: /(\.scss|\.css)$/,
        loader: ExtractTextPlugin.extract({fallback: 'style-loader', use: 'css-loader?sourceMap!resolve-url-loader!sass-loader?sourceMap'})
      }
    ]
  },
  resolve: {
    alias: {
      components:  path.resolve(__dirname, 'src', 'components'),
      navigation:  path.resolve(__dirname, 'src', 'navigation'),
      reducers:    path.resolve(__dirname, 'src', 'reducers'),
      actions:     path.resolve(__dirname, 'src', 'actions'),
      routes:      path.resolve(__dirname, 'src', 'routes'),
      utils:       path.resolve(__dirname, 'src', 'utils'),
      styles:      path.resolve(__dirname, 'src', 'styles'),
      images:      path.resolve(__dirname, 'public', 'images')
    },
    extensions: ['.webpack-loader.js', '.web-loader.js', '.loader.js', '.js', '.jsx'],
    modules: [
      path.resolve(__dirname, 'node_modules'),
      sourcePath
    ]
  },

  devServer: {
    host: 'localhost',
    port: 3000,
    contentBase: './public/',
    historyApiFallback: true,
    // respond to 404s with index.html

    hot: true,
    // enable HMR on the server
  },

   plugins: [
    new webpack.HotModuleReplacementPlugin(),
    // enable HMR globally

    new webpack.NamedModulesPlugin(),
    // prints more readable module names in the browser console on HMR updates

    new webpack.NoEmitOnErrorsPlugin(),
    // do not emit compiled assets that include errors


    extractCommons,
    //css files

    new ExtractTextPlugin('shared.css'),

    new HtmlWebpackPlugin({
        template: 'index.template.ejs',
        inject: 'body',
    })

  ],
}
}
    {
  "presets": [
    [
      "latest", {
        "es2015": {
          "modules": false
        }
      }
    ],
    "react",
    "stage-2"
  ],
  "plugins": [
  "transform-object-rest-spread",
    "react-hot-loader/babel-loader"
  ]
}

和我的条目文件:

import 'babel-polyfill';
import React from 'react';
import ReactDOM from 'react-dom';
import { AppContainer } from 'react-hot-loader';
import { Provider } from 'react-redux';
import { createStore, applyMiddleware, compose } from 'redux';
import { composeWithDevTools } from 'redux-devtools-extension/developmentOnly';
import { responsiveStoreEnhancer } from 'redux-responsive';
import reduxThunk from 'redux-thunk';
import reducers from './reducers';
import App from 'routes/App';
import injectTapEventPlugin from 'react-tap-event-plugin';

// Needed for onTouchTap
// http://stackoverflow.com/a/34015469/988941
injectTapEventPlugin();


const composeEnhancers = composeWithDevTools({});

const store = createStore(
  reducers,
  composeEnhancers(
    responsiveStoreEnhancer,
    applyMiddleware(
      reduxThunk
    ))
);

ReactDOM.render(
  <AppContainer>
  <Provider store={store} >
     <App />
   </Provider> 
  </AppContainer>
, document.getElementById('root')
);

// Hot Module Replacement API
if (module.hot) {
  module.hot.accept('routes/App', () => { render(
    <AppContainer>
  <Provider store={store} >
     <App />
   </Provider> 
  </AppContainer>) 
})
}

问题是网上的文档很少,因为他们正在重新做每件事。有人能帮我吗?我现在很困惑,不知道往上走哪条路。

共有1个答案

宇文良骏
2023-03-14

您试图在使用v1包的同时使用v3配置。您应该将包升级到V3。

npm install --save react-hot-loader@next

yarn install --save react-hot-loader@next
 类似资料:
  • django-webpack-loader Read http://owaislone.org/blog/webpack-plus-reactjs-and-django/ for a detailed step by step guide on setting up webpack with django using this library. Use webpack to generate yo

  • 本文向大家介绍Webpack的Loader和Plugin的区别,包括了Webpack的Loader和Plugin的区别的使用技巧和注意事项,需要的朋友参考一下 Loader 用于对模块源码的转换,loader描述了webpack如何处理非javascript模块,并且在buld中引入这些依赖。loader可以将文件从不同的语言(如TypeScript)转换为JavaScript,或者将内联图像转换

  • 我无法在React组件中显示图像。经过多次尝试(尝试了这个,这个,这个,这个,这个,这个和这个)和错误之后,我请求帮助。我正在开发构建(不是生产构建)。 我仍然得到这个错误: 组成部分 网页包配置: 目录结构 如何显示图像? 这里的示例代码: githublink查看 /src/components/home/HomePage.js 我可以做些什么来查看主页上的图像?

  • 问题内容: 我开始在开发使用的服务器端渲染应用程序的环境中工作。我对每个Webpack包在开发和生产(运行时)环境中的作用感到非常困惑。 这是我的理解摘要: :是一个软件包,是一种工具,用于将Web应用程序的不同部分结合在一起,然后捆绑成一个.js文件(通常是)。然后,结果文件将在产品环境中提供以由应用程序加载,并包含运行代码的所有必需组件。功能包括缩小代码,缩小代码等。 :是一个提供 服务器 以

  • Firebase 3.0 Starter using React Redux This is a Firebase 3.0 start using React and Redux. It uses the latest version of libraries, including the brand new React Hot Loader (still beta) Stack React Re

  • 我正在尝试让web workers启动并运行Vue cli3,但我遇到了麻烦,无法让它正常工作。 我想使用下面的包worker-loader(而不是vue-worker),因为它看起来维护得很好,而且有更多的贡献。 在他们的教程之后,我尝试使用vue cli修改webpack,如下所示: 我希望能和他们的相配 可以在这里阅读(https://github.com/webpack-contrib/w