Skip to content

Ship ESM and CommonJS builds so packages are importable by bundlers - #186

Merged
MathiasVDA merged 5 commits into
mainfrom
copilot/fix-npm-build-module-exports
Sep 23, 2026
Merged

MathiasVDA merged 5 commits into
mainfrom
copilot/fix-npm-build-module-exports

Conversation

Copilot AI commented Sep 23, 2026 •

Copy link
Copy Markdown

Packages only shipped an IIFE (browser-global) bundle, so import Yasgui from '@matdata/yasgui' failed in Vite/webpack/esbuild despite the docs advertising it.

Changes

  • esbuild.config.js — Each package now emits three JS bundles instead of one: ESM (*.mjs), CommonJS (*.cjs), and the existing browser-global IIFE (*.min.js). A single canonical *.min.css is retained (all three JS bundles import identical styles; extracted extractCss/removeExtraCss helpers handle this).
  • packages/*/package.json (all 4) — Added main (CJS), module (ESM), unpkg/jsdelivr (global), and a conditional exports map with import/require/types/default plus CSS and ./build/* subpaths.
  • Extensions — Used .mjs/.cjs rather than .esm.js/.cjs.js: files under build/ inherit the root "type": "module", making bare .js ambiguous to Node. Explicit extensions guarantee correct format resolution in every consumer.
  • Docs / changeset — CommonJS example corrected to require('@matdata/yasgui').default (esbuild places the default under .default); minor changeset added for all four packages.
// ESM (bundlers / Node import)
import Yasgui from '@matdata/yasgui';
import '@matdata/yasgui/build/yasgui.min.css';

// CommonJS
const Yasgui = require('@matdata/yasgui').default;

Existing <script src=".../build/yasgui.min.js"> global usage is preserved via unpkg/jsdelivr.

Copilot AI changed the title [WIP] Fix npm build to support module exports for bundlers Ship ESM and CommonJS builds so packages are importable by bundlers Sep 23, 2026
Copilot AI requested a review from MathiasVDA September 23, 2026 18:08
@MathiasVDA
MathiasVDA requested a lite review from Copilot September 23, 2026 19:50
@MathiasVDA
MathiasVDA marked this pull request as ready for review September 23, 2026 19:50

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

The review identified packaging/build issues that can mislead consumers (Node engine constraints) and potentially break sourcemap correctness for renamed CSS outputs.

Get a fresh assessment by requesting another Copilot review.

Review effort: Lite
Findings: 1 Medium severity · 2 Low severity

Open (3)
What changed in this PR

This PR updates MatGUI’s build and package metadata so the published npm artifacts are importable by bundlers and Node consumers (ESM/CJS), while preserving the existing browser-global IIFE distribution for <script> usage—addressing issue #185.

Changes:

  • Extend the esbuild pipeline to emit ESM (.mjs), CJS (.cjs), and IIFE (.min.js) bundles per package while keeping a single canonical CSS output.
  • Update each package’s package.json to expose main/module/unpkg/jsdelivr and a conditional exports map for import/require/types/default (plus CSS/build subpaths where relevant).
  • Add a regression unit test that validates the packed tarball’s structure and verifies ESM/CJS resolution and a Vite build consumer.
File Description
esbuild.config.js Builds per-package ESM/CJS/IIFE outputs and manages a single canonical CSS artifact.
packages/​yasgui/​package.json Adds main/module/exports/CDN fields so bundlers and Node can resolve the package correctly.
packages/​yasqe/​package.json Adds main/module/exports/CDN fields so bundlers and Node can resolve the package correctly.
packages/​yasr/​package.json Adds main/module/exports/CDN fields so bundlers and Node can resolve the package correctly.
packages/​utils/​package.json Adds main/module/exports/CDN fields so bundlers and Node can resolve the package correctly.
test/​unit/​package-consumption-test.ts Regression test that exercises npm pack output and validates ESM/CJS resolution and Vite bundling.
docs/​developer-guide.md Updates CommonJS usage to access the default export via .default.
.changeset/​esm-module-exports.md Records a minor release note describing the new ESM/CJS publishing behavior.

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

Comment thread esbuild.config.js
Comment thread packages/yasqe/package.json
Comment thread packages/yasr/package.json
Refactor CSS extraction logic to handle renaming and sourcemaps more robustly.

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: MathiasVDA <15101339+MathiasVDA@users.noreply.github.com>
@MathiasVDA
MathiasVDA merged commit 36015f9 into main Sep 23, 2026
@MathiasVDA
MathiasVDA deleted the copilot/fix-npm-build-module-exports branch September 23, 2026 20:25
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.

npm build has no module exports, so it can't be imported with a bundler

3 participants