Skip to content

nestjs使用rspack编译无法热更新 #291

@ying-bin

Description

@ying-bin

按照nestjs项目示例配置热更新,无法生效

使用的是@nestjs/platform-fastify框架
rspack配置如下:

const { join, resolve } = require('path');
const { rspack } = require('@rspack/core');
const nodeExternals = require('webpack-node-externals');
const { RunScriptWebpackPlugin } = require('run-script-webpack-plugin');
const { TsCheckerRspackPlugin } = require('ts-checker-rspack-plugin');

const config = {
  context: __dirname,
  mode: 'development',
  target: 'node',
  devtool: 'inline-source-map',
  entry: {
    main: ['@rspack/core/hot/poll?100', './src/main.ts'],
  },
  output: {
    devtoolModuleFilenameTemplate: (info) => {
      return resolve(info.absoluteResourcePath);
    },
    path: join(__dirname, 'dist'),
    filename: 'main.js',
    library: {
      type: 'commonjs2',
    },
    clean: true,
  },
  externals: [
    nodeExternals({
      allowlist: ['@rspack/core/hot/poll?100'],
    }),
  ],
  resolve: {
    extensions: ['.tsx', '.ts', '.js', '.json', '.node'],
    tsConfig: {
      configFile: resolve('tsconfig.json'),
    },
  },
  module: {
    rules: [
      {
        test: /\.node$/,
        loader: 'node-loader',
        options: {
          name: '[path][name].[ext]',
        },
      },
      {
        test: /\.tsx?$/,
        exclude: /node_modules/,
        loader: 'builtin:swc-loader',
        options: {
          sourceMaps: true,
          jsc: {
            parser: {
              syntax: 'typescript',
              tsx: true,
              decorators: true,
              dynamicImport: true,
            },
            transform: {
              legacyDecorator: true,
              decoratorMetadata: true,
            },
            target: 'es2017',
            loose: true,
            externalHelpers: false,
            keepClassNames: true,
          },
          module: {
            type: 'commonjs',
            strict: false,
            strictMode: true,
            lazy: false,
            noInterop: false,
          },
        },
      },
    ],
  },
  optimization: {
    minimize: false,
    nodeEnv: false,
  },
  plugins: [
    new rspack.EnvironmentPlugin({
      NODE_ENV: 'development',
    }),
    new RunScriptWebpackPlugin({
      name: 'main.js',
      autoRestart: false,
    }),
    new rspack.HotModuleReplacementPlugin(),
    new rspack.CopyRspackPlugin({
      patterns: [{ from: 'public', to: 'public' }, { from: 'templates', to: 'templates' }, { from: 'i18n', to: 'i18n' }, { from: 'config', to: 'config' }],
    }),
    new TsCheckerRspackPlugin({
      typescript: {
        configFile: resolve('tsconfig.json'),
      },
    }),
  ],
  devServer: {
    hot: true,
    devMiddleware: {
      writeToDisk: true,
    },
  },
  cache: true,
  experiments: {
    cache: {
      type: 'persistent',
    },
  },
  watch: true,
  watchOptions: {
    ignored: /node_modules/,
    poll: 100,
  },
};

module.exports = config;

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions