https://docs.nestjs.com/recipes/hot-reload#hot-reload

 

Documentation | NestJS - A progressive Node.js framework

Nest is a framework for building efficient, scalable Node.js server-side applications. It uses progressive JavaScript, is built with TypeScript and combines elements of OOP (Object Oriented Programming), FP (Functional Programming), and FRP (Functional Rea

docs.nestjs.com

 

 

 

프로젝트 코드를 작성하고 새로 작성한 코드를 실행하기 위해서는 서버를 새로 시작해야했다.

이를 간단히 하기위해서 express를 활용할때는 nodemon 모듈을 이용했는데, NestJS 공식문서에 hot reload에 관련한 내용이 있어서 이 방식으로 해보려고 한다.

 

 

공식문서에는 NestCli를 사용할때와 아닐때로 구분되어 있다.

 

나는 사용하는 버전으로 !

 

 

$ npm i --save-dev webpack-node-externals run-script-webpack-plugin webpack

npm 설치

 

 

const nodeExternals = require('webpack-node-externals');
const { RunScriptWebpackPlugin } = require('run-script-webpack-plugin');

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

프로젝트 루트 위치에 webpack-hmr.config.js 파일을 작성한다.

 

 

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

main.ts 파일에 module.hot 설정한다.

 

 

"start:dev": "nest build --webpack --webpackPath webpack-hmr.config.js --watch"
$ npm run start:dev

package.json에 script추가하고 실행하면 된다.

 

 

공식문서 고대로 따라하면 됨!