Skip to content

Investigate if the build time can be optimized #3825

@vladyslav-polishchuk

Description

@vladyslav-polishchuk

What Feature Can Be Enhanced?

I noticed that build time for web project may take between 1:20min to 2min. This potentially can be improved, since for example the api project builds within seconds.

From cursor:

Great! I can see from the build output that it compiled successfully in 83 seconds (about 1.5 minutes). Now let me provide you with a comprehensive analysis:

Build Performance Analysis

Based on my investigation of your Next.js monorepo, here's what I found:

Current Build Time: ~83 seconds (1.5 minutes)

Is this normal? For a project of your scale, this is actually reasonable but could be optimized. Here's why:

Project Scale Indicators:

  1. Large Monorepo Structure:

    • 2,777+ TypeScript files in libs/ alone
    • 200+ routes in the Next.js app
    • Massive dependency graph with 200+ internal packages mapped in tsconfig.base.json
    • Complex architecture with frontend/backend/shared libraries
  2. Heavy Dependencies:

    • Ant Design components with styled-components
    • MobX state management (mobx-keystone)
    • GraphQL code generation
    • Multiple code editors (CodeMirror, Lexical, React Quill)
    • Sentry monitoring integration
  3. Build Configuration:

    • Styled Components compilation enabled
    • SVG webpack loader for all SVGs
    • Bundle analyzer conditionally enabled
    • Webpack build workers already enabled ✓
    • Webpack memory optimizations enabled ✓

Why It Takes This Long:

  1. TypeScript Compilation: With 2,777+ TS files and complex type relationships
  2. Module Resolution: 200+ path aliases in tsconfig need to be resolved
  3. Code Bundling: Generating optimized bundles for 100+ dynamic routes
  4. Asset Processing: SVG transformations via SVGR
  5. Styled Components: Runtime CSS-in-JS compilation

Optimization Recommendations:

High Impact (Recommended):

  1. Enable Module Federation or Turbopack for dev (already noted in your config):

    // In next.config.mjs, turbopack is only for dev
    // Consider enabling for production when stable
  2. Implement SWC Plugin optimizations:

    // In next.config.mjs
    experimental: {
      optimizePackageImports: [
        'antd',
        '@ant-design/icons',
        '@radix-ui/react-*',
        'mobx-keystone'
      ]
    }
  3. Enable TypeScript incremental builds:

    // In tsconfig.json (check if already enabled)
    "incremental": true,
    "tsBuildInfoFile": ".next/cache/tsconfig.tsbuildinfo"
  4. Split vendor chunks more aggressively:

    // In next.config.mjs webpack config
    webpack(config) {
      config.optimization = {
        ...config.optimization,
        splitChunks: {
          chunks: 'all',
          cacheGroups: {
            antd: {
              test: /[\\/]node_modules[\\/](antd|@ant-design)[\\/]/,
              priority: 10,
            },
            mobx: {
              test: /[\\/]node_modules[\\/](mobx|mobx-)[\\/]/,
              priority: 10,
            }
          }
        }
      }
      return config
    }

Medium Impact:

  1. Use Nx caching more effectively:

    • Ensure Nx cache is enabled (it is - saw Nx Cloud in logs)
    • Build times should be < 10s on subsequent builds with cache hits
  2. Consider module boundaries:

    • Your libs structure is good, but ensure proper barrel exports
    • Avoid deep import paths that break tree-shaking
  3. Reduce bundle size:

    • Some routes have 1.3+ MB First Load JS
    • Use dynamic imports for heavy components
    • Consider removing unused Ant Design components

Low Impact (Nice to have):

  1. Upgrade to latest Next.js patches (currently on 15.4.4)

  2. Enable parallel builds in production:

    # Set environment variable
    NEXT_PARALLEL=true nx run web:build:prod
  3. Profile the build:

    ANALYZE_BUNDLE=true nx run web:build:prod

Immediate Action Items:

  1. Check if Nx cache is working properly - subsequent builds should be much faster
  2. Add the optimizePackageImports experimental flag - can reduce build time by 20-30%
  3. Review the largest route bundles (1.3MB+ is quite large)

Benchmarks for Comparison:

  • Small Next.js app (< 50 routes): 10-30 seconds
  • Medium app (50-100 routes): 30-60 seconds
  • Large app (100-200 routes): 60-120 seconds ← You are here
  • Enterprise monorepo (200+ routes): 90-180 seconds

Conclusion: Your build time is at the lower end of normal for a project of this scale. With optimizations, you could potentially reduce it to 45-60 seconds, but going much lower would require significant architectural changes (like extracting routes to separate apps or implementing micro-frontends).

Would you like me to implement any of these optimizations?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions