首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >以下模块无法热更新:(需要完全重新加载)

以下模块无法热更新:(需要完全重新加载)
EN

Stack Overflow用户
提问于 2016-06-03 18:04:43
回答 2查看 5.7K关注 0票数 8

我试图设置热模块重新加载在一个反应/类型记录(与TSX)环境。我使用react/redux现实世界的例子作为一个模型来推动事情的进行,到目前为止,这就是我所拥有的:

server.js

代码语言:javascript
复制
var webpack = require('webpack')
var webpackDevMiddleware = require('webpack-dev-middleware')
var webpackHotMiddleware = require('webpack-hot-middleware')
var config = require('./webpack.config')

var app = new (require('express'))()
var port = 3000

var compiler = webpack(config)
app.use(webpackDevMiddleware(compiler, { noInfo: true, publicPath: config.output.publicPath }))
app.use(webpackHotMiddleware(compiler))

app.use(function(req, res) {
  res.sendFile(__dirname + '/index.html')
})

app.listen(port, function(error) {
  if (error) {
    console.error(error)
  } else {
    console.info("==>   Listening on port %s. Open up http://localhost:%s/ in your browser.", port, port)
  }
})

webpack.config.js

代码语言:javascript
复制
var path = require('path')
var webpack = require('webpack')
var HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
    devtool: 'cheap-module-eval-source-map',
    entry: [
        'webpack-hot-middleware/client',
        path.resolve('./src/index.tsx'),    
    ],
    output: {
        path: path.join(__dirname, 'dist'),
        filename: 'bundle.js'
    },
    plugins: [
        new webpack.optimize.OccurrenceOrderPlugin(),
        new webpack.HotModuleReplacementPlugin(),
        new HtmlWebpackPlugin({ template: './index.html' })
    ],
    module: {
        loaders: [
            { test: /\.tsx?$/, loader: 'ts-loader' }
        ]
    },
    resolve: {
        extensions: ['', '.ts', '.tsx', '.js', '.json']
    },
}

index.tsx

代码语言:javascript
复制
import * as React from 'react';
import { render } from 'react-dom';
import Root from './containers/root';

render(
    <Root />,
    document.getElementById('root')
);

containers/root.tsx

代码语言:javascript
复制
import * as React from 'react';

export default class Root extends React.Component<void, void> {
    render(): JSX.Element {
        return (
            <p>boom pow</p>
        );
    }
}

在根元素中将<p>boom pow</p>更改为<p>boom boom pow</p>将在浏览器的javascript控制台中启动:

代码语言:javascript
复制
[HMR] bundle rebuilding
client.js?3ac5:126 [HMR] bundle rebuilt in 557ms
process-update.js?e13e:27 [HMR] Checking for updates on the server...
process-update.js?e13e:81 [HMR] The following modules couldn't be hot updated: (Full reload needed)
This is usually because the modules which have changed (and their parents) do not know how to hot reload themselves. See http://webpack.github.io/docs/hot-module-replacement-with-webpack.html for more details.
process-update.js?e13e:89 [HMR]  - ./src/containers/root.tsx
process-update.js?e13e:89 [HMR]  - ./src/index.tsx

据我所知,我已经通过了这些步骤,但仍然没有运气。

我遗漏了什么?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-06-06 23:45:26

正如评论中提到的那样,这个问题在我的加载程序中缺失了--我不确定这是否与它有关,但我也转向了在键入文本之后使用babel,并将类型记录编译为ES6。新配置如下:

代码语言:javascript
复制
var path = require('path')
var webpack = require('webpack')
var HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
    devtool: 'cheap-module-eval-source-map',
    entry: [
        'webpack-hot-middleware/client',
        path.resolve('./src/index.ts'), 
    ],
    output: {
        path: path.join(__dirname, 'dist'),
        filename: 'bundle.js'
    },
    plugins: [
        new webpack.optimize.OccurrenceOrderPlugin(),
        new webpack.HotModuleReplacementPlugin(),
        new HtmlWebpackPlugin({ template: path.resolve('./src/index.html') })
    ],
    module: {
        loaders: [
            {   test: /\.tsx?$/, 
                loaders: [
                    'react-hot',
                    'babel?presets[]=es2015',                  
                    'ts-loader'
                ] 
            },
            {   test: /\.json$/, 
                loader: 'json'
            }
        ]
    },
    resolve: {
        extensions: ['', '.ts', '.tsx', '.js', '.json']
    },
}
票数 2
EN

Stack Overflow用户

发布于 2019-11-16 17:21:16

如果有人还在努力解决这个问题,请参见自述:https://github.com/webpack-contrib/webpack-hot-middleware/blob/master/README.md

此模块只涉及将浏览器客户端连接到webpack服务器并接收更新的机制。它将订阅来自服务器的更改,并使用webpack的HMR执行这些更改。实际上,使您的应用程序能够使用热重载进行无缝更改超出了范围,通常由另一个库处理。

webpack-hot-middleware不处理热重加载,例如,您需要使用react-hot-loader

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37620861

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档