【问题标题】:Module that requires buffer fails to work after successful compilation编译成功后需要缓冲区的模块无法工作
【发布时间】:2020-04-04 05:58:07
【问题描述】:

我正在尝试使用导入bufferbase58。我都安装了:

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

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

我的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
      }
    }
  }];

【问题讨论】:

  • 在挖掘了依赖之后,我注意到问题不在于rollup-plugin-node-globals提供的缓冲区实现,而是base58的使用。
  • 你解决过这个问题吗?

标签: commonjs svelte rollupjs


【解决方案1】:

对于浏览器的使用,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 包并在您的应用中使用它。

【讨论】:

  • 在玩弄了我的代码子集之后,我能够在不使用 Browserify 的情况下成功地为浏览器编译代码。
  • 太棒了!我的建议是安装 buffer 包,而不是使用 Browserify。好像bs58模块也可以和ArrayBuffer一起使用,但是我没有确认。这是你所做的吗?也许在这里发布您的解决方案作为其他人的答案?
  • @vrde 你能描述一下你是如何让它工作的吗?我有同样的问题。
猜你喜欢
  • 2017-09-02
  • 1970-01-01
  • 2016-04-20
  • 2013-09-02
  • 1970-01-01
  • 1970-01-01
  • 2018-06-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多