Coder Social home page Coder Social logo

Comments (12)

LGmatrix13 avatar LGmatrix13 commented on May 27, 2024 1

Not sure how much of a drop-in replacement this would be, but bun has become a popular bundler lately since it can export a package to all the different js extensions, including ESM.

https://bun.sh/docs/bundler#content-types

from node-rate-limiter-flexible.

mroderick avatar mroderick commented on May 27, 2024 1

This issue is really a shortcoming of the bundler/treeshaker used by OP and not of rate-limiter-simple.

Since OP is using ESM, they can deep import files from the package, circumventing to the root file and helping their treeshaker out.

import RateLimiterMemory from "rate-limiter-flexible/lib/RateLimiterMemory.js";

console.log(typeof RateLimiterMemory);
// => function

That means that we don't need to convert this codebase to ESM just yet or in a hurry, but can take our time and do it without introducing new problems for users.

from node-rate-limiter-flexible.

djMax avatar djMax commented on May 27, 2024 1

No, because it is just not that simple. Node is one environment, but webpack, jest, vitest, typescript - they are all different and all have varying levels of support. And ESM is like a poison that once it puts a drop in your bucket, no other package can do anything but ESM. Whereas CJS works just fine in either ESM or CJS. So dual bundles are require to get flexibility on the backend and tree shaking on the front end.

from node-rate-limiter-flexible.

animir avatar animir commented on May 27, 2024

@achingbrain The question about ESM is interesting.
I don't have much experience with tree-shaking optimizations. Different projects use different approaches. Can we keep both CJS and ESM without breaking dependent projects?
Do you have any suggestions on changing it with or without breaking changes?

from node-rate-limiter-flexible.

animir avatar animir commented on May 27, 2024

@LGmatrix13 Thanks, it looks promising.
I still get my head around the idea of having ESM module exports.
Not sure, what's the best practice here.
Should it be provided with both options CJS and ESM? Or just ESM and deprecate using CJS? If both how it is published to npm? Maybe I asked you too many questions already :-) Sorry, just not sure how to proceed.

from node-rate-limiter-flexible.

achingbrain avatar achingbrain commented on May 27, 2024

Can we keep both CJS and ESM without breaking dependent projects?

There are a few caveats around shipping both - the same authored class imported by a CJS file and a ESM file is treated as a separate class by the js runtime (since the classes were loaded from different locations) so were you to pass the instance between them instanceof won't work as expected, and like this you can end up including both versions in your bundle by accident.

Tools like rollup will create CJS/ESM versions and you can let the runtime select them with an "exports" entry in the project's package.json:

{
  "exports": {
    ".": {
      "types": "./path/to/index.d.ts",
      "require": "./path/to/index.cjs.js",
      "import": "./path/to/index.esm.js"
    }
  }
}

From my experience the short term pain of going ESM-only is preferable to the long-term pain of supporting both CJS and ESM exports.

from node-rate-limiter-flexible.

animir avatar animir commented on May 27, 2024

@achingbrain Thank you for your valuable input.

I think, it is better to go clean ESM since Node.js natively supports it.
I've prepared a PR with refactoring. Could you double check this index.js https://github.com/animir/node-rate-limiter-flexible/pull/250/files#diff-e727e4bdf3657fd1d798edcd6b099d6e092f8573cba266154583a746bba0f346 is what allows to make optimal tree-shaking?

from node-rate-limiter-flexible.

djMax avatar djMax commented on May 27, 2024

the words "clean" and "ESM" are antonyms. Please don't convert this repo.

from node-rate-limiter-flexible.

animir avatar animir commented on May 27, 2024

@djMax Could you tell more about your concern on ESM?

from node-rate-limiter-flexible.

Systerr avatar Systerr commented on May 27, 2024

Converting to ESM is a really good idea. Node.js has natively supported it for a long time, and browsers also support it. For CJS (CommonJS) node executions, developers can use "async import()" (dynamic import)

from node-rate-limiter-flexible.

djMax avatar djMax commented on May 27, 2024

I think the only sane solution here is dual output packages - CJS and ESM. ESM causes no end of pain with complex environments - jest, webpack, nextjs, they all have their own solutions to this pain and they often fight each other. As a library developer, the nice thing to do is output both and let the particular use case choose based on the environment.

https://www.sensedeep.com/blog/posts/2021/how-to-create-single-source-npm-module.html

from node-rate-limiter-flexible.

animir avatar animir commented on May 27, 2024

@djMax Hi, thank you for sharing the link. Since ESM is now natively supported by Node.js should we all use ESM and abandon CJS?

from node-rate-limiter-flexible.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.