Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set custom vite config settings in Angular 17?

We would like to configure vite settings in Angular 17 via a vite.config.js file or some other way. Specifically, these settings:

export default {
  optimizeDeps: {
      disabled: false,
      exclude: [
          'my-third-party-package',
      ],
  },
}

Things I have tried but yielded no results:

  • Running ng eject
  • Reading the official documentation on Angular's new build system (https://angular.io/guide/esbuild#esm-default-imports-vs-namespace-imports)
  • Creating a vite.config.js in the root folder and "hoping" Angular picks it up at build time.
like image 536
MrFullStack Avatar asked Jan 17 '26 21:01

MrFullStack


2 Answers

Probably when you serve the app with the new Angular build system, you will have an error like this:

typeError: Failed to fetch dynamically imported module

This is because some dependencies are not optimized for Vite.

In order to fix this you must set optimizeDeps on your vite.config.js but it is not possible in Angular. See https://github.com/angular/angular-cli/issues/26859


I found the next solution:

Inside angular.json disable cli cache:

"cli": {
  "cache": {
    "enabled": false,
  }
}

See https://github.com/angular/angular-cli/issues/26345

like image 194
Leandro Matilla Avatar answered Jan 20 '26 12:01

Leandro Matilla


Angular documentation provides a way to exclude dependenies from optimization/bundling by Vite using prebundle options in serve task configuration. Example:

"serve": {
  "builder": "@angular/build:dev-server",
  "configurations": {
    "production": {
      "buildTarget": "sapphire-manager-ui:build:production"
    },
    "development": {
      "buildTarget": "sapphire-manager-ui:build:development"
    }
  },
  "options": {
    "prebundle": {
      "exclude": ["tsyringe", "zod", "nanoid"]
    }
  },
  "defaultConfiguration": "development"
},
like image 39
Alexei Klenin Avatar answered Jan 20 '26 11:01

Alexei Klenin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!