Skip to content

Add ESM support - #16

Closed
lfbittencourt wants to merge 2 commits into
mainfrom
issue-12-add-esm-support
Closed

lfbittencourt wants to merge 2 commits into
mainfrom
issue-12-add-esm-support

Conversation

@lfbittencourt

Copy link
Copy Markdown
Owner

Summary

  • Converts export = to export default in src/index.ts and src/pattern-to-tree.ts — export = is CJS-only and errors when targeting ES modules
  • Adds tsconfig.cjs.json compiling to dist/cjs/ with declaration files
  • Adds tsconfig.esm.json compiling to dist/esm/ with module: ESNext
  • Updates package.json with an exports map, main pointing to dist/cjs/index.js, and a types field
  • Removes the CJS-only note from README and adds both require and import examples

Closes #12

Test plan

  • yarn build compiles both CJS (dist/cjs/) and ESM (dist/esm/) without errors
  • yarn test — all 34 tests pass
  • Type declarations (*.d.ts) are generated in dist/cjs/

🤖 Generated with Claude Code

lfbittencourt and others added 2 commits February 18, 2026 17:45
export = is CJS-only syntax and errors when targeting ESM modules.
export default works for both CJS (with esModuleInterop) and ESM,
and is already what the test imports expect.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
- Add tsconfig.cjs.json: compiles to dist/cjs/ with declaration files
- Add tsconfig.esm.json: compiles to dist/esm/ with ESNext modules
- Update package.json: exports map, main → dist/cjs/index.js, types field, updated build script
- Update README: remove CJS-only note, show both require and import examples

Closes #12

Co-Authored-By: Claude Sonnet 4.6 <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.

Pull request overview

This PR introduces dual-package (CommonJS + ES module) support for string-expansion, updating the TypeScript build outputs and package entrypoints so consumers can use either require or import.

Changes:

  • Added separate TypeScript build configs for CJS (dist/cjs/) and ESM (dist/esm/).
  • Switched entrypoint exports from export = to export default.
  • Updated package.json (main, types, and exports) and refreshed README usage examples.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
tsconfig.esm.json Adds an ESM build output configuration.
tsconfig.cjs.json Adds a CJS build output configuration with declarations.
src/pattern-to-tree.ts Converts export = to export default.
src/index.ts Converts export = to export default for the package entrypoint.
package.json Adds conditional exports + updates build script and entry fields.
README.md Updates documentation to show both CJS and ESM usage.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread package.json
"types": "dist/cjs/index.d.ts",
"exports": {
".": {
"import": "./dist/esm/index.js",

Copilot AI Feb 18, 2026

Copy link

Choose a reason for hiding this comment

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

The ESM export points to ./dist/esm/index.js, but this package has no "type": "module" (so .js files are treated as CommonJS by Node). That means import 'string-expansion' will likely try to parse the ESM build as CJS and throw a syntax error. Consider either emitting ESM as .mjs, or adding dist/esm/package.json with { "type": "module" } and keeping the .js extension, or switching the package to type: module and emitting the CJS build with a .cjs extension.

Suggested change
"import": "./dist/esm/index.js",

Copilot uses AI. Check for mistakes.
Comment thread tsconfig.esm.json
{
"extends": "./tsconfig.json",
"compilerOptions": {
"module": "ESNext",

Copilot AI Feb 18, 2026

Copy link

Choose a reason for hiding this comment

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

Compiling to ESM with module: ESNext while keeping the base moduleResolution (currently node10) will emit extensionless relative imports (e.g. import './expand-tree'), which are not resolvable by Node's ESM loader without flags. If the goal is Node ESM support, consider using module/moduleResolution: NodeNext (or a bundler) and updating source imports to include .js extensions so the emitted ESM can be executed directly by Node.

Suggested change
"module": "ESNext",
"module": "ESNext",
"moduleResolution": "NodeNext",

Copilot uses AI. Check for mistakes.
Comment thread src/index.ts
import patternToTree from './pattern-to-tree';

export = (pattern: string) => expandTree(patternToTree(pattern));
export default (pattern: string) => expandTree(patternToTree(pattern));

Copilot AI Feb 18, 2026

Copy link

Choose a reason for hiding this comment

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

Switching the entrypoint to export default changes the CommonJS shape when compiled with module: CommonJS: require('string-expansion') will return an object with a .default property rather than the function itself. If you want to preserve the existing CJS API, consider adding a small CJS wrapper entry that assigns module.exports = <default export>, and point the exports.require/main fields at that wrapper.

Suggested change
export default (pattern: string) => expandTree(patternToTree(pattern));
declare const module: any;
const expand = (pattern: string) => expandTree(patternToTree(pattern));
export default expand;
(module as any).exports = expand;

Copilot uses AI. Check for mistakes.
Comment thread README.md

```js
// CommonJS
const expand = require('string-expansion');

Copilot AI Feb 18, 2026

Copy link

Choose a reason for hiding this comment

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

The CommonJS example implies require('string-expansion') returns a callable function. With the current TypeScript change to export default, the compiled CJS build will typically require consumers to access .default (or destructure it). Either adjust the CJS build to keep the old require shape, or update this example to match the published API.

Suggested change
const expand = require('string-expansion');
const expand = require('string-expansion').default;

Copilot uses AI. Check for mistakes.
@lfbittencourt lfbittencourt mentioned this pull request Mar 27, 2026
@lfbittencourt
lfbittencourt deleted the issue-12-add-esm-support branch March 27, 2026 23:09
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.

Add ESM support

2 participants