Skip to content

fix(react-refresh): lazily access module.exports to prevent unexpected/potential TDZ error#2714

Merged
hyf0 merged 3 commits intomainfrom
hyf_fix_832084908203948
Apr 13, 2023
Merged

fix(react-refresh): lazily access module.exports to prevent unexpected/potential TDZ error#2714
hyf0 merged 3 commits intomainfrom
hyf_fix_832084908203948

Conversation

@hyf0
Copy link
Contributor

@hyf0 hyf0 commented Apr 11, 2023

Summary

The causes/details could be seen at pmmmwh/react-refresh-webpack-plugin#190.


The problem is caused by eagerly accessing some un-initialized variables declared by const. The accessing is done due to the react-refresh need to access each export of modules. By making accessing laziness, we solved the problem.

This PR solves the problem by lazily accessing module.exports.

This PR may have potential performance problems:

  • react-refresh would only start to work after the next tick of the event loop, aka all synchronous code is executed.

Credit to @underfin for pair programming with me.

Related issue (if exists)

Closes #2697.


Summary

🤖 Generated by Copilot at 93d5641

This pull request fixes a potential error with the react-refresh plugin and the temporal dead zone issue by lazily accessing the module.exports object. It also improves the hot module replacement functionality by deferring the refresh function with a promise. The changes affect the @rspack/binding package and the crates/rspack_plugin_javascript/src/visitors/swc_visitor/react.rs file.

Related issue (if exists)

Walkthrough

🤖 Generated by Copilot at 93d5641

  • Fix potential TDZ error with react-refresh plugin by lazily accessing module.exports (link, link)
  • Defer refresh function call with a promise to ensure modules are fully loaded and initialized before updating components (link)
  • Update changelog for @rspack/binding package with patch version and message (link)

@changeset-bot
Copy link

changeset-bot bot commented Apr 11, 2023

🦋 Changeset detected

Latest commit: ca7ee35

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 17 packages
Name Type
@rspack/binding Patch
@rspack/postcss-loader Patch
@rspack/core Patch
webpack-test Patch
@rspack/cli Patch
@rspack/dev-middleware Patch
@rspack/dev-server Patch
@rspack/plugin-html Patch
benchmarkcase-rspack-react-refresh Patch
@rspack/dev-client Patch
@rspack/plugin-minify Patch
@rspack/plugin-node-polyfill Patch
@rspack/binding-darwin-arm64 Patch
@rspack/binding-darwin-x64 Patch
@rspack/binding-linux-x64-gnu Patch
@rspack/binding-win32-x64-msvc Patch
@rspack/fs Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions github-actions bot added the team The issue/pr is created by the member of Rspack. label Apr 11, 2023
@hyf0 hyf0 force-pushed the hyf_fix_832084908203948 branch from bf5a5c4 to b64cfcd Compare April 11, 2023 13:24
@hyf0 hyf0 force-pushed the hyf_fix_832084908203948 branch from 93d5641 to ca7ee35 Compare April 13, 2023 02:49
@hyf0 hyf0 marked this pull request as ready for review April 13, 2023 07:06
@hyf0 hyf0 added this pull request to the merge queue Apr 13, 2023
Merged via the queue into main with commit d28a9d0 Apr 13, 2023
@hyf0 hyf0 deleted the hyf_fix_832084908203948 branch April 13, 2023 07:09
@github-actions github-actions bot mentioned this pull request Apr 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

team The issue/pr is created by the member of Rspack.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unexpected throwing error of Rspack output

2 participants