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



我正在尝试在反应/打字稿(使用 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')
);

containers/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

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

我错过了什么?

正如评论者所提到的,这个问题在我的加载器中丢失了 - 我不确定这是否与它有关,但我也在打字稿后改用 babel - 并将打字稿编译为 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']
    },
}

如果有人仍然为此苦苦挣扎,请参阅自述文件:https://github.com/webpack-contrib/webpack-hot-middleware/blob/master/README.md

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

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

相关内容

  • 没有找到相关文章

最新更新