Skip to content

Conversation

@rubicon2
Copy link

Solves issue #27

As per the typescript docs:

Users will detect a misconfigured module smell if they have to access the .default property of a default import, and if they’re trying to write code that will run both in Node.js and a bundler, they might be stuck. Some third-party TypeScript transpilers expose options that change the way default exports are emitted to mitigate this difference, but they don’t produce their own declaration (.d.ts) files, so that creates a mismatch between the runtime behavior and the type checking, further confusing and frustrating users. Instead of using default exports, libraries that need to ship as CommonJS should use export = for modules that have a single main export, or named exports for modules that have multiple exports...

I have made this change, so now esm imports (import/export) and commonjs imports (require) will both work.

However, I had to change the import in the test file from require('../dist').default to require('../dist'), so I guess that would break the import for existing users that are using .default.

As per typescript docs: "...libraries that need to
ship as CommonJS should use 'export ='  for modules
that have a single main export..."

This fixes esm imports not working, but commonjs
imports will have to drop '.default' that was
previously necessary, e.g. in the test file.
So a breaking change.
@rubicon2
Copy link
Author

I have been trying this out with bundlers to see if it all works, and it turns out this also fixes a broken import when importing with require() and bundling with webpack. Importing with esm syntax and bundling actually worked fine already.

I don't understand why the esm syntax worked when bundling, but not with an express app I was working on. I guess webpack transpiled the code into something that works??

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.

1 participant