[Bug]: Bundleless CJS output in a `"type": "module"` package cannot import a sibling's default export

オープン
#2,013 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
3/5
見積もり時間
1〜2日
初心者へのやさしさ
72/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
javascript, node.js, typescript

調査の方向性

Start by reproducing the failure with the provided config: build the bundleless CJS and ESM outputs, then run the two Node.js commands from the issue. Trace how Rslib/Rspack rewrites the sibling default import to require("./b.cjs") and handles __esModule interop. Done when both generated outputs print hi for a package with "type": "module".

索引モデルが issue の本文から書いたものです。

説明

Version
System:
  OS: Linux 6.1 Debian GNU/Linux 12 (bookworm) 12 (bookworm)
  CPU: (64) x64 Intel(R) Xeon(R) CPU @ 2.80GHz
  Memory: 217.67 GB / 251.88 GB
  Container: Yes
  Shell: 5.2.15 - /bin/bash
npmPackages:
  @rslib/core: ^1.0.6 => 1.0.6 (@rspack/core 2.3.1, main b6645e20)
Details

With bundle: false and format: "cjs" in a package with "type": "module", Rslib emits .cjs files and rewrites import greet from "./b.js" to require("./b.cjs"). Because the rewritten request ends in .cjs, Rspack applies Node.js CJS interop and treats the default import as module.exports directly. However, ./b.cjs is Rslib's own CJS output compiled from an ES module (exports["default"] = ... with __esModule: true), so the default import resolves to the module exports object instead of exports.default, and calling it throws TypeError: ... is not a function.

The ESM output of the same project works, and the CJS output also works when "type": "module" is removed from package.json (where output files use .js and Rspack checks __esModule).

Reproduce link

https://gist.github.com/trackoor/1d2c98bfe726648fc3b3f3321a8a6f30

Reproduce Steps

In a project with { "name": "lib", "type": "module" } in package.json:

// src/b.js
export default function greet() {
  return "hi";
}

// src/index.js
import greet from "./b.js";
export const out = greet();

// rslib.config.mjs
import { defineConfig } from "@rslib/core";
export default defineConfig({
  lib: [
    { format: "cjs", bundle: false },
    { format: "esm", bundle: false },
  ],
  source: { entry: { index: "./src/**" } },
});

Run npx rslib build, then run the generated outputs:

node --input-type=module -e 'import { out } from "./dist/index.js"; console.log(out)'
node -e 'console.log(require("./dist/index.cjs").out)'

Actual output:

  • ESM (dist/index.js): hi
  • CJS (dist/index.cjs):
TypeError: external_b_cjs_namespaceObject is not a function

(dist/index.cjs emits const external_b_cjs_namespaceObject = require("./b.cjs"); const out = external_b_cjs_namespaceObject();.)

Expected output:

  • Both print hi.

Credit: https://ucsbplse.github.io/

主要言語
TypeScript
スター
1k
フォーク
68
平均マージ
9時間 13分
マージ済み PR(30日)
52

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

web-infra-dev/rslib のほかの issue

web-infra-dev/rslib の issue をすべて見る

似ている issue

TypeScript の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。