使用 vite 时如何排除 https 包?

How can I exclude the https package when using vite?

提问人:Eric G 提问时间:10/25/2023 最后编辑:Eric G 更新时间:10/26/2023 访问量:84

问:

使用默认项目重现该问题很简单:

yarn create vite my-vue-app --template vue
cd my-vue-app
yarn add cesium vite-plugin-cesium vite -D

更新到:vite.config.js

// vite.config.js
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import cesium from 'vite-plugin-cesium'
import path from 'path'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [vue(), cesium()],
  resolve: {
    alias: {
      '@': path.resolve(__dirname, './src')
    }
  }
})

在 HelloWorld.vue 中,导入 铯import * as Cesium from 'cesium'

然后,运行 ,生成以下错误:yarn dev

✘ [ERROR] Failed to resolve entry for package "https". The package may have incorrect main/module/exports specified in its package.json. [plugin vite:dep-pre-bundle]

    node_modules/esbuild/lib/main.js:1373:21:
      1373 │         let result = await callback({
           ╵                      ^

    at packageEntryFailure (file:///home/me/projects/test/my-vue-app/node_modules/vite/dist/node/chunks/dep-bb8a8339.js:28725:11)
    at resolvePackageEntry (file:///home/me/projects/test/my-vue-app/node_modules/vite/dist/node/chunks/dep-bb8a8339.js:28722:5)
    at tryNodeResolve (file:///home/me/projects/test/my-vue-app/node_modules/vite/dist/node/chunks/dep-bb8a8339.js:28453:20)
    at Context.resolveId (file:///home/me/projects/test/my-vue-app/node_modules/vite/dist/node/chunks/dep-bb8a8339.js:28212:28)
    at Object.resolveId (file:///home/me/projects/test/my-vue-app/node_modules/vite/dist/node/chunks/dep-bb8a8339.js:44276:64)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async file:///home/me/projects/test/my-vue-app/node_modules/vite/dist/node/chunks/dep-bb8a8339.js:65922:21
    at async file:///home/me/projects/test/my-vue-app/node_modules/vite/dist/node/chunks/dep-bb8a8339.js:39976:34
    at async requestCallbacks.on-resolve (/home/me/projects/test/my-vue-app/node_modules/esbuild/lib/main.js:1373:22)
    at async handleRequest (/home/me/projects/test/my-vue-app/node_modules/esbuild/lib/main.js:729:13)


  This error came from the "onResolve" callback registered here:

    node_modules/esbuild/lib/main.js:1292:20:
      1292 │       let promise = setup({
           ╵                     ^

    at setup (file:///home/me/projects/test/my-vue-app/node_modules/vite/dist/node/chunks/dep-bb8a8339.js:39956:19)
    at handlePlugins (/home/me/projects/test/my-vue-app/node_modules/esbuild/lib/main.js:1292:21)
    at buildOrContextImpl (/home/me/projects/test/my-vue-app/node_modules/esbuild/lib/main.js:978:5)
    at Object.buildOrContext (/home/me/projects/test/my-vue-app/node_modules/esbuild/lib/main.js:786:5)
    at /home/me/projects/test/my-vue-app/node_modules/esbuild/lib/main.js:2186:68
    at new Promise (<anonymous>)
    at Object.context (/home/me/projects/test/my-vue-app/node_modules/esbuild/lib/main.js:2186:27)
    at Object.context (/home/me/projects/test/my-vue-app/node_modules/esbuild/lib/main.js:2026:58)
    at prepareEsbuildOptimizerRun (file:///home/me/projects/test/my-vue-app/node_modules/vite/dist/node/chunks/dep-bb8a8339.js:46041:35)

  The plugin "vite:dep-pre-bundle" was triggered by this import

    node_modules/@cesium/engine/Source/Core/Resource.js:2076:48:
      2076 │       return URL.protocol === "https:" ? import("https") : import("http");
           ╵                                                 ~~~~~~~

奇怪的是,该包在运行该包的浏览器环境中不存在。为什么会引用它?https

我相信应该可以通过将包标记为“外部”(以及其他类似的包,如 http、url 和 zlib)来删除对它的引用。但是,我不确定如何使用 vite 做到这一点。

尝试了两件事是添加:

build: {
    rollupOptions: {
        external: ['https', 'http', 'url', 'zlib']
    }
  },

  optimizeDeps: {
    exclude: ['https'],
  },

vite.config.js,但行为保持不变。

如何配置默认项目,以便不引用 https 包?

vue.js 配置 vite cesiumjs

评论

0赞 Estus Flask 10/26/2023
看起来像是包裹的故障。考虑到正在使用 esbuild,为什么单独的 rollup 选项不起作用是有道理的,可能这样的事情会 stackoverflow.com/questions/69286329/......
0赞 Eric G 10/26/2023
好奇。。。你能重现这个问题吗?我有几份报告说,它在我的机器之外不容易复制......

答: 暂无答案