【问题标题】:Buffer is not defined in React-viteReact-vite 中没有定义缓冲区
【发布时间】:2022-07-14 17:53:09
【问题描述】:

从 CRA 迁移后未定义缓冲区(创建反应应用程序)

“vite”:“^2.7.12”

我尝试添加插件,为 Buffer 添加定义,但它不起作用。

const viteConfig = defineConfig({
/*    define: {
        "Buffer": {}
    },*/
    plugins: [reactRefresh(), react()],
    build: {
        rollupOptions: {
            input: {
                main: resolve('index.html'),
            },
        },
    },
    clearScreen: false
});

【问题讨论】:

    标签: reactjs web buffer vite


    【解决方案1】:

    安装这个库

    @esbuild-plugins/node-globals-polyfill

    并将其添加到您的 vite.config.js 中

    export default defineConfig({
        // ...other config settings
        optimizeDeps: {
            esbuildOptions: {
                // Node.js global to browser globalThis
                define: {
                    global: 'globalThis'
                },
                // Enable esbuild polyfill plugins
                plugins: [
                    NodeGlobalsPolyfillPlugin({
                        buffer: true
                    })
                ]
            }
        }
    })
    

    将此库导入添加到您的 vite.config.js

    import { NodeGlobalsPolyfillPlugin } from '@esbuild-plugins/node-globals-polyfill'
    

    【讨论】:

    • 我正在使用 vite.config.ts,我怎样才能停止打字稿抱怨@esbuild-plugins/node-globals-polyfill 缺少类型?找不到@types 包。我做过的最好的事情是添加 //ts-ignore,但对此并不满意
    【解决方案2】:

    对我来说,上面的配置不起作用,我不得不在 3 个文件中进行更改, 在 vite.config.ts , index.html 和添加包

    1.安装包

    yarn install process util buffer events
    

    2.更新 vite.config

    import { NodeModulesPolyfillPlugin } from "@esbuild-plugins/node-modules-polyfill";
    import react from "@vitejs/plugin-react";
    import { resolve } from "path";
    import rollupNodePolyFill from "rollup-plugin-node-polyfills";
    import { defineConfig } from "vite";
    
    export default defineConfig({
        plugins: [react()],
        optimizeDeps: {
            esbuildOptions: {
                define: {
                    global: "globalThis",
                },
                plugins: [
                    GlobalPolyFill({
                        process: true,
                        buffer: true,
                    }),
                ],
            },
        },
        resolve: {
            alias: {
                process: "process/browser",
                stream: "stream-browserify",
                zlib: "browserify-zlib",
                util: "util",
            },
        },
    });
    
    

    3.更新 index.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8" />
      <link rel="icon" type="image/svg+xml" href="/src/assets/images/favicon.svg" />
      <meta name="viewport" content="width=device-width, initial-scale=1.0" />
      <title>Vite App</title>
      <script>
        window.global = window;
      </script>
      <script type="module">
        import process from "process";
        import EventEmitter from "events";
        import {Buffer} from "buffer";
        window.Buffer = Buffer;
        window.process = process;
        window.EventEmitter = EventEmitter;
      </script>
    </head>
    
    <body>
      <div id="root"></div>
      <script type="module" src="./src/index.js"></script>
    </body>
    </html>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多