Skip to content

Instantly share code, notes, and snippets.

@ceejbot
Last active April 3, 2018 04:21
Show Gist options
  • Save ceejbot/d8703c71dd5cfba4ba93e755371e96e7 to your computer and use it in GitHub Desktop.
Save ceejbot/d8703c71dd5cfba4ba93e755371e96e7 to your computer and use it in GitHub Desktop.
ESModule use cases, a list in progress

ES module use cases

  • Alice is writing a new library, and she is excited to use the new ES6 syntax. However, she would like to use an older but still good package she found on npm, that exports its interface using CommonJS. She does so easily after reading the NodeJS documentation on how to do this.

  • Bob is updating a module for his work, and he needs to support existing CommonJS-using codebases as well as a new project that prefers to stick with ES6 for static analysis reasons. He publishes a package that exports both kinds of interfaces.

  • Carol is updating her popular code coverage reporting tool for the new world. She uses the ESM loader hooks to instrument test code as it is imported so she can get code coverage reporting on par with what she has for CommonJS.

  • David is writing a transpiler. He writes a custom hook that transpiles source as it's loaded from his language to JavaScript.

  • Eugenia wants to abandon the CommonJS API for her existing npm package and move to a new ESM-only API.

  • Hermione wants to load modules from a database and not from the filesystem. She writes a custom loader hook to do so.

  • Ian wants to replace the dependencies of a module with mocks while testing. He writes a custom hook to do so.

@GeoffreyBooth
Copy link

  • Geoffrey wants to rewrite CoffeeScript’s modules tests from being string comparisons to instead actually execute import and export statements. The test runner is written in CommonJS, and needs to test both transpiled CommonJS strings and transpiled ES module strings output from the new modules tests.

Okay, maybe this one is overly specific, but a real-world use case can’t hurt 😄 The generalized case of test runners that need to test both types of modules is a common use case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment