首页 > 解决方案 > 在 SvelteKit 中将 esbuild 标记为外部依赖项

问题描述

我正在开发一个 SvelteKit 应用程序,并且我在 API 端点上使用 esbuild 来转换获取 POST 的代码。

我正在从 esbuild 导入 transformSync 来执行此操作:

import { transformSync } from 'esbuild';

这在运行开发服务器以及使用 SvelteKit 在本地构建和运行生产预览时都可以正常工作。但是,当我推送到生产环境时(在 Vercel 上,使用 vercel 适配器),我遇到了以下错误:

Error: The esbuild JavaScript API cannot be bundled. Please mark the "esbuild" package as external so it's not included in the bundle.
More information: The file containing the code for esbuild's JavaScript API (/var/task/index.js) does not appear to be inside the esbuild package on the file system, which usually means that the esbuild package was bundled into another file. This is problematic because the API needs to run a binary executable inside the esbuild package which is located using a relative path from the API code to the executable. If the esbuild package is bundled, the relative path will be incorrect and the executable won't be found.

我尝试将以下内容添加到我的 SvelteKit Vite 配置中:

vite: {
    ssr: {
        external: ['esbuild']
    }
}

和:

vite: {
    optimizeDeps: {
        exclude: ['esbuild']
    }
}

都没有奏效。

谁能在这里指出我做错了什么的正确方向?

标签: vercelvitesveltekitesbuild

解决方案


推荐阅读