Recommend `node`/`default` conditions instead of `require`/`import` as a solution to the dual package hazard
まだ誰も着手していません。
- 主要言語
- JavaScript
- スター
- 122k
- フォーク
- 37.3k
- 平均マージ
- 4日 2時間
- マージ済み PR(30日)
- 283
説明
Affected URL(s)
https://nodejs.org/api/packages.html#dual-package-hazard
Description of the problem
Publishing packages with dual CommonJS and ESM sources, while has the benefits of supporting both CJS consumers and ESM-only platforms, is known to cause problems because Node.js might load both versions. Example:
package.json |
foo.cjs |
foo.mjs |
|---|---|---|
|
|
|
package.json |
bar.js |
|---|---|
|
|
// my app
import { object as fooObj } from "foo";
import { object as barObj } from "bar";
console.log(fooObj === barObj); // false?????
The two suggested solutions boil down to "even when you have an ESM entrypoint, still use only CJS internallly". This solves the dual package hazard, but completely defeats the cross-platform benefits of dual modules.
If foo instead used these export conditions:
{
"name": "foo",
"exports": {
"node": "./foo.cjs",
"default": "./foo.mjs"
}
}
Then:
- there would be no dual-package hazard in Node.js, because it only ever loads the CommonJS version
- there would be no dual-package hazard in bundlers, because they would only ever load either the
nodeversion (if they are configured to target Node.js) or thedefaultversion (if they are configured to target other platforms). - the package solves the dual-package hazard while still providing an ESM-only version
We have been using this node/default pattern in @babel/runtime for a couple years, because we wanted to provide an ESM-only version for browsers while still avoiding the dual-package hazard (@babel/runtime is mostly stateless, but @babel/runtime/helpers/temporalUndefined relies on object identity of an object defined in a separate file).
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
nodejs.org/api/packages.html#dual-package-hazard にある影響を受ける「Dual package hazard」セクションから始め、この issue にある既存の提案ソリューションと node/default の提案を確認してください。推奨事項について合意に達し、ここで説明されている CommonJS と ESM の動作を含め、そのドキュメントセクションを正確に更新できれば完了です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- javascript, node.js
- 領域
- documentation
- issue の種類
- ドキュメント
- 難易度
- 3/5
- 見積もり時間
- 1〜2日
- 活発さ
- 静か
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 52/100