Skip to content

Ship native ESM to avoid bundling an extra KaTeX copy - #12

Merged
cyanzhong merged 3 commits into
mainfrom
fix-native-esm-export
Sep 21, 2026
Merged

cyanzhong merged 3 commits into
mainfrom
fix-native-esm-export

Conversation

@cyanzhong

Copy link
Copy Markdown

Why

The packaging fix in #10 made clean installs work, but routed both import and require to CommonJS. MarkEdit-preview imports KaTeX as ESM, while the plugin then required CommonJS KaTeX, so Vite bundled an additional copy. This fixes that packaging regression without changing the parser.

Changes

  • Route import to native dist/index.mjs and require to the existing dist/index.js.
  • Supply a matching .d.mts declaration for native ESM TypeScript consumers, preserving the CommonJS types and existing declaration subpath.
  • Compile and watch both formats from the same source with correct embedded-source maps. CommonJS runtime and source map remain byte-identical to 1.1.7.
  • Add package-entry, type-resolution, source-map, shared KaTeX instance, and fixture-parity checks.
  • Document native ESM usage; no version bump or release tag changes.

Validation

  • All 71 existing assertions pass.
  • All 53 fixtures render identically through ESM and CommonJS.
  • Native ESM/CommonJS imports and NodeNext consumer types pass.
  • Both entry points render from an extracted npm pack tarball.
  • Watch mode updates both runtimes and declarations; repeated compilation passes npm run verify-dist.
  • Full MarkEdit-preview builds were run in memory with deployment disabled and the packed ESM entry, without changing Preview files.
Preview entry JavaScript bytes Gzip bytes
Installed 1.1.7 CommonJS 5,295,115 1,522,885
Original TypeScript source 5,022,788 1,442,220
Proposed packed ESM 5,022,788 1,442,220

The new bundle removes the additional top-level CommonJS KaTeX module and returns exactly to the original-source bundle size. Mermaid retains its own pre-existing KaTeX dependency.

cyanzhong and others added 2 commits September 21, 2026 10:43
Preserve ESM KaTeX imports to avoid bundling an additional CommonJS copy in ESM consumers. Build and watch both formats with matching source maps and declarations, and validate package entry points and rendering parity.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.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.

Copilot review overview

🟡 Changes recommended

Resolve the package-version inconsistency and add runtime/type assertions against an extracted npm pack tarball.

Get a fresh assessment by requesting another Copilot review.

Review effort: Lite
Findings: 1 Low severity

Open (1)
What changed in this PR

Adds native ESM packaging alongside CommonJS to prevent duplicate KaTeX bundling while preserving existing behavior.

Changes:

  • Adds conditional ESM/CommonJS exports and ESM declarations.
  • Builds both formats with source maps and expanded validation.
  • Documents ESM usage and updates generated artifacts.
File Reviewed changes
types.d.ts Declaration formatting updates.
test/​package.js Entry-point, type, source-map, and parity checks.
README.md Native ESM usage documentation.
package.json Conditional exports and dual-format build configuration.
package-lock.json Package metadata synchronization.
dist/​index.mjs.map Generated ESM source map.
dist/​index.mjs Generated native ESM runtime.
dist/​index.d.mts ESM TypeScript declaration.
build/​compile.js Dual-format compilation and watch support.

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

Comment thread package.json Outdated
Keep the ESM packaging PR at version 1.1.6; the maintainer will handle the version bump and release separately.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@cyanzhong
cyanzhong merged commit 40bae4c into main Sep 21, 2026
1 check passed
@cyanzhong
cyanzhong deleted the fix-native-esm-export branch September 21, 2026 06:13
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