我正在使用Vite作为我的构建工具,为一个苗条的前端应用程序。
我使用的一些库是需要缓冲区模块的Node.js模块。为了提供缓冲区功能,我使用了优秀的缓冲器模块。我需要缓冲功能,以使我的应用程序工作,所以我不能只是摆脱了一个简单的填充。我的Vite配置如下所示:
export default defineConfig(({ command, mode }) => {
build: {
target: "esnext",
rollupOptions: {
plugins: [inject({ Buffer: ['buffer', 'Buffer'] })]
}
},
optimizeDeps: {
esbuildOptions: {
define: { global: 'globalThis' },
plugins: [NodeGlobalsPolyfillPlugin({ process: true })]
}
},
plugins: [svelte({
experimental: { prebundleSvelteLibraries: true },
preprocess: [sveltePreprocess({ typescript: true }), optimizeImports()]
})]
});问题是,虽然我的产品构建可以工作(缓冲区功能是由inject插件注入到rollupOptions中),但是开发构建不使用汇总,因此失败了。如何使生产和开发构建同时注入需要的缓冲区库?
发布于 2022-08-15 09:03:43
我不明白为什么多填不能解决你的问题。
尝试将这段代码放在条目JS文件的开头(可能是src/index.js),以确保它在使用 Buffer global之前执行。
import { Buffer } from 'buffer';
globalThis.Buffer = Buffer; // <-- polyfillhttps://stackoverflow.com/questions/73310246
复制相似问题