簡體   English   中英

我如何 package BuckleScript / ReasonML 的節點模塊?

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

背景

我是 BuckleScript 的絕對初學者,雖然我之前下載過帶有 npm 的包,但我從未編寫過庫。

目標:使用 npm 在我的項目中安裝我的新 package 本地 package

我正在嘗試將服務人員 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?  

我試過的

  • 我猜我在這里濫用 BuckleScript 而不是 npm 因為 npm 被如此廣泛采用並且有據可查,我認為我已經找到了問題,但我絕對不排除我濫用 ZBB30E85411B45DCF82 的可能性。也。
  • 我確實將“bs-service-worker”列為 bs 依賴項。 我還嘗試了“../bs-service-worker”,以防 BuckleScript 不喜歡虛擬目錄,但它似乎沒有幫助。
  • 我的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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM