webpack#WebpackOptionsNormalized TypeScript Examples

The following examples show how to use webpack#WebpackOptionsNormalized. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: optimization.ts    From backstage with Apache License 2.0 5 votes vote down vote up
optimization = (
  options: BundlingOptions,
): WebpackOptionsNormalized['optimization'] => {
  const { isDev } = options;

  return {
    minimize: !isDev,
    minimizer: [
      new ESBuildMinifyPlugin({
        target: 'es2019',
        format: 'iife',
      }),
    ],
    runtimeChunk: 'single',
    splitChunks: {
      automaticNameDelimiter: '-',
      cacheGroups: {
        default: false,
        // Put all vendor code needed for initial page load in individual files if they're big
        // enough, if they're smaller they end up in the main
        packages: {
          chunks: 'initial',
          test(module: any) {
            return Boolean(
              module?.resource?.match(/[\\/]node_modules[\\/](.*?)([\\/]|$)/),
            );
          },
          name(module: any) {
            // get the name. E.g. node_modules/packageName/not/this/part.js
            // or node_modules/packageName
            const packageName = module.resource.match(
              /[\\/]node_modules[\\/](.*?)([\\/]|$)/,
            )[1];

            // npm package names are URL-safe, but some servers don't like @ symbols
            return packageName.replace('@', '');
          },
          filename: isDev
            ? 'module-[name].js'
            : 'static/module-[name].[chunkhash:8].js',
          priority: 10,
          minSize: 100000,
          minChunks: 1,
          maxAsyncRequests: Infinity,
          maxInitialRequests: Infinity,
        } as any, // filename is not included in type, but we need it
        // Group together the smallest modules
        vendor: {
          chunks: 'initial',
          test: /[\\/]node_modules[\\/]/,
          name: 'vendor',
          priority: 5,
          enforce: true,
        },
      },
    },
  };
}