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(node): use eval for require to avoid bundler issue #3239

Merged
merged 4 commits into from
Nov 18, 2024

Conversation

ScriptedAlchemy
Copy link
Member

Description

Related Issue

Types of changes

  • Docs change / refactoring / dependency upgrade
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)

Checklist

  • I have added tests to cover my changes.
  • All new and existing tests passed.
  • I have updated the documentation.

Copy link

changeset-bot bot commented Nov 17, 2024

🦋 Changeset detected

Latest commit: d6e8545

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

This PR includes changesets to release 27 packages
Name Type
@module-federation/node Patch
@module-federation/modern-js Patch
@module-federation/nextjs-mf Patch
@module-federation/runtime Patch
@module-federation/enhanced Patch
@module-federation/rspack Patch
@module-federation/webpack-bundler-runtime Patch
@module-federation/sdk Patch
@module-federation/runtime-tools Patch
@module-federation/managers Patch
@module-federation/manifest Patch
@module-federation/dts-plugin Patch
@module-federation/third-party-dts-extractor Patch
@module-federation/devtools Patch
@module-federation/bridge-react Patch
@module-federation/bridge-vue3 Patch
@module-federation/bridge-shared Patch
@module-federation/bridge-react-webpack-plugin Patch
@module-federation/retry-plugin Patch
@module-federation/data-prefetch Patch
@module-federation/rsbuild-plugin Patch
@module-federation/error-codes Patch
@module-federation/storybook-addon Patch
@module-federation/modernjsapp Patch
@module-federation/esbuild Patch
@module-federation/utilities Patch
website-new 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

Copy link

netlify bot commented Nov 17, 2024

Deploy Preview for module-federation-docs ready!

Name Link
🔨 Latest commit d6e8545
🔍 Latest deploy log https://app.netlify.com/sites/module-federation-docs/deploys/673a54d59b5fa70008a5b091
😎 Deploy Preview https://deploy-preview-3239--module-federation-docs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@ScriptedAlchemy ScriptedAlchemy marked this pull request as ready for review November 18, 2024 22:20
Copy link
Contributor

@squadronai squadronai bot left a comment

Choose a reason for hiding this comment

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

Summary

This pull request introduces a fix for a bundler issue in the Node.js package. The core change is the use of eval('require') instead of the standard require function to handle hot reloading. This change ensures that the application can properly load dependencies and function correctly in the presence of a bundler, which was causing issues previously.

The change is focused on improving the integration of the Node.js package with the existing codebase by addressing a critical bug related to the bundler. The pull request is a non-breaking change that fixes an issue, without introducing any new features.

File Summaries
File Summary
packages/node/src/utils/hot-reload.ts The code changes introduce a fix for a bundler issue by using eval('require') instead of the standard require function to handle hot reloading. This change ensures that the application can properly load dependencies and function correctly in the presence of a bundler.

Copy link
Contributor

@squadronai squadronai bot left a comment

Choose a reason for hiding this comment

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

Incremental Review

Comments posted: 2

Configuration

Squadron Mode: essential

Commits Reviewed

9ec100313de189b61a316f134926e37417e906e0...d6e8545701c1dacca831756e0093d26149434891

Files Reviewed
  • packages/node/src/utils/hot-reload.ts
Files Ignored

These files were ignored due to the filter in the squadron.yaml file.

  • .changeset/eighty-schools-smell.md
  • .github/workflows/e2e-next-dev.yml

Comment on lines 12 to +15
//@ts-ignore
return typeof __non_webpack_require__ !== 'undefined'
? __non_webpack_require__
: require;
: eval('require');
Copy link
Contributor

Choose a reason for hiding this comment

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

The current implementation using eval('require') poses security risks and may be blocked by Content Security Policy (CSP). A safer alternative would be to use Function constructor or check for the global require object:

Suggested change
//@ts-ignore
return typeof __non_webpack_require__ !== 'undefined'
? __non_webpack_require__
: require;
: eval('require');
//@ts-ignore
return typeof __non_webpack_require__ !== 'undefined'
? __non_webpack_require__
: Function('return require')();

Additionally, consider adding a comment explaining why this workaround is necessary for webpack bundling to help future maintainers understand the purpose of this code.

Choose a reason for hiding this comment

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

Yep, we have this CSP problem :(
@ScriptedAlchemy

@@ -12,7 +12,7 @@
//@ts-ignore
Copy link
Contributor

Choose a reason for hiding this comment

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

The @ts-ignore comment should be replaced with a more specific @ts-expect-error to better document the expected type error. This helps catch issues if the type error is resolved in the future.

@ScriptedAlchemy ScriptedAlchemy merged commit 6d35cf7 into main Nov 18, 2024
16 checks passed
@ScriptedAlchemy ScriptedAlchemy deleted the use-eval-require branch November 18, 2024 22:38
@KyrieLii KyrieLii mentioned this pull request Nov 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants