4 ms·
Static analysis and tree shaking have been done with CJS for about a decade. https://gist.github.com/joepie91/bca2fda868c1e8b2c2caf76af7dfcad3 https://gist.git
by jampekka 2y ago
Static analysis and tree shaking have been done with CJS for about a decade.
https://gist.github.com/joepie91/bca2fda868c1e8b2c2caf76af7dfcad3 https://gist.github.com/joepie91/bca2fda868c1e8b2c2caf76af7d...
- wffurr 2y agoIn a limited way due to side effects from the require function. Reading that rant, the author did not seem to take much time to understand the rationale behind ES modules: “ yet for some completely unclear reason, ESM proponents decided to remove that property” is just pure ignorance.
- jampekka 2y ago> In a limited way due to side effects from the require function. In a limited way that covers something like 99.9% of CJS usage. Bundling is based on static analysis. If by side effects you mean running code, not just declaring exports, ES6 import does these side effects too. Thinking that CJS can't be used for static analysis or tree-shaking is the widest spread pure ignorance.
- 9dev 2y agoThe main argument in that post doesn't even hold; using ESM, you can always using dynamic imports: await import( someExpression ) Besides, static analysis and tree shaking completely break down if modules impose side effects from being required, which is one of the main gripes of Python as well. ESM completely alleviates that.
- jampekka 2y agoThat forces dynamic imports (and is horrible ergonomics as discussed in the post). CJS requires can be statically analyzed to figure out whether a require can be static, as is done e.g. in all bundlers. ESM import runs side effects as well.
- 9dev 2y agoWe have had top-level await for a while now. I don’t know what’s so horrible about this: const app = express() app.use("/users", await import("./routers/users")) …if you’re so inclined to do things exactly as you did in the past. I’m pretty sure bundlers will even transform that into an ordinary module import. The cases where you actually need dynamic imports are few and far between. Are we actually talking about engine limitations here, or is it just a few snowflakes that insist on creating loggers like this? require("debug")("acme") What is so particularly pretty about that is beyond me. > ESM import runs side effects as well. At runtime, yes. Not during static analysis.