Comments (12)
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.
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.
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.
@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.
@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.
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.
@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.
the words "clean" and "ESM" are antonyms. Please don't convert this repo.
from node-rate-limiter-flexible.
@djMax Could you tell more about your concern on ESM?
from node-rate-limiter-flexible.
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.
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.
@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)
- Redis configuration
- Run test using github actions HOT 1
- Lua script takes non integer argument as input which causes random error on user supplied input HOT 6
- Lua Script uses only Write commands and Cannot Scale in a Redis Cluster HOT 9
- Rate limit redis - Express middleware HOT 7
- Unable to use table with schema name HOT 7
- Redis limiter on version 3.0.2 TypeError: Invalid argument type TypeError: result is not iterable HOT 2
- RateLimiterMemory blockDuration doesnt work with 1 billion seconds HOT 4
- rateLimiter.get always returns `null` HOT 4
- Usage with Prisma ? HOT 2
- RateLimiterMemory msBeforeNext is negative in Vercel edge function HOT 8
- PM2 cluster error "timeout: no answer from master in time" if the master is not already available when the client is created HOT 2
- consume() vs penalty() HOT 1
- RateLimiterMongo TypeError: Cannot read properties of null (reading 'value') HOT 1
- [Feature Request]: Don't Throw Errors When `limiter.consume()` Rejects a Request from a User HOT 4
- How to prevent negative values HOT 2
- Dynamo this.client.updateItem is not a function HOT 1
- Revise Documentation HOT 2
- Consider applying inMemoryBlockOnConsumed to RateLimiter.get(key) as well as consume(). HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from node-rate-limiter-flexible.