rspack icon indicating copy to clipboard operation
rspack copied to clipboard

[Bug]: Struggling with nestjs

Open MIreland opened this issue 7 months ago • 3 comments

System Info

  System:
    OS: macOS 14.6.1
    CPU: (10) arm64 Apple M1 Max
    Memory: 1.20 GB / 32.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 20.18.0 - ~/.nvm/versions/node/v20.18.0/bin/node
    npm: 10.8.2 - ~/.nvm/versions/node/v20.18.0/bin/npm
    pnpm: 9.12.3 - ~/.nvm/versions/node/v20.18.0/bin/pnpm
    bun: 1.0.0 - ~/.bun/bin/bun
  Browsers:
    Chrome: 136.0.7103.114
    Safari: 17.6
  npmPackages:
    @rspack/cli: catalog: => 1.3.8 
    @rspack/core: catalog: => 1.3.8 

Details

I'm struggling to set up nestjs in my local app.

I see a lot of issues that look like this:

Image

I also saw a lot of issues that look like this:

Image

Inspired by https://github.com/web-infra-dev/rspack/issues/2741

I created an rspack config that looks like this:

const { RunScriptWebpackPlugin } = require('run-script-webpack-plugin');
const { rspack } = require('@rspack/core');

/** @type {import('@rspack/cli').Configuration} */
const config = {
    context: __dirname,
    target: 'node',

    entry: {
        main: ['@rspack/core/hot/poll?100', './src/main.ts'],
    },
    resolve: {
        extensions: ['...', '.ts', '.tsx', '.jsx'],
    },
    module: {
        rules: [
            {
                test: /\.ts$/,
                use: {
                    loader: 'builtin:swc-loader',
                    options: {
                        jsc: {
                            parser: {
                                syntax: 'typescript',
                                decorators: true,
                            },
                            transform: {
                                legacyDecorator: true,
                                decoratorMetadata: true,
                            },
                        },
                    },
                },
            },
        ],
    },
    optimization: {
        minimize: false,
        minimizer: [
            new rspack.SwcJsMinimizerRspackPlugin({
                minimizerOptions: {
                    // We need to disable mangling and compression for class names and function names for Nest.js to work properly
                    // The execution context class returns a reference to the class/handler function, which is for example used for applying metadata using decorators
                    // https://docs.nestjs.com/fundamentals/execution-context#executioncontext-class
                    compress: {
                        keep_classnames: true,
                        keep_fnames: true,
                    },
                    mangle: {
                        keep_classnames: true,
                        keep_fnames: true,
                    },
                },
            }),
        ],
    },
    externalsType: 'commonjs',
    plugins: [
        !process.env.BUILD &&
            new RunScriptWebpackPlugin({
                name: 'main.js',
                autoRestart: false,
            }),
    ].filter(Boolean),
    devServer: {
        devMiddleware: {
            writeToDisk: true,
        },
    },
    externals: [
        function (obj, callback) {
            const resource = obj.request;
            const lazyImports = [
                '@nestjs/core',
                '@nestjs/microservices',
                '@nestjs/platform-express',
                'cache-manager',
                'class-validator',
                'class-transformer',
                // ADD THIS
                '@nestjs/microservices/microservices-module',
                '@nestjs/microservices/server',
                '@nestjs/websockets',
                'amqplib',
                'nats',
                'socket.io-adapter',
                'utf-8-validate',
                'bufferutil',
                'kerberos',
                '@mongodb-js/zstd',
                'snappy',
                '@aws-sdk/credential-providers',
                'mongodb-client-encryption',
                '@nestjs/websockets/socket-module',
                'bson-ext',
                'snappy/package.json',
                'aws4',
                'lua',
                'sharp',
                'amqp-connection-manager',
                'swc-loader',
                'node-gyp',
                'pg-native',
                'loader-utils',
                'fsevents',
                'kafkajs',
                'mqtt',
                '@swc/wasm',
                '@swc/core',
                '@swc/core-darwin-arm64',
                'swc+core-darwin-arm64',
                '@apollo/gateway',
                'apollo-server-fastify',
                'mock-aws-s3',
                '@mapbox+node-pre-gyp',
                'ts-morph',
            ];
            const requireImports = ['node-gyp', 'shard', 'sharp', 'fsevents'];
            if (requireImports.includes(resource)) {
                require.resolve(resource);
                callback(null, resource);
                return;
            }

            if (!lazyImports.includes(resource)) {
                return callback();
            }
            try {
                require.resolve(resource);
            } catch (err) {
                callback(null, resource);
            }
            callback();
        },
    ],
};
module.exports = config;

Reproduce link

https://github.com/MIreland/nestjs-rspack

Reproduce Steps

  1. check out that repo
  2. pnpm install
  3. pnpm dev
Image

MIreland avatar May 17 '25 01:05 MIreland

Also, how do I know what I should/shouldn't be putting in lazyImports in that config?

MIreland avatar May 17 '25 01:05 MIreland

Hi, I’d like to work on this issue. Can I be assigned?

kusham avatar Sep 07 '25 12:09 kusham

I was about to create a similar issue, having hard time setting up rspack with NestJS. I have a very minimal rspack.config and it's working. But I'm having an issue with Swagger docs, most DTOs appears empty object, seems like rspack are trimming reflect metadata for DTOs.

mhadi512 avatar Sep 18 '25 05:09 mhadi512