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

问题描述 投票:0回答:1

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

Rollup failed to resolve import "node:path"

通过将失败的导入添加到汇总选项中,我可以修复错误,但构建会继续抱怨每个

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?
    ],
},
rollupjs vite
1个回答
30
投票

build.rollupOptions.external
也接受正则表达式。以下
RegExp
匹配任何以
node:
开头的字符串:

/^node:.*/

因此配置

external
如下:

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

export default defineConfig({
  build: {
    rollupOptions: {
      external: [
        /^node:.*/,
      ]
    }
  }
})
© www.soinside.com 2019 - 2024. All rights reserved.