Nest.js框架热重装不起作用

时间:2018-11-01 19:12:52

标签: node.js webpack-dev-server nestjs

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

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

我正在运行以下命令:npm run webpack,但是它关闭了,它向我返回提示,并且它不会一直在监视文件:

    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文件时,它们都会更改,并且直到服务器重新启动后才会重新加载它们。

2 个答案:

答案 0 :(得分:1)

首先安装所需的软件包:

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();

答案 1 :(得分:0)

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

npm run start:dev