Nestjs 开发环境热更新的方案
Nestjs 的热更新是基于 Webpack HMR(Hot-Module Replacement) 方案
警告请注意,webpack不会自动将您的资产(例如graphql文件)复制到dist文件夹。同样,webpack与glob静态路径(例如TypeOrmModule中的实体属性)不兼容。
1 使用 CLI
如果您正在使用Nest CLI,配置过程非常简单。CLI包装webpack,它允许使用HotModuleReplacementPlugin。
安装首先安装依赖包:
$ npm i --save-dev webpack-node-externals
配置在根目录下创建 webpack.config.js,内容如下:
const webpack = require('webpack');const nodeExternals = require('webpack-node-externals');module.exports = function(options) { return { ...options, entry: ['webpack/hot/poll?100', './src/main.ts'], watch: true, externals: [ nodeExternals({ whitelist: ['webpack/hot/poll?100'], }), ], plugins: [...options.plugins, new webpack.HotModuleReplacementPlugin()], };}
此函数获取包含默认webpack配置的原始对象&