如何为 BuckleScript / ReasonML 打包节点模块?

How do I package a node module for BuckleScript / ReasonML?

背景

我是 BuckleScript 的绝对初学者,虽然我以前用 npm 下载过包,但我从未编写过库。

目标:使用 npm 在我的项目中安装我的新包本地包

我正在尝试将 service worker api 的某些部分包装在 JavaScript 中。我从一个文件 bs-service-worker/src/ExtendableEvent.re 开始,就像这样

type _extendableEvent('a);
type extendableEvent_like('a) = Dom.event_like(_extendableEvent('a));
type extendableEvent = extendableEvent_like(Dom._baseClass);

[@bs.send] external waitUntil: (extendableEvent, Js.Promise.t('a)) => unit
    = "waitUntil";

这会按预期编译并生成 ExtendableEvent.bs.js。

不过现在,我想通过创建一个新的 npm 项目并导入我在本地拥有的东西来继续测试我目前拥有的东西。我创建了一个新的同级目录并执行了 npm install ../bs-service-worker。成功了,然后我对我的新 BuckleScript 项目进行了完整性检查。那也成功了。

问题:打开我的模块导致错误

当我在新项目中将open ExtendableEvent;添加到Demo.re时,出现以下错误:

  We've found a bug for you!
  /home/el/workbench/bucklescript/bs-service-worker-examples/src/Demo.re 11:6-20

   9 │  
  10 │ /**/  
  11 │ open ExtendableEvent;  
  12 │   
  13 │ /*  

  The module or file ExtendableEvent can't be found.
  - If it's a third-party dependency:  
    - Did you list it in bsconfig.json?  
    - Did you run `bsb` instead of `bsb -make-world`  
      (latter builds third-parties)?  
  - Did you include the file's directory in bsconfig.json?  

我试过的

更多代码:

bs-service-worker/bs-config.json

{
  "name": "bs-service-worker",
  "version": "0.1.0",
  "sources": {
    "dir" : "src",
      "subdirs" : true,
      "public": "all"
  },
  "package-specs": {
    "module": "commonjs",
    "in-source": true
  },
  "suffix": ".bs.js",
  "bs-dependencies": [

  ],
  "warnings": {
    "error" : "+101"
  },
  "namespace": true,
  "refmt": 3
}

bs-service-worker-examples/bsconfig.json

{
    "name": "bs-service-worker-examples",
    "version": "0.1.0",
    "sources": {
      "dir" : "src",
      "subdirs" : true
    },
    "package-specs": {
        "module": "commonjs",
        "in-source": true
    },
    "suffix": ".bs.js",
    "bs-dependencies": [
        "bs-service-worker",
        "bs-fetch", 

    ],
    "warnings": {
        "error" : "+101"
    },
    "namespace": true,
    "refmt": 3
}

bs-service-worker-examples/package.json

{
  "name": "bs-service-worker-examples",
  "version": "0.0.1",
  "scripts": {
    "build": "npx bsb -make-world",
    "start": "npx bsb -make-world -w",
    "clean": "npx bsb -clean-world"
  },
  "keywords": [
    "BuckleScript"
  ],
  "author": "Eleanor (https://webbureaucrat.bitbucket.io)",
  "license": "MIT",
  "devDependencies": {
    "bs-platform": "^7.3.2"
  },
  "dependencies": {
    "bs-fetch": "^0.6.1",
    "bs-service-worker": "file:../bs-service-worker"
  }
}

轻松重现问题

重现此问题的最快方法是分叉 this repository 并尝试将其添加为本地 npm 依赖项。

问题似乎是你的库 bsconfig.json 中有 "namespace": true,这会将所有模块包装在一个命名空间模块中,并根据 name 生成一个愚蠢的名称场地。在这种情况下,我认为它将是 BsServiceWorker

您可以删除该设置,或将其设置为 false,但命名空间是避免来自不同库的模块或您自己的应用程序之间发生冲突的好主意,因此我建议将其设置为习惯,明智的名字。例如:

"namespace": "ServiceWorker"

然后您可以在消费者项目中打开 ExtendableEvent

open ServiceWorker.ExtendableEvent;

有关详细信息,请参阅 documentation on the namespace field