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

feat: support rollup plugin this.load in plugin container context #11469

Merged
merged 2 commits into from
Feb 23, 2023

Conversation

luxaritas
Copy link
Contributor

@luxaritas luxaritas commented Dec 22, 2022

Description

This adds support for calling this.load (added in Rollup 2.60) in a rollup plugin when using a plugin context via Vite's plugin container (namely, when using the dev server).

Resolves #6810

Additional context

This fixes the issue I have in my specific use case (rollup-plugin-typescript2 needing to call this.load, though not using any of its output - I tested this PR locally with that test case), and is strictly more functional than the previous lack of support. However, there are some limitations which I don't know if it's appropriate to address or otherwise present issues:

  • The return of this.load from Vite will be a ModuleInfoProxy, which doesn't include most of the properties from ModuleInfo. I imagine some plugins are likely to be calling this.load expecting some of these parameters to work. That said, this is already an existing limitation of getModuleInfo, so I assume that is out of scope.
  • Further, this.load is supposed to trigger transform and moduleParsed - this does not (in accordance with vite's on-demand nature)
  • The resolveDependencies parameter is left unused, as since the modules are not actually transformed/parsed, from my understanding dependency information is unavailable/irrelevant
  • ModuleOptions properties other than meta are unused (though as far as I know, that's the responsibility of updateModuleInfo, and so out of scope, and presumably unnecessary in the on-demand situation)

I don't have much experience working at this level, so feedback is definitely appreciated, and I'm happy to make any adjustments


What is the purpose of this pull request?

  • Bug fix
  • New Feature
  • Documentation update
  • Other

Before submitting the PR, please make sure you do the following

  • Read the Contributing Guidelines.
  • Read the Pull Request Guidelines and follow the PR Title Convention.
  • Check that there isn't already a PR that solves the problem the same way to avoid creating a duplicate.
  • Provide a description in this PR that addresses what the PR is solving, or reference the issue that it solves (e.g. fixes #123).
  • Ideally, include relevant tests that fail without this PR but pass with it.

@luxaritas luxaritas changed the title feat: Support rollup plugin this.load in plugin container context feat: support rollup plugin this.load in plugin container context Dec 22, 2022
@bluwy bluwy added rollup plugin compat p2-nice-to-have Not breaking anything but nice to have (priority) labels Dec 28, 2022
Copy link
Member

@sapphi-red sapphi-red left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the PR!

Comment on lines +224 to +227
// Don't throw an error when returning from an async function
if (key === 'then') {
return undefined
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL about this 😮

const p = new Proxy({}, { get() { throw new Error('foo') } })

async function foo() { return p }

;(async () => {
  await foo() // error
})()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that threw me for a loop initially - I believe this is due to async/await being syntactic sugar for a promise, and promises being able to return promises, so the runtime needs to "probe" for a .then() on the return to see whether it needs to chain the promise.

packages/vite/src/node/server/pluginContainer.ts Outdated Show resolved Hide resolved
Co-authored-by: 翠 / green <green@sapphi.red>
@bluwy
Copy link
Member

bluwy commented Feb 23, 2023

Sorry for attending this 2 months late 😬 Let's get this in 4.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p2-nice-to-have Not breaking anything but nice to have (priority) rollup plugin compat
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support Rollup's this.load() method in the plugin context
3 participants