gpt4 book ai didi

commonjs - 需要缓冲区的模块在成功编译后无法工作

转载 作者:行者123 更新时间:2023-12-04 11:48:03 30 4
gpt4 key购买 nike

我正在尝试使用 base58进口 buffer .我已经安装了两个:

  • https://github.com/calvinmetcalf/rollup-plugin-node-builtins
  • https://github.com/calvinmetcalf/rollup-plugin-node-globals

  • 并将它们添加到插件列表中,但缓冲区似乎未定义。老实说,我不完全确定我在这里做什么,使用这两个废弃的包似乎比预期的要复杂。

    如果需要,我可以提供一个示例,但也许这很简单,已经有一个简单的答案。

    我的 rollup.config.js如下(我删除了一些不重要的部分):

    import alias from "@rollup/plugin-alias";
    import copy from "rollup-plugin-copy";
    import json from "rollup-plugin-json";
    import globals from "rollup-plugin-node-globals";
    import builtins from "rollup-plugin-node-builtins";
    import svelte from "rollup-plugin-svelte";
    import resolve from "rollup-plugin-node-resolve";
    import replace from "@rollup/plugin-replace";
    import commonjs from "rollup-plugin-commonjs";
    import livereload from "rollup-plugin-livereload";
    import { terser } from "rollup-plugin-terser";

    // ...

    export default [
    {
    input: "src/background/",
    output: {
    sourcemap: true,
    format: "iife",
    name: "background",
    file: "build/background/bundle.js"
    },
    plugins: [
    builtins(),
    globals(),

    // If you have external dependencies installed from
    // npm, you'll most likely need these plugins. In
    // some cases you'll need additional configuration —
    // consult the documentation for details:
    // https://github.com/rollup/rollup-plugin-commonjs
    resolve({
    browser: true,
    dedupe: importee =>
    importee === "svelte" || importee.startsWith("svelte/")
    }),
    commonjs(),

    // Watch the `build` directory and refresh the
    // browser on changes when not in production
    //! production && livereload("build"),

    // If we're building for production (npm run build
    // instead of npm run dev), minify
    production && terser()
    ],
    watch: {
    clearScreen: true,
    chokidar: {
    usePolling: true
    }
    }
    }];

    最佳答案

    浏览器使用,their README说要使用 Browserify。

    为了缓冲区兼容性,Browserify says它使用 this package .

    事实上,这似乎没有障碍( REPL ):

    <script>
    import { default as bs58 } from 'bs58'

    // the woes of importing CJS in ESM...
    import { default as B } from 'buffer'
    const { Buffer } = B

    const bytes = Buffer.from('003c176e659bea0f29a3e9bf7880c112b1b31b4dc826268187', 'hex')
    const address = bs58.encode(bytes)

    const out = bs58.decode(address)
    const decoded = out.toString('hex')
    </script>

    {address} => {decoded}

    您应该尝试安装 buffer在本地打包并在您的应用程序中使用它。

    关于commonjs - 需要缓冲区的模块在成功编译后无法工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59277545/

    30 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com