nestjs-swc icon indicating copy to clipboard operation
nestjs-swc copied to clipboard

Not able to pass swc options and generate sourceMaps

Open Crismon96 opened this issue 1 year ago • 0 comments
trafficstars

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;

Crismon96 avatar Apr 09 '24 08:04 Crismon96