GithubHelp home page GithubHelp logo

Comments (7)

guybedford avatar guybedford commented on May 12, 2024

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.

MylesBorins avatar MylesBorins commented on May 12, 2024

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.

GeoffreyBooth avatar GeoffreyBooth commented on May 12, 2024

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.

evanw avatar evanw commented on May 12, 2024

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.

guybedford avatar guybedford commented on May 12, 2024

@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.

guybedford avatar guybedford commented on May 12, 2024

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.

guybedford avatar guybedford commented on May 12, 2024

It seems like #46 or similar will be the approach for now.

from cjs-module-lexer.

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.