Skip to content

Conversation

@scottlovegrove
Copy link
Contributor

Summary

  • Add dual module support to enable compatibility with both CommonJS and ES Module projects
  • Implement TypeScript configurations for separate CommonJS and ESM builds
  • Create automated post-build script to fix ESM imports with required .js extensions
  • Update package.json with proper exports field and dual entry points
  • Modernize Jest configuration for ESM compatibility

Changes Made

New Files

  • tsconfig.cjs.json - CommonJS build configuration
  • tsconfig.esm.json - ESM build configuration
  • scripts/fix-esm-imports.cjs - Post-build script to add .js extensions for ESM compliance

Modified Files

  • package.json - Added dual module support with exports field and updated build scripts
  • jest.config.js - Converted to ESM format with modern ts-jest configuration

Build Output Structure

dist/
├── cjs/              # CommonJS output
│   ├── package.json  # Marks directory as CommonJS
│   └── ...
├── esm/              # ESM output (with .js extensions)
│   └── ...
└── types/            # TypeScript declarations
    └── ...

Benefits

  • Backward compatibility - Existing CommonJS projects continue working unchanged
  • ESM support - Modern projects can use native ES modules
  • NestJS compatibility - Resolves ES module resolution issues
  • Tree-shaking - ESM builds enable better bundling optimizations
  • Future-proof - Aligns with Node.js ecosystem direction

Test Plan

  • All existing tests pass (262 tests)
  • Build process generates correct dual output structure
  • CommonJS imports work correctly
  • ESM imports work correctly
  • Module instantiation works for both formats
  • ESM import fixing adds .js extensions properly

🤖 Generated with Claude Code

Add dual module support to enable compatibility with both CommonJS and ES Module projects.

- Add TypeScript configurations for both CommonJS and ESM builds
- Create post-build script to fix ESM imports with .js extensions
- Update package.json with proper exports field and dual entry points
- Modernize Jest configuration for ESM compatibility
- Generate separate dist/cjs, dist/esm, and dist/types directories
- Maintain backward compatibility with existing CommonJS projects
- Enable proper ES module resolution for modern projects and frameworks like NestJS

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
@scottlovegrove scottlovegrove merged commit 772dc66 into main Oct 31, 2025
1 check passed
@scottlovegrove scottlovegrove deleted the scottl/common-esm branch October 31, 2025 11:24
scottlovegrove added a commit that referenced this pull request Nov 24, 2025
…e-export issue

Fixes #407

When package.json has "type": "module", TypeScript treats .d.ts files as ESM modules,
which require explicit file extensions for relative imports. Without these extensions,
type re-exports from consuming packages fail with InternalResolutionError.

Changes:
- Add scripts/fix-dts-imports.cjs to add .js extensions to all relative imports in .d.ts files
- Update build process to run fix-dts step after generating types
- Add @arethetypeswrong/cli validation to integrity-checks to prevent regressions

This resolves the regression introduced in v6.0.0 (PR #379) where dual module support
broke type re-exports in consuming projects.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
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