成功编译后,需要缓冲区的模块无法正常工作

时间:2019-12-11 01:06:54

标签: commonjs svelte rollupjs

我正在尝试使用导入buffer的{​​{3}}。我都安装了:

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

如果需要,我可以提供一个示例,但也许已经很简单,却很简单。

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

1 个答案:

答案 0 :(得分:0)

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