简体   繁体   English

使用节点配置替换Webpack热模块?

[英]Webpack Hot Module Replacement with Node Config?

So ive looked at documentation for webpack HMR, and cant seem to find a way to hot module load a server side file. 因此,我看了webpack HMR的文档,似乎找不到找到热模块加载服务器端文件的方法。 For example, i have a config file, written in typescript, that I need to be able to reload on change to that config file. 例如,我有一个用打字稿写的配置文件,我需要能够在更改该配置文件时重新加载它。 So i will need to update after compile. 所以我需要在编译后进行更新。 The docs are all favored towards using webpack dev server except this one HMR which doesnt explain what my webpack config should look like in order to use HMR. 所有文档都倾向于使用webpack开发服务器,但这一HMR并未说明使用HMR的webpack配置的外观。 Any help or suggestions would be awesome. 任何帮助或建议都会很棒。

You can set it up in a very similar way to client side HMR: 您可以通过与客户端HMR非常相似的方式进行设置:

1) Target 'node': 1)目标“节点”:

// webpack.config.js
module.exports = {
    ...
    target: 'node'
    ...
}

2) Enable hot module replacement, either through the cli option --hot or through config: 2)通过cli选项--hot或通过config启用热模块更换:

// webpack.config.js
module.exports = {
    ...
    plugins: [new webpack.HotModuleReplacementPlugin()]
    ...
};

3) Include HMR management code in your entry point: 3)在您的入口点包括HMR管理代码:

// webpack.config.js
module.exports = {
    ...
    entry: [
        'webpack/hot/poll?1000', // This differs from client side HMR
        'app.js'
    ]
    ...
};

4) Teach your code how to hot update using module.hot.accept : 4)教您的代码如何使用module.hot.accept进行热更新:

NOTE: This is often abstracted away by a loader. 注意:这通常是由加载程序提取的。

// app.js
let config = require('./config');
setInterval(() => {
    console.log(config.FOO_BAR);
}, 2000);

// NOTE: This would need to be done everywhere you use './config' so you might want to create a loader to generate it.
if (module.hot) {
    module.hot.accept('./config', () => {
        config = require('./config');
    });
}

4) Compile the bundle in watch mode. 4)在监视模式下编译捆绑软件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM