![](/img/trans.png)
[英]How to correctly setup a local ReasonMl / Bucklescript dependencies
[英]How do I package a node module for BuckleScript / ReasonML?
我是 BuckleScript 的絕對初學者,雖然我之前下載過帶有 npm 的包,但我從未編寫過庫。
我正在嘗試將服務人員 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。
不過,現在,我想提前 go 並通過創建一個新的 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?
npm run build
命令確實是npx bsb -make-world
。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"
}
}
重現此問題的最快方法是分叉此存儲庫並嘗試將其添加為本地 npm 依賴項。
問題似乎是你有"namespace": true
在你的庫的bsconfig.json
中,它將所有模塊包裝在一個命名空間模塊中,並根據name
字段生成一個愚蠢的名稱。 在這種情況下,我認為它將是BsServiceWorker
。
您可以刪除該設置,或將其設置為false
,但命名空間是避免來自不同庫或您自己的應用程序的模塊之間的沖突的好主意,因此我建議將其設置為自定義的、合理的名稱。 例如:
"namespace": "ServiceWorker"
然后,您可以使用以下命令在使用者項目中打開ExtendableEvent
:
open ServiceWorker.ExtendableEvent;
有關更多詳細信息,請參閱有關namespace
字段的文檔。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.