8

我正在尝试在反应/打字稿(使用 TSX)环境中设置热模块重新加载。我已经使用react/redux 真实世界的示例作为模型来让事情顺利进行,这就是我目前所拥有的:

服务器.js

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

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']
    },
}

索引.tsx

import * as React from 'react';
import { render } from 'react-dom';
import Root from './containers/root';

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

容器/root.tsx

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 控制台中启动此操作:

[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

我尽我所能完成了这些步骤,但仍然没有运气。

我错过了什么?

4

2 回答 2

2

如果有人仍然对此感到困惑,请参阅自述文件:https ://github.com/webpack-contrib/webpack-hot-middleware/blob/master/README.md

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

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

于 2019-11-16T17:21:16.077 回答
2

正如评论者所提到的,这个问题在我的加载器中丢失了——我不确定这是否与它有关,但我也切换到在 typescript 之后使用 babel——并将 typescript 编译为 ES6。新配置如下:

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']
    },
}
于 2016-06-06T23:45:26.467 回答