Vite - 如何在 Rollupjs build.rollupOptions.external 中使用通配符?

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

我正在使用 Vite 构建库,但在构建库时出现以下错误:

Rollup failed to resolve import "node:path"

通过将失败的导入添加到 Rollup 选项中,我能够修复错误,但构建继续为每个 node:* 导入抱怨。最后我不得不将每个单独添加到 build.rollupOptions.external:

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

虽然这解决了问题,但单独列出每个 node 导入非常耗时。有没有一种方法可以使用某种通配符语法来自动解析所有 node 导入?

build: {
  rollupOptions: {
    external: [             
      'node:*' // i.e. this syntax does not work, is there something similar that would work?
    ],
},

build.rollupOptions.external also accepts regular expressions. The following RegExp 匹配任何以 node::

开头的字符串
/^node:.*/

所以配置external如下:

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

export default defineConfig({
  build: {
    rollupOptions: {
      external: [
        /^node:.*/,
      ]
    }
  }
})