Skip to content

fix: stop tsc clobbering the cjs build and ship real ESM from the module entry - #131

Merged
roderickhsiao merged 2 commits into
v2-pnpmfrom
v2-packaging
Sep 4, 2026
Merged

roderickhsiao merged 2 commits into
v2-pnpmfrom
v2-packaging

Conversation

@roderickhsiao

Copy link
Copy Markdown
Owner

Stacked on react-aspect-ratio#129 (which is itself stacked on react-aspect-ratio#128) — base is v2-pnpm. Merge order: 128 → 129 → this.

Fixes the two pre-existing packaging bugs flagged in #129. Both predate v2 and affect what 1.x published too.

1. tsc was overwriting the CommonJS build

.scripts/prepublish.sh runs babel into dist/, then .scripts/user/prepublish.sh runs tsc --build tsconfig.publish.json for the .d.ts files. That tsconfig inherits outDir: dist, noEmit: false and target: ESNext from the base config — so tsc emitted .js as well and, running second, replaced babel's ES5 CommonJS dist/index.js with its own ESNext output. The published main has never been the babel build the script claims to produce.

tsconfig.publish.json now sets emitDeclarationOnly: true. Switched --build → plain -p at the same time, which also stops tsconfig.publish.tsbuildinfo being emitted into the published dist/.

2. The module entry point shipped CommonJS

The es env in .babelrc set targets.esmodules but not modules: false, so preset-env still downlevelled import/export. dist/es/index.js — the file module points at — shipped require() and exports.__esModule, defeating tree-shaking for every bundler that resolved it.

Added modules: false. Both es/ and next/ now also get a {"type":"module"} marker file, since they are ESM with a .js extension in a package with no top-level "type" — without it Node reparses them (MODULE_TYPELESS_PACKAGE_JSON) and would treat them as CommonJS.

Size

dist/index.js goes 437B → 694B gzipped. That is the fix working, not a regression: it is now the ES5 build it always claimed to be, rather than the ESNext emit that had been overwriting it. Still comfortably under the 2KB budget.

Verification

  • Every target now emits its declared format — main is CJS, module/next are ESM (export function / export default), umd is UMD.
  • Rendered both published entry points through react-dom/server: require('dist/index.js') and import 'dist/es/index.js' produce identical markup (<div class="react-aspect-ratio-placeholder" style="--aspect-ratio:(0.75)">y</div>), with both default and named exports present on each.
  • No MODULE_TYPELESS_PACKAGE_JSON warning after the marker files.
  • pnpm run test:ci 6/6, tsc --noEmit clean, bundlesize 4/4.

🤖 Generated with Claude Code

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The changes align the build outputs with the package entry points; only a minor, non-blocking log message mismatch was found.

Pull request overview

Fixes long-standing packaging issues so the published artifacts match their declared entry points (CJS for main, real ESM for module/next) without TypeScript overwriting Babel output.

Changes:

  • Prevent tsc from overwriting the Babel-generated CJS build by enabling emitDeclarationOnly in tsconfig.publish.json.
  • Stop emitting tsbuildinfo into the publish output by switching from tsc --build to tsc -p.
  • Ensure ESM outputs stay ESM by setting modules: false for the es Babel env and adding per-folder "type":"module" markers for dist/es and dist/next.
File summaries
File Description
tsconfig.publish.json Emits declarations only to avoid clobbering Babel’s JS output in dist/.
.scripts/user/prepublish.sh Uses tsc -p for declaration emission (non-build mode).
.scripts/prepublish.sh Adds ESM marker package.json files to dist/es and dist/next.
.babelrc Preserves ESM syntax for the es build via modules: false.
Review details
  • Files reviewed: 4/4 changed files
  • Comments generated: 1
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread .scripts/user/prepublish.sh Resolved
roderickhsiao added a commit that referenced this pull request Sep 4, 2026
Declarations go to dist/, not dist/types — package.json points `types` at
dist/index.d.ts. Flagged in review on kindred #131.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
roderickhsiao added a commit that referenced this pull request Sep 4, 2026
Declarations go to dist/, not dist/types — package.json points `types` at
dist/index.d.ts. Flagged in review on
[react-aspect-ratio#131](#131).

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
roderickhsiao added a commit that referenced this pull request Sep 4, 2026
Declarations go to dist/, not dist/types — package.json points `types` at
dist/index.d.ts. Flagged in review on
[react-aspect-ratio#131](#131).

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@roderickhsiao
roderickhsiao requested a lite review from Copilot September 4, 2026 07:29
roderickhsiao and others added 2 commits September 4, 2026 00:30
…ule entry

Two long-standing packaging bugs, both predating v2.

tsc shares dist/ with babel and ran second, so `tsc --build tsconfig.publish.json`
overwrote dist/index.js — babel's ES5 CommonJS output — with its own ESNext emit.
The published `main` was never the babel build. tsconfig.publish.json now sets
emitDeclarationOnly, and the plain -p form replaces --build so no tsbuildinfo
lands in dist/ either.

The `es` babel env set targets.esmodules but not modules:false, so preset-env
still downlevelled import/export to CommonJS and the declared `module` entry
shipped require(). Fixed, and the es/ and next/ directories now get a
{"type":"module"} marker so Node reads them as ESM rather than reparsing.

dist/index.js grows 437B -> 694B gzipped: it is now the ES5 build it always
claimed to be, not the ESNext emit that was overwriting it. Still well under
the 2KB budget.

Verified by rendering both entry points through react-dom/server — identical
markup from main and module.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Declarations go to dist/, not dist/types — package.json points `types` at
dist/index.d.ts. Flagged in review on
[react-aspect-ratio#131](#131).

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The publish build script can continue after a failed build step and still exit successfully, which risks publishing an incomplete or incorrect dist/.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Review details
  • Files reviewed: 4/4 changed files
  • Comments generated: 1
  • Review effort level: Lite

Comment thread .scripts/prepublish.sh Resolved
@roderickhsiao
roderickhsiao merged commit 5fc554a into main Sep 4, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants