Comments (4)
The __export(is_equal_exports, {
and __esm()
helpers are generated for any CommonJS-style imports
. For example import('./is-equal.mjs')
require('./is-equal')
. This is because they captures the namespace object and esbuild doesn't know if outside code used or not used the object.
Example of namespace capturing in ESM context. // However, esbuild does know the namespace object is PURE, so if you never used the object it will be tree-shaked.
Example of using import()
. // Not only it generates a namespace object, but also it has to generate a promise to preserve the semantic of import()
. The promise obviously is a side-effect, which cannot be tree-shaked.
from esbuild.
I see, so it depends on the style of import where the module is imported, not so much the module itself.
Here is a full representation of my scenario, btw.
When you say "namespace object", I think you are referring to the "module object" in the case of ESM and the value of module.exports
in the case of CJS, right?
I still don't quite understand, though, because init_is_equal
is not the module/namespace object, it is a function generated by ESBuild to initialise the module, and esbuild inserts it where the module is require()
'd and only every calls the function. init_is_equal
is not passed around to application code as a reference to the module.
Furthermore, it is a complete no-op. It was generated by esbuild as an empty function so surely esbuild can know it is a no-op and omit it?
from esbuild.
When you say "namespace object", I think you are referring to the "module object" in the case of ESM and the value of module.exports in the case of CJS, right?
Yes, my wording might not be very correct.
esbuild bundle works by scanning the code in 2 phases. The first phase is gathering and transforming input files in parallel (that's why it is fast). The second phase is linking these individual modules, performing scope hoisting and generating esm-cjs interop helpers.
I guess when esbuild see your wrapper.cts, it knows:
- This is a commonjs module because it has
export = ...
. - This module imports something outside using es modules syntax
import { ...
.
Therefore, it can prepare such template for wrapper.cts:
// prepare a commonjs module object
var require_wrapper = __commonJS {
// evaluating dependencies to make sure
// any possible side effect evaluates before this module
init_is_equal();
... // rest of the module's content
};
During the second phase, the linker just has to make sure these variables are bound. There's no further side-effect tracking algorithm performed on them.
It might be diffcult to adding such optimization during the linking phase without hurting the speed.
from esbuild.
Ok while there is opportunity to optimise the output I understand your explanation, thanks!
from esbuild.
Related Issues (20)
- Keep number literals in their original form HOT 2
- Incorrect warning regarding "default" conditional export HOT 4
- Built-in dependencies working with --bundle --platform=node sometimes. Suggested regex to work HOT 1
- bundle with ts decorators works fine with v0.17.19, not later HOT 2
- Question: is there a way to drop specific exports? HOT 3
- How do i get the build result using js-api during watch? HOT 4
- Why is ESBuild not including React in the bundle? HOT 2
- @aws-sdk/lib-storage File Upload is not working at [email protected] HOT 1
- Outrageous peak memory usage with binary modules
- option: single line output (escape newline whitespace) HOT 2
- With `await esbuild.stop()`, deno test still detect resource leaks
- [WARNING] Base config file "./xxxx.json" forms cycle [tsconfig.json]
- Unused code is left when minifying
- No loader is configured for ".node" files HOT 1
- [question] Working directory CLI argument HOT 2
- i have a question , why is vite not used esbuild to bundle ,however select rollup to build HOT 2
- Unable to bundle WASM modules read by `node:fs` HOT 4
- I meet a error when using webpack bundle esbuild
- Variable assignment could not be repeated
- Plugin callback for unresolved imports HOT 10
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 esbuild.