Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vite - How do I use a wildcard in Rollupjs build.rollupOptions.external?

Tags:

rollupjs

vite

I'm using Vite to build a library and I get the following error when building the library:

Rollup failed to resolve import "node:path"

By adding the failed import to the Rollup options I'm able to fix the error but the build continues to complain for each node:* import. In the end I've had to add each one individually to the build.rollupOptions.external:

build: {
  rollupOptions: {
    external: [            
      'node:path',           
      'node:https',
      'node:http',
      'node:zlib',
      ... 
    ],
},

While this solves the issue it is time consuming to list each node import individually. Is there instead a way to use some sort of wildcard syntax to automatically resolve all node imports?

build: {
  rollupOptions: {
    external: [             
      'node:*' // i.e. this syntax does not work, is there something similar that would work?
    ],
},
like image 690
Carlton Avatar asked Nov 20 '25 21:11

Carlton


1 Answers

build.rollupOptions.external also accepts regular expressions. The following RegExp matches any string that starts with node::

/^node:.*/

So configure external as follows:

// vite.config.js
import { defineConfig } from 'vite'

export default defineConfig({
  build: {
    rollupOptions: {
      external: [
        /^node:.*/,
      ]
    }
  }
})
like image 161
tony19 Avatar answered Nov 23 '25 10:11

tony19



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!