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

fix(commonjs): keep this context for callbacks #1603

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

patricklx
Copy link

@patricklx patricklx commented Oct 5, 2023

Rollup Plugin Name: commonjs

This PR contains:

  • bugfix
  • feature
  • refactor
  • documentation
  • other

Are tests included?

  • yes (bugfixes and features will not be merged without tests)
  • no

Breaking Changes?

  • yes (breaking changes will not be merged unless absolutely necessary)
  • no

If yes, then include "BREAKING CHANGES:" in the first commit message body, followed by a description of what is breaking.

List any relevant issue numbers:

Description

it's not possible to use the commonjs plugin in vitejs, as some callback functions are not called with the original context.
edit: As I understand now, rollup already binds the context. but vitejs doesn't.

@patricklx patricklx changed the title keep this context for callbacks [commonjs] keep this context for callbacks Oct 5, 2023
@tada5hi tada5hi changed the title [commonjs] keep this context for callbacks fix(commonjs): keep this context for callbacks Oct 5, 2023
@@ -108,7 +108,7 @@ export function getRequireResolver(extensions, detectCyclesAndConditional, curre
setInitialParentType(parentId, parentMeta.initialCommonJSType);
await Promise.all(
parentRequires.map(({ resolved, isConditional }) =>
analyzeRequiredModule(parentId, resolved, isConditional, this.load)
analyzeRequiredModule(parentId, resolved, isConditional, this.load.bind(this))
Copy link
Member

Choose a reason for hiding this comment

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

Fair enough, but Rollup already this-binds all context functions. This is by design, but admittedly, this is not well-communicated.

Copy link
Author

@patricklx patricklx Oct 5, 2023

Choose a reason for hiding this comment

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

ah, okay. this issue appeared while using commonjs plugin with vitejs.

Copy link
Member

Choose a reason for hiding this comment

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

Interesting. So I assume this happened in dev mode? Then this would mean that Vite does not this.bind. Maybe an issue to raise with Vite. But this would also make it harder to write a test as you would need to test with a fake plugin context.

Copy link
Member

Choose a reason for hiding this comment

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

Also, Andrew will not like that you skipped the PR template where you should have added all this information.

Copy link
Member

Choose a reason for hiding this comment

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

I think so too ^^ 😅

Copy link
Author

Choose a reason for hiding this comment

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

I added it now

@shellscape
Copy link
Collaborator

Looks like you need an upstream update:

git remote add upstream git@github.com:rollup/plugins.git
git fetch upstream
git merge upstream/master
git push

That should get you set so that actions run without errors.

We'll also need a new test that tests this explicit change before we can merge. It's the only way we'll be able to protect against a regression.

@lukastaegert what do you think about this change on the whole, given the info in the other comment thread?

@lukastaegert
Copy link
Member

@shellscape I think it is ok.
It would be nice to have some kind of test, but this is really difficult as you would basically need to write a mock Rollup context, which in itself would be hard to maintain and likely not worth the effort, so we can go through with this once CI passes.

@patricklx
Copy link
Author

I think now this should be resolved in vitejs to also bind the functions just like rollup.

@patricklx
Copy link
Author

patricklx commented Oct 11, 2023

I also opened a PR on vitejs.
But according to this comment vitejs/vite#14569 (review)
the functions are not bound in rollup.
I also did a code search and could not find any .bind for load function.
@lukastaegert

@patricklx
Copy link
Author

@lukastaegert
Copy link
Member

Yes, that was indeed overlooked

@shellscape
Copy link
Collaborator

@patricklx what's the state of this PR at the moment?

@patricklx
Copy link
Author

it should be ready. I also making a PR to vite as well

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

Successfully merging this pull request may close these issues.

None yet

4 participants