Skip to content

Not able to pass swc options and generate sourceMaps #3

@Crismon96

Description

@Crismon96

Thanks for creating this configuration so that we can have more performant nest applications.
I want to generate sourceMaps for my bundle to find bugs more easily. Unfortunately I am not able to configure swc in your setup, swcOptions can not be configured by the exported functions.

I then copied your implementation for a production build and adjusted only the sourceMap parameter.
Unfortunately the output does not include any source map files whatsoever. I am not the most experienced in terms of this build tooling so maybe you can help me find the issue.

This is my configuration (a copy of your production config only that it should pass sourceMaps: true as an options to swc):

const CopyPlugin = require('copy-webpack-plugin');
const path = require('path');
const nodeExternals = require('webpack-node-externals');
const TerserPlugin = require('terser-webpack-plugin');

const swcConfig = {
  sourceMaps: true,
  module: {
    type: 'commonjs',
    strict: true,
  },
  jsc: {
    target: 'es2020',
    parser: {
      syntax: 'typescript',
      decorators: true,
    },
    transform: {
      legacyDecorator: true,
      decoratorMetadata: true,
    },
    keepClassNames: true,
  },
};

const baseConfig = {
  externals: [
    // HINT: We need to include this package inside the build to handle missing dependencies errors, and tree-shake it
    nodeExternals({
      modulesFromFile: true,
      allowlist: ['webpack/hot/poll?100', '@rnw-community/nestjs-webpack-swc'],
    }),
  ],
  externalsPresets: { node: true },
  cache: {
    type: 'filesystem',
    cacheDirectory: path.resolve(process.cwd(), '.build_cache'),
    allowCollectingMemory: true,
  },
  module: {
    rules: [
      {
        test: /\.ts?$/u,
        use: { loader: 'swc-loader', options: { ...swcConfig, minify: false, sourceMaps: true } },
        exclude: /(node_modules)/u,
      },
    ],
  },
  node: {
    __dirname: false,
    __filename: false,
  },

  mode: 'production',
  optimization: {
    minimize: true,
    minimizer: [
      new TerserPlugin({
        parallel: true,
        terserOptions: {
          keep_classnames: true,
          mangle: false,
        },
      }),
    ],
  },
};

// Add CopyPlugin to the plugins array
baseConfig.plugins = [
  ...(baseConfig.plugins || []), // Spread existing plugins
  new CopyPlugin({
    patterns: [
      { from: 'templates/**/*.xlsx', to: '[path][name][ext]' },
      { from: '../../libs/i18n/backend/src/locales', to: 'locales' },
    ],
  }),
];

// Export the modified configuration
module.exports = baseConfig;

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions