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: always use the regular function to avoid losing module context #531

Merged
merged 7 commits into from
Nov 12, 2021
Merged

fix: always use the regular function to avoid losing module context #531

merged 7 commits into from
Nov 12, 2021

Conversation

latin-1
Copy link
Contributor

@latin-1 latin-1 commented Nov 8, 2021

Using an arrow function here would cause the loss of context (this) inside the module factory, which may break some modules that rely on it.

@pmmmwh
Copy link
Owner

pmmmwh commented Nov 8, 2021

Hey, thanks for the PR. I'll review this tomorrow and also investigate a bit into the issue.

Is there concrete example demonstrating how arrow functions would break?

@latin-1
Copy link
Contributor Author

latin-1 commented Nov 9, 2021

Assume we have a module, x.js:

this.foo = "bar";

And now, we are trying to require it from node:

$ echo 'this.foo = "bar";' > x.js
$ node -e 'require("./x").foo' -p
bar

Webpack has a similar approach to Node.js by calling the module factory with this = module.exports. However, the react-refresh-webpack-plugin's runtime code breaks this treatment.

Currently, we are running into this bug with @mediapipe/face_mesh, which uses the same way to export something in both browsers and webpack.

lib/utils/makeRefreshRuntimeModule.js Outdated Show resolved Hide resolved
lib/utils/makeRefreshRuntimeModule.js Outdated Show resolved Hide resolved
test/unit/makeRefreshRuntimeModule.test.js Outdated Show resolved Hide resolved
@pmmmwh pmmmwh self-requested a review November 12, 2021 23:25
@pmmmwh pmmmwh merged commit 9d9dd67 into pmmmwh:main Nov 12, 2021
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

2 participants