Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon Are there any examples of excluding CommonJS modules in Vite configurations
    undefined


Are there any examples of excluding CommonJS modules in Vite configurations


Yes, there are examples of excluding CommonJS modules in Vite configurations. Here are a few:

1. Exclude Specific Modules:
- You can exclude specific CommonJS modules from being transformed to ESM by using the `exclude` option in the `vite.config.js` file:

javascript
   import { defineConfig } from 'vite';
   import laravel from 'laravel-vite-plugin';

   export default defineConfig({
     plugins: [
       laravel({
         input: [
           'resources/css/app.css',
           'resources/js/app.js',
         ],
         exclude: ['node_modules/react-calendar', 'node_modules/react-date-picker'],
       }),
     ],
   });
   

This configuration excludes the `react-calendar` and `react-date-picker` modules from being transformed[1].

2. Exclude Modules in `optimizeDeps`:
- You can also exclude modules in the `optimizeDeps` section of the configuration:

javascript
   import { defineConfig } from 'vite';
   import laravel from 'laravel-vite-plugin';
   import esbuildCommonjs from 'vite-plugin-commonjs';

   export default defineConfig({
     plugins: [
       laravel({
         input: [
           'resources/css/app.css',
           'resources/js/app.js',
         ],
       }),
       esbuildCommonjs({
         exclude: ['node_modules/react-calendar', 'node_modules/react-date-picker'],
       }),
     ],
   });
   

This configuration excludes the `react-calendar` and `react-date-picker` modules from the optimization process[2].

3. Exclude Modules in `build`:
- You can also exclude modules in the `build` section of the configuration:

javascript
   import { defineConfig } from 'vite';
   import laravel from 'laravel-vite-plugin';

   export default defineConfig({
     plugins: [
       laravel({
         input: [
           'resources/css/app.css',
           'resources/js/app.js',
         ],
       }),
     ],
     build: {
       rollupOptions: {
         input: 'resources/js/app.js',
         exclude: ['node_modules/react-calendar', 'node_modules/react-date-picker'],
       },
     },
   });
   

This configuration excludes the `react-calendar` and `react-date-picker` modules from the build process[3].

These examples demonstrate how to exclude specific CommonJS modules from being transformed to ESM in Vite configurations.

Citations:
[1] https://itf-laravel-9.netlify.app/config/vite.html
[2] https://sebastiandedeyne.com/vite-with-laravel
[3] https://vitejs.dev/guide/features
[4] https://github.com/laravel/vite-plugin/blob/1.x/UPGRADE.md
[5] https://laravel.com/docs/11.x/vite