Skip to content

Webpack Config

Chelsea edited this page Nov 24, 2023 · 3 revisions

Home > Deployments And Infrastructure > Webpack Config


Certain dependencies as well as any new lambdas that are added to the codebase must also be listed in the webpack config in order to build and deploy. The current webpack config file is shown below. Please refer here for more configuration options and documentation.

/**
 * Webpack configuration modelled after https://docs.nestjs.com/faq/serverless
 */

const TerserPlugin = require('terser-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');

module.exports = function (options, webpack) {
  const lazyImports = [
    '@nestjs/microservices/microservices-module',
    '@nestjs/websockets',
    '@nestjs/websockets/socket-module',
    '@nestjs/mapped-types',
    'class-transformer/storage',
    'cache-manager',
    'class-validator',
    'class-transformer',
  ];

  return {
    ...options,
    entry: {
      'src/lambdas/batch-reconcile': './src/lambdas/batch-reconcile.ts',
      'src/lambdas/dailyfilecheck': './src/lambdas/dailyfilecheck.ts',
      'src/lambdas/parser': './src/lambdas/parser.ts',
      'src/lambdas/reconcile': './src/lambdas/reconcile.ts',
      'src/lambdas/report': './src/lambdas/report.ts',
      'src/database/migrate': './src/database/migrate.ts',
      'src/database/seeder': './src/database/seeder.ts',
      'src/database/clear-dev-db': './src/database/clear-dev-db.ts', 
      'src/lambda': './src/lambda.ts'
    },
    target: 'node',
    mode: 'production',
    devtool: 'source-map',
    externals: [],
    optimization: {
      minimize: true,
      splitChunks: {
        chunks: 'all',
        cacheGroups: {
          vendor: {
            test: '/[\\/]node_modules[\\/]/',
            name: 'vendors',
          },
        },
      },
      minimizer: [
        new TerserPlugin({
          terserOptions: {
            keep_classnames: true,
          },
        }),
      ],
    },
    module: {
      rules: [
        {
          test: /.tsx?$/,
          use: [
            {
              loader: 'ts-loader',
              options: {
                transpileOnly: false,
              },
            },
          ],
          exclude: /node_modules/,
        },
      ],
    },
    output: {
      ...options.output,
      filename: '[name].js',
      chunkFilename: '[id].dependencies.js',
      libraryTarget: 'commonjs2',
    },
    plugins: [
      ...options.plugins,
      new CopyWebpackPlugin({
        patterns: [
          '../../node_modules/swagger-ui-dist/swagger-ui.css',
          '../../node_modules/swagger-ui-dist/swagger-ui-bundle.js',
          '../../node_modules/swagger-ui-dist/swagger-ui-standalone-preset.js',
          '../../node_modules/swagger-ui-dist/favicon-16x16.png',
          '../../node_modules/swagger-ui-dist/favicon-32x32.png',
        ],
      }),
      new webpack.IgnorePlugin({
        checkResource(resource) {
          if (/^pg-native$/.test(resource)) {
            return true;
          }

          if (lazyImports.includes(resource)) {
            try {
              require.resolve(resource);
            } catch (err) {
              return true;
            }
          }
          return false;
        },
      }),
    ],
  };
};

Clone this wiki locally