Improve tree-shaking

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

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

評価

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

調査の方向性

Read the package build configuration and inspect how it produces dist/d3-shape.mjs; the issue identifies Rollup's output.preserveModules as a possible direction. Run the provided esbuild reproduction to record bundle sizes, then check whether publishing per-module files restores the 3.2.0 behavior while keeping the dist/ layout and bundled CDN builds.

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

説明

Since 3.2.1, the package's default entry points to one bundled file, dist/d3-shape.mjs, instead of separate source files. sideEffects: false used to let bundlers drop unused files. With one file, importing anything keeps that module, and bundlers then have to prove each top-level statement is safe to remove.

Most of them can't be removed, so importing one curve now pulls in nearly all the others.

Reproduction
npm i [email protected]
echo "export { curveLinear } from 'd3-shape'" \
  | npx [email protected] --bundle --minify --format=esm | gzip -c | wc -c
Import 3.2.0 (gzip) 3.2.1 (gzip)
curveLinear 265 B 2,509 B
arc 2,478 B 4,871 B

Other toolchains are affected too, though less.

Cause

The file has top-level statements that bundlers treat as side effects:

  • 17 prototype assignments, such as Linear.prototype = { … }
  • calls such as var catmullRom = (function custom(alpha) { … })(0.5) and const sqrt3 = sqrt(3)

The prototype assignments matter most. Adding /*#__PURE__*/ to all 18 top-level calls only reduced the curveLinear case to about 2.3 KB, and those annotations can't be applied to assignments.

Suggested fix

Publish one file per module again, for example with Rollup's output.preserveModules, while keeping the dist/ layout and the bundled CDN builds. This restores the behavior of 3.2.0 without changing the source.

The same build change shipped in d3-array 3.2.5 and d3-time 3.1.1, which grew in the same way under esbuild: ticks + tickIncrement from 420 B to about 1.3 KB, and timeDay + utcDay from 597 B to about 2.7 KB. I haven't looked at the cause in those packages.

主要言語
JavaScript
スター
2.5k
フォーク
307
平均マージ
4時間 16分
マージ済み PR(30日)
1

環境構築

このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。

はじめの一歩

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

d3/d3-shape のほかの issue

d3/d3-shape の issue をすべて見る

似ている issue

JavaScript の issue をもっと見る

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

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