Comments (7)
We did discuss syntax approaches like this, but it seemed like in most cases the advisable approach was to use ESM wrappers or ESM modules directly for these executions since if Node.js supports this feature it would support ESM anyway.
We can make changes to this processing, but backporting is not necessarily a given at this point so changing things here would result in spotty support for 12 LTS, whereas right now there is comprehensive backwards compat on all Node.js modules semantics paths so it can be reliable.
//cc @GeoffreyBooth @MylesBorins @bmeck @jkrems
from cjs-module-lexer.
Yeah, unfortunately Node.js v12 is now in maintenance so the bar to make changes is going to be pretty hard... especially ones that are not backwards compatible. Not impossible, but very very hard to do.
from cjs-module-lexer.
Would you consider adding some way to annotate the exports that are available in a given file without having to rewrite the JavaScript in the file? A comment with something like
//# cjsExports="someName","someOtherName",...
would work
If esbuild could output such a comment, wouldn’t it also therefore be able to output a CommonJS module that cjs-module-lexer could successfully statically analyze? That seems like the most straightforward solution, as then nothing would need to change on Node’s side and it would work back to Node 12.
from cjs-module-lexer.
If esbuild could output such a comment, wouldn’t it also therefore be able to output a CommonJS module that cjs-module-lexer could successfully statically analyze?
Yes it could. I addressed this in my original post:
This is undesirable both for minification reasons and because it would require me to support another code path, which is more overhead for me in terms of code complexity and would lead to a bigger test matrix.
It's fine if your answer is "no". The decision is up to you since it's your project. Just thought I'd ask first to see if there was a way to avoid the complexity of having to generate this magic syntax.
from cjs-module-lexer.
@evanw it might be possible to write out an abbreviated form that basically fools the lexer, to avoid those calls being the full exact defineProperty statements. In a way this might effectively be like what you were asking about.
The lexer doesn't have any concept of execution flow at all, so a non-executed function that looks like it defines exports would be supported.
Here's one way of doing that:
// fake exports to inform lexer
(() => exports.a = exports.b = exports.c = void);
// actual exports definition function
defineExports(exports);
That is then something that is quite close to the syntax you are after that will work all the way back to Node.js 12 as well.
I'd still recommend simply outputting an analyzable form of exports definitions if you can and that would still be preferable. But if you can't then the above can be an option.
from cjs-module-lexer.
I guess the remaining risk is minifiers removing these unused functions... perhaps there's a way to get around that as well?
That said, the same likely applies to any "syntax approach" even the one asked.
from cjs-module-lexer.
It seems like #46 or similar will be the approach for now.
from cjs-module-lexer.
Related Issues (20)
- Babel interopRequireWildcard reexports
- CommonJS import from a Module package too inflexible HOT 5
- Extending exports object detections HOT 20
- add tag for 1.2.2 HOT 4
- node ES6 import throws this error. HOT 18
- Error selecting connection connect ECONNREFUSED 127.0.0.1:5432
- Function call changes in TypeScript 4.4 seem to be breaking detection of named exports HOT 7
- Can't export when enumerable is "!0"
- When trying to build with snowpack this error is thrown HOT 4
- Not work well with TypeScript export default as output HOT 2
- exports can be detected in hashbang
- Feature Request: Also detect esbuild Re-exports HOT 1
- Bug: cannot detect exported numbers HOT 5
- Incompatibility with TypeScript (edge case) HOT 3
- Issue with parsing 'react-transition-group' HOT 2
- Rollup ^3.0.0 cjs re-exports edge case with externalLiveBindings: false HOT 8
- Out of bounds memory access HOT 1
- Real world edge case: prettier HOT 1
- Add RISC-V Support HOT 2
- Unable to detect exports from bundled webpack output HOT 6
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 cjs-module-lexer.