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

Expose module and bundle exports from hooks #4721

Closed
gerardmarquinarubio opened this issue Nov 21, 2022 · 5 comments · Fixed by #4731
Closed

Expose module and bundle exports from hooks #4721

gerardmarquinarubio opened this issue Nov 21, 2022 · 5 comments · Fixed by #4731

Comments

@gerardmarquinarubio
Copy link

Feature Use Case

Easier to write plugins that access a module's exports, for example:

// myTest.ts
function do() {
  ...
}
export debug = true;
export debugMessage = 'Hello!';
export default do;
// plugin
name: 'Debug plugin',
moduleParsed: async (info) => {
  if (info.exports.debug) {
    console.warn(info.exports.debugMessage);
  }
},

Of course this is just an easy example and not supposed to be something meaningful. I can imagine many use cases of plugins that one could develop accessing a module's exports.

Feature Proposal

Expose a module's and bundles exports in hooks as an object that you can access.

@lukastaegert
Copy link
Member

Basically like what we have on chunks, i.e. exports, imports and importedBindings? That would be doable with some caveats. The reason we do not have that yet is that it is sometimes hard to know the actual exports of a module before the build phase is done.

If the module does an export * from 'otherModule', then we likely do not know yet which named exports from the other module are injected in moduleParsed. Which is why we only have hasDefaultExport at this point because star reexports never add a default export, so this is reliable.

I would definitely be open to good proposals and PRs here. Maybe it could also make sense to differentiate between exports and re-exports (something like reexportedBindings?), which could have special syntax for namespace reexports.

@gerardmarquinarubio
Copy link
Author

The reason we do not have that yet is that it is sometimes hard to know the actual exports of a module before the build phase is done.

Then we should limit it to "inline" exports that have no dependencies. I don't know what the correct naming for those exports would be (local exports maybe).

If the module does an export * from 'otherModule', then we likely do not know yet which named exports from the other module are injected in moduleParsed. Which is why we only have hasDefaultExport at this point because star reexports never add a default export, so this is reliable.

Parsing of the AST should be done to analyse if the export declaration contains a value in the same file. Something similar to what Nextjs is doing here.

Thanks for your comment, hopefully more people become interested in this. I see so many benefits on adding this and it would add so much value for plugin writers for rollup.

@lukastaegert
Copy link
Member

lukastaegert commented Nov 28, 2022

Maybe something like this:

exports: ['foo', 'bar', '*'] // contains some unspecified namespace reexport,
exportedBindings: {
  '.': ['foo'] // or some other way to signify local exports,
  '/some/dependency': ['bar', '*'], // reexported stuff
}

@lemanschik

This comment was marked as off-topic.

@rollup-bot
Copy link
Collaborator

This issue has been resolved via #4731 as part of rollup@3.6.0. You can test it via npm install rollup.

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

Successfully merging a pull request may close this issue.

4 participants