Carlton
Carlton

Reputation: 669

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

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?
    ],
},

Upvotes: 18

Views: 38785

Answers (1)

tony19
tony19

Reputation: 138206

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:.*/,
      ]
    }
  }
})

Upvotes: 31

Related Questions