【问题标题】:How to import GUN SEA with Deno Fresh?如何使用 Deno Fresh 导入 GUN SEA?
【发布时间】:2022-10-16 01:12:49
【问题描述】:

从 Fresh 导入 GUN 的第一次尝试是将 esm 中的 gun 库添加到 import_map.json,它在使用 GUN 的简单示例中正确工作。

{
  "imports": {
    ...
    "gun": "https://esm.sh/gun@0.2020.1237",
  }
}

但是当我想另外导入gun/sea时出现问题, 导入import Sea from "gun/sea"; 后。

我收到了这个错误:

error: Uncaught (in promise) Error: Dynamic require of "./lib/text-encoding" is not supported

在 GitHub 上,当出现此类问题时,我阅读了在导入 SEA 之前导入 gun/lib/mobile。 但这带来了一个额外的问题:

error: Uncaught (in promise) TypeError: Assignment to constant variable.

我检查了gun/lib/mobile 文件,它确实包含几行全局变量:

import Buffer from "buffer";
import { TextEncoder, TextDecoder } from "text-encoding";
global.Buffer = global.Buffer || Buffer.Buffer;
global.TextEncoder = TextEncoder;
global.TextDecoder = TextDecoder;

有没有机会让 SEA 在 Fresh 上工作?

【问题讨论】:

  • 这看起来像是 ESM 代码转换的错误或 Gun 源的问题;我建议在github.com/amark/gun/issues/new 创建一个问题;或者,您可以尝试 Deno 对“npm:”前缀模块的不稳定支持(即尝试 import GUN from "npm:gun";

标签: deno gun gundb freshjs


【解决方案1】:

import_map.json 文件的导入中添加以下行:

"@gun": "https://esm.sh/gun@0.2020.1237"

然后使用此导入语句在任何路由/组件中导入 GUN:

import GUN from "@gun"

用法(来自 GUN 文档):

const gun = GUN();

gun.get('mark').put({
  name: "Mark",
  email: "mark@gun.eco",
});

gun.get('mark').on((data, key) => {
  console.log("realtime updates:", data);
});

setInterval(() => { gun.get('mark').get('live').put(Math.random()) }, 9);

【讨论】:

    猜你喜欢
    • 2022-10-08
    • 2022-10-12
    • 2021-09-25
    • 2020-12-03
    • 1970-01-01
    • 2021-05-16
    • 2019-02-05
    • 2021-03-06
    • 2023-01-21
    相关资源
    最近更新 更多