0.67.0 • Published 3 months ago

@rnw-community/nestjs-webpack-swc v0.67.0

Weekly downloads
-
License
MIT
Repository
github
Last release
3 months ago

NestJS Webpack + SWC configuration

NestJS webpack and SWC configuration helpers, this can speed up your local NestJS development up to 10x.

npm version npm downloads

  • NestJS offers Webpack configuration with HMR which significantly improves rebuild speed within local development especially if your project grows.
  • SWC is next generation of fast developer tools which transpiles typescript blazingly fast compared to Babel, and it has webpack swc-loader

This package helps to easily configure NestJS webpack and SWC integration and make you DX good and quick again

Installation

  1. Install package @rnw-community/nestjs-webpack-swc using your package manager
  2. Install additional peer dependencies:

Configuration

Development webpack configuration

Create webpack-dev.config.js in root of the NestJS package:

module.exports = require('@rnw-community/nestjs-webpack-swc/get-nestjs-webpack-dev.config').getNestJSWebpackDevConfig;

Production webpack configuration

Create webpack-prod.config.js in root of the NestJS package:

module.exports = require('@rnw-community/nestjs-webpack-swc/get-nestjs-webpack-prod.config').getNestJSWebpackProdConfig;

NestJS HMR

For blazing fast DX, this package provides utility to simplify HMR configuration:

import { handleNestJSWebpackHmr } from '@rnw-community/nestjs-webpack-swc';

const bootstrap = async (): Promise<void> => {
    //...
    handleNestJSWebpackHmr(app, module);
};

Scripts package.json

Change package.json build, start:dev scripts:

{
    "build": "nest build --webpack --webpackPath webpack-prod.config.js",
    "start:dev": "nest build --webpack --webpackPath webpack-dev.config.js --watch"
}

.gitignore

For maximum speed webpack is configured to generate filesystem cache and uses .build_cache folder in package root, so you need to add it to your .gitignore file.

Possible webpack issues

Due to webpack bundling approach you may encounter problems with packages that use absolute/relative paths, each of this cases needs separate solutions. Feel free to open an issue.

Typeorm migrations

If your project is using TypeORM, then you will face problems with running migrations from NestJS app, this package provides utility for loading TypeORM migrations within webpack build.

  1. Install additional dev dependencies:
  1. Add this package to your tsconfig.*.json files:
{
    "compilerOptions": {
        "types": ["node", "@types/webpack-env"]
    }
}
  1. Add code that will return class instances array that you can pass to TypeORM configuration, you will only need to provide path to the folder where all migrations are stored:
const migrations = importTypeormWebpackMigrations(require.context('./migration/', true, /\.ts$/u));

Typeorm CLI

With webpack in place you will not have your migrations transpiled into the dist folder anymore so to use typeorm cli you will need additional changes:

  1. Install additional dev dependencies:
  2. Add/change your package.json scripts(you should have tsconfig.build.json with module:commonjs):
{
    "scripts": {
        "typeorm": "ts-node -P tsconfig.build.json -r tsconfig-paths/register ./node_modules/.bin/typeorm",
        "migrate:create": "yarn typeorm migration:create -d ./data-source.mjs",
        "migrate:down": "yarn typeorm migration:revert -d ./data-source.mjs",
        "migrate:generate": "yarn typeorm migration:generate -d ./data-source.mjs",
        "migrate:up": "yarn typeorm migration:run -d ./data-source.mjs"
    }
}

-d ./data-source.mjs is a TypeORM v3 breaking changes you can remove it for v2

SWC possible issues

SWC bindings

If your project is running inside the docker container and your host system has different architecture you may end up with Error: Bindings not found SWC error, this is happening because when you install SWC it uses the bindings for your host machine, to fix this:

  1. Create .yarnrc file in the project root:
--ignore-engines true
--ignore-platform true
  1. Add swc bindings dependencies:
{
    "@swc/core-linux-arm64-musl": "^1.2.242",
    "@swc/core-linux-x64-musl": "^1.2.242"
}

License

This library is licensed under The MIT License.

0.67.0

3 months ago

0.66.0

3 months ago

0.65.2

4 months ago

0.65.3

4 months ago

0.64.0

5 months ago

0.65.0

5 months ago

0.63.0

5 months ago

0.62.1

6 months ago

0.62.0

7 months ago

0.62.2

6 months ago

0.60.0

7 months ago

0.59.3

7 months ago

0.57.2

7 months ago

0.57.3

7 months ago

0.57.0

7 months ago

0.57.1

7 months ago

0.55.0

7 months ago

0.59.6

7 months ago

0.59.4

7 months ago

0.59.5

7 months ago

0.54.12

10 months ago

0.61.0

7 months ago

0.56.0

7 months ago

0.53.0

1 year ago

0.51.0

1 year ago

0.52.3

1 year ago

0.54.0

1 year ago

0.52.0

1 year ago

0.50.0

1 year ago

0.49.2

1 year ago

0.49.0

1 year ago

0.42.0

2 years ago

0.43.0

2 years ago

0.42.1

2 years ago

0.48.2

1 year ago

0.48.0

2 years ago

0.46.0

2 years ago

0.43.3

2 years ago

0.47.0

2 years ago

0.43.4

2 years ago

0.44.0

2 years ago

0.43.1

2 years ago

0.45.0

2 years ago

0.43.2

2 years ago

0.40.0

2 years ago

0.41.0

2 years ago

0.38.2

2 years ago

0.39.0

2 years ago

0.38.1

2 years ago

0.38.0

2 years ago

0.37.0

2 years ago

0.36.1

2 years ago

0.36.0

2 years ago

0.35.1

2 years ago

0.35.0

2 years ago

0.34.0

2 years ago

0.33.0

2 years ago

0.32.1

2 years ago

0.38.3

2 years ago

0.32.0

2 years ago

0.31.1

2 years ago

0.31.0

2 years ago

0.30.0

2 years ago

0.29.0

2 years ago

0.28.0

2 years ago

0.27.4

2 years ago

0.27.3

2 years ago

0.27.2

2 years ago

0.27.1

2 years ago

0.27.0

2 years ago