Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: dual module export #607

Merged
merged 10 commits into from
Oct 18, 2023
Merged

Conversation

antstanley
Copy link
Contributor

This PR adds dual CommonJS and ES Module support to DynamoDB Toolbox.

Issue it resolves: #606

The changes include the following

  • Update package.json to include exports property to define location to be used for ES imports
"exports": {
    ".": {
      "import": {
        "default": "./dist/esm/index.js",
        "types": "./dist/esm/index.d.ts"
      },
      "require": {
        "default": "./dist/cjs/index.js",
        "types": "./dist/cjs/index.d.ts"
      }
    }
  },
  • Update package.json to set default module type to module
  "types": "module"
  • Create a tsconfig.esm.json file with ES module specify TypeScript settings

  • Rename tsconfig.build.json to tsconfig.cjs.json and add CommonJS setting for TypeScript

  • Update root tsconfig.json to use ES modules by default

  • Update all .ts files to use explicit .js imports in accordance with the ES Module spec and Node implementation. Reference https://www.totaltypescript.com/relative-import-paths-need-explicit-file-extensions-in-ecmascript-imports

  • Rename jest.config.js to jest.config.cjs to explicitly set it to a CommonJS module as the package default is now module

  • Update ts-jest config in jest.config.cjs to useESM: true and other ES module required config

  • Update all test files to use explicit .js imports as required by ES module spec and Node implementation

  • Add module declaration deep-copy.d.ts for the deep-copy module used by Entity, as it's existing declaration is not sufficient for ES modules

Apologies for the size of the PR. Unfortunately due to ES modules requiring explicit import of files by .js extensions I had to touch almost every file, but I only touched them gently.

The tests run and pass. I've also shipped a version of this to @antstanley/dynamodb-toolbox if you want to run it yourself and validate behaviour.

Bundling this version now enables me to use ES modules with DynamoDB Toolbox in my Lambda functions without bundling the AWS SDK v3.

One side effect I've noticed is the ES module version of DynamoDB Toolbox bundles more efficiently, dropping around 30Kb from my specific bundle.

@antstanley
Copy link
Contributor Author

Missed two file extenstions on the test suite used for type checking. Updated and committed b74fd58

Comment on lines +1 to +4
declare module 'deep-copy' {
declare function dcopy<T>(arg: T): T
export default dcopy
}
Copy link
Collaborator

@naorpeled naorpeled Oct 18, 2023

Choose a reason for hiding this comment

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

Just wondering,
why do we need this? deep-copy comes out of the box with type defs.
Not sure what I'm missing 🙏

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The type defs don't support ES modules. The export a function, not a module. ES modules require a module declaration for modules to be reference correctly.

The type def shipped looks like this

declare function dcopy<T>(arg: T): T

export default dcopy

with no module declaration

Copy link
Collaborator

Choose a reason for hiding this comment

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

Oh, gotcha!

@naorpeled
Copy link
Collaborator

Hey @antstanley,
thanks for this!!! much appreciated 😎

You're awesome.

LGTM!

@naorpeled naorpeled merged commit 67914eb into jeremydaly:main Oct 18, 2023
5 checks passed
@antstanley
Copy link
Contributor Author

Yay! I've recently gone on a mission to get all the packages I use to support dual modules so I can use ESM in Lambda.

Merging this saves me over a 1MB in my bundle size for one of my functions 1.2MB -> 200KB, mainly by enabling me to use ESM without bundling AWS SDK v3.

Thank you!

@naorpeled
Copy link
Collaborator

this is now available in v0.9.0 :)

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.

Dual module exports with ES Module support
2 participants