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 pendingRequests #4294

Closed
KaelWD opened this issue Dec 8, 2021 · 5 comments · Fixed by #4296
Closed

Expose pendingRequests #4294

KaelWD opened this issue Dec 8, 2021 · 5 comments · Fixed by #4296

Comments

@KaelWD
Copy link

KaelWD commented Dec 8, 2021

Feature Use Case

I'm writing a plugin that needs to make modifications to a file based on other files in the project. In vite I'm using the transform hook with server._pendingRequests to check if I'm the only module left, but there's nothing like this exposed in rollup.

Feature Proposal

Something similar to vite: https://github.com/vitejs/vite/blob/b625a2cf3730fda94d934b23bcec0c859bfa0b24/packages/vite/src/node/server/transformRequest.ts#L49-L52

ModuleLoader:

+    public pendingRequests = new Map()
     async fetchModule({ id, meta, moduleSideEffects, syntheticNamedExports }, importer, isEntry) {
...
         this.graph.watchFiles[id] = true;
-        await this.addModuleSource(id, importer, module);
+        const promise = this.addModuleSource(id, importer, module)
+        this.pendingRequests.set(id, promise.then(() => module))
+        await promise;
+        this.pendingRequests.delete(id)
         const resolveStaticDependencyPromises = this.getResolveStaticDependencyPromises(module)

PluginContext:

    const context = {
+       pendingRequests: graph.moduleLoader.pendingRequests,
        addWatchFile(id) {
@lukastaegert
Copy link
Member

While you do not have this exact API, you can already do something similar with a combination of this.load and this.getModuleIds.

The id of a module is added to this.getModuleIds() before its load hook is even run, so all "pending" modules can be found here. When you have the id, you can await this.load(id) to wait until it is loaded and get its module info.

@KaelWD
Copy link
Author

KaelWD commented Dec 10, 2021

That seems to work, thanks! I did still need a timeout too though because this.load(id) sometimes resolves instantly but module.code is still null, so I'd end up in an infinite loop.

@lukastaegert
Copy link
Member

sometimes resolves instantly but module.code is still null

That should not happen, would be interested if you could reproduce it.

@KaelWD
Copy link
Author

KaelWD commented Dec 10, 2021

@lukastaegert
Copy link
Member

lukastaegert commented Dec 10, 2021

Thanks so much, there was actually a bug in the logic. Fix at #4296. Now it should be reliable.

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.

2 participants