Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide information regarding multiple re-exports #99

Open
dario-piotrowicz opened this issue Aug 8, 2024 · 1 comment
Open

Provide information regarding multiple re-exports #99

dario-piotrowicz opened this issue Aug 8, 2024 · 1 comment

Comments

@dario-piotrowicz
Copy link

The problem I'm facing is that parse only reports the last re-export of cjs modules completely loosing the information about pre-existing ones.

For example by parsing the following code:

if (true) {
  module.exports = require("./a.cjs");
} else {
  module.exports = require("./b.cjs");
}

only the last re-export assignment is recognized and reported to the caller (reexports = ['./b.cjs' ]).

It would be very useful if all the re-exports in the code could be recognized and reported to the caller

For example as: reexports = ['./a.cjs', './b.cjs']

The above anyways could be considered ambiguous since the same result could be obtained by parsing

module.exports = {...require('./a.cjs'), ...require('./b,cjs')};

so maybe reexports could/should actually be an array or array of strings?
Meaning that parsing

if (true) {
  module.exports = {...require('./a.cjs'), ...require('./b,cjs')};
} else {
  module.exports = {...require('./c.cjs'), ...require('./d,cjs')};
}

could yield reexports = [['./a.cjs', './b.cjs' ], ['./c.cjs', './d.cjs' ]]

Minimal Reproduction

For convenience I've created a reproduction here: https://github.com/dario-piotrowicz/cjs-module-lexer-reexports-repro

Additional information

Bug?

As I mentioned, I am not sure if this could be considered a bug since the reexport resetting seems to be intentional.

Solving this might generate different problems

If this issue were to be addressed, I would imagine that the following code:

module.exports = require('./a.cjs');
module.exports = require('./b.cjs');

would also produce two re-exports... although only one is actually valid... so this is definitely something to keep in mind 😕

The lexer is content-unaware so I don't think much could be done to avoid the above arguably incorrect result, even if nesting was kept into account (which might be a stretch in the lexer) that would still not always generate perfect results, for example when parsing code like this:

if(true){
   module.exports = require('./a.cjs');
}

module.exports = require('./b.cjs');

(maybe instead of expanding reexports there could be a new field in the parse result? as extra-reexports or something like that that could host all the reexports excluding the last one? so keeping the same exact functionality the lexer has now whilst also providing callers with the extra bit of information?)

More context

To give a bit more context, I am simply trying to parse cjs code and gather informations about all the exports and re-exports that the code contains, unfortunately as mentioned in the issue the lexer currently doesn't return all possible re-exports but only the last one encountered, this is problematic in my use case as I would want to know all possible re-exports as well (and that information gets, as far as I can tell, completely lost).

@guybedford
Copy link
Collaborator

This could make sense.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants