Hot Reload

The highest impact on your application’s bootstrapping process is TypeScript compilation. Fortunately, with webpack HMR (Hot-Module Replacement), we don’t need to recompile the entire project each time a change occurs. This significantly decreases the amount of time necessary to instantiate your application, and makes iterative development a lot easier.

Warning Note that webpack won’t automatically copy your assets (e.g. graphql files) to the dist folder. Similarly, webpack is not compatible with glob static paths (e.g., the entities property in TypeOrmModule).

With CLI

If you are using the Nest CLI, the configuration process is pretty straightforward. The CLI wraps webpack, which allows use of the HotModuleReplacementPlugin.

Installation

First install the required packages:

  1. $ npm i --save-dev webpack-node-externals start-server-webpack-plugin

Configuration

Once the installation is complete, create a webpack-hmr.config.js file in the root directory of your application.

  1. const webpack = require('webpack');
  2. const nodeExternals = require('webpack-node-externals');
  3. const StartServerPlugin = require('start-server-webpack-plugin');
  4. module.exports = function(options) {
  5. return {
  6. ...options,
  7. entry: ['webpack/hot/poll?100', options.entry],
  8. watch: true,
  9. externals: [
  10. nodeExternals({
  11. whitelist: ['webpack/hot/poll?100'],
  12. }),
  13. ],
  14. plugins: [
  15. ...options.plugins,
  16. new webpack.HotModuleReplacementPlugin(),
  17. new webpack.WatchIgnorePlugin([/\.js$/, /\.d\.ts$/]),
  18. new StartServerPlugin({ name: options.output.filename }),
  19. ],
  20. };
  21. };

This function takes the original object containing the default webpack configuration and returns a modified one with an applied HotModuleReplacementPlugin plugin.

Hot-Module Replacement

To enable HMR, open the application entry file (main.ts) and add the following webpack-related instructions:

  1. declare const module: any;
  2. async function bootstrap() {
  3. const app = await NestFactory.create(AppModule);
  4. await app.listen(3000);
  5. if (module.hot) {
  6. module.hot.accept();
  7. module.hot.dispose(() => app.close());
  8. }
  9. }
  10. bootstrap();

To simplify the execution process, add a script to your package.json file.

  1. "start:dev": "nest build --webpack --webpackPath webpack-hmr.config.js"

Now simply open your command line and run the following command:

  1. $ npm run start:dev

Without CLI

If you are not using the Nest CLI, the configuration will be slightly more complex (will require more manual steps).

Installation

First install the required packages:

  1. $ npm i --save-dev webpack webpack-cli webpack-node-externals ts-loader start-server-webpack-plugin

Configuration

Once the installation is complete, create a webpack.config.js file in the root directory of your application.

  1. const webpack = require('webpack');
  2. const path = require('path');
  3. const nodeExternals = require('webpack-node-externals');
  4. const StartServerPlugin = require('start-server-webpack-plugin');
  5. module.exports = {
  6. entry: ['webpack/hot/poll?100', './src/main.ts'],
  7. watch: true,
  8. target: 'node',
  9. externals: [
  10. nodeExternals({
  11. whitelist: ['webpack/hot/poll?100'],
  12. }),
  13. ],
  14. module: {
  15. rules: [
  16. {
  17. test: /.tsx?$/,
  18. use: 'ts-loader',
  19. exclude: /node_modules/,
  20. },
  21. ],
  22. },
  23. mode: 'development',
  24. resolve: {
  25. extensions: ['.tsx', '.ts', '.js'],
  26. },
  27. plugins: [
  28. new webpack.HotModuleReplacementPlugin(),
  29. new StartServerPlugin({ name: 'server.js' }),
  30. ],
  31. output: {
  32. path: path.join(__dirname, 'dist'),
  33. filename: 'server.js',
  34. },
  35. };

This configuration tells webpack a few essential things about your application: location of the entry file, which directory should be used to hold compiled files, and what kind of loader we want to use to compile source files. Generally, you should be able to use this file as-is, even if you don’t fully understand all of the options.

Hot-Module Replacement

To enable HMR, open the application entry file (main.ts) and add the following webpack-related instructions:

  1. declare const module: any;
  2. async function bootstrap() {
  3. const app = await NestFactory.create(AppModule);
  4. await app.listen(3000);
  5. if (module.hot) {
  6. module.hot.accept();
  7. module.hot.dispose(() => app.close());
  8. }
  9. }
  10. bootstrap();

To simplify the execution process, add a script to your package.json file.

  1. "start:dev": "webpack --config webpack.config.js"

Now simply open your command line and run the following command:

  1. $ npm run start:dev

A working example is available here.