Nest.js 框架热重载不工作

Nest.js framework hot reload doesn't work

我已按照文档中的步骤操作:

https://docs.nestjs.com/techniques/hot-reload

我是 运行 这个命令:npm run webpack 但它关闭了,它 returns 我的提示,它不会继续监视文件:

    gabriel@roraima-tv:/var/www/studying/tera-ping-pong$ npm run webpack

    > tera-ping-pong@0.0.0 webpack /var/www/studying/tera-ping-pong
    > webpack --config webpack.config.js


    webpack is watching the files…

    Hash: 6e13d56ba7d77331e5c2
    Version: webpack 4.23.1
    Time: 3014ms
    Built at: 11/01/2018 1:39:11 PM
                       Asset       Size  Chunks             Chunk         Names
    dist/app.controller.d.ts  177 bytes          [emitted]  
        dist/app.module.d.ts   35 bytes          [emitted]  
       dist/app.service.d.ts   56 bytes          [emitted]  
              dist/main.d.ts   11 bytes          [emitted]  
          dist/main.hmr.d.ts   11 bytes          [emitted]  
                   server.js     39 KiB    main  [emitted]  main
    Entrypoint main = server.js
    [0] multi webpack/hot/poll?1000 ./src/main.hmr.ts 40 bytes {main}         [built]
    [./node_modules/webpack/hot/log-apply-result.js]         (webpack)/hot/log-apply-result.js 1.27 KiB {main} [built]
    [./node_modules/webpack/hot/log.js] (webpack)/hot/log.js 1.11 KiB         {main} [built]
    [./node_modules/webpack/hot/poll.js?1000] (webpack)/hot/poll.js?        1000 1.15 KiB {main} [built]
    [./src/app.controller.ts] 1.44 KiB {main} [built]
    [./src/app.module.ts] 1.03 KiB {main} [built]
    [./src/app.service.ts] 883 bytes {main} [built]
    [./src/main.hmr.ts] 1.07 KiB {main} [built]
    [@nestjs/common] external "@nestjs/common" 42 bytes {main} [built]
    [@nestjs/core] external "@nestjs/core" 42 bytes {main} [built]
    gabriel@roraima-tv:/var/www/studying/tera-ping-pong$ 

因此,每当我添加我的 *.ts 文件更改时,它们不会被重新加载,直到服务器重新启动。

首先安装需要的包:

npm i --save-dev webpack-node-externals start-server-webpack-plugin

安装完成后,在应用程序的根目录中创建一个 webpack-hmr.config.js 文件。

 const webpack = require('webpack');
const nodeExternals = require('webpack-node-externals');
const StartServerPlugin = require('start-server-webpack-plugin');

module.exports = function(options) {
  return {
    ...options,
    entry: ['webpack/hot/poll?100', options.entry],
    watch: true,
    externals: [
      nodeExternals({
        allowlist: ['webpack/hot/poll?100'],
      }),
    ],
    plugins: [
      ...options.plugins,
      new webpack.HotModuleReplacementPlugin(),
      new webpack.WatchIgnorePlugin([/\.js$/, /\.d\.ts$/]),
      new StartServerPlugin({ name: options.output.filename }),
    ],
  };
};

要启用HMR,打开应用程序入口文件(main.ts)并添加以下与webpack相关的指令:

declare const module: any;

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  await app.listen(3000);

  if (module.hot) {
    module.hot.accept();
    module.hot.dispose(() => app.close());
  }
}
bootstrap();

你可以只在 CLI 中使用这个命令,它是默认的:

npm run start:dev

问题是你使用了

npm start

仅代替

npm start:dev

以监视模式运行服务器:

您可以运行在手表模式下嵌套

nest start --watch