Skip to content

Conversation

eps1lon
Copy link
Collaborator

@eps1lon eps1lon commented Aug 21, 2025

Noticed this when reviewing #34193.

We have an externals property for each bundle we build. This is supposed to prevent bundling modules that are listed in that entry.

However, all Node.js modules were also treated as externals by @rollup/plugin-node-resolve. This is wrong for bundles that are not targeting Node.js (e.g. Bun or edge environments). Instead, bundles need explicitly list the modules they support. For example, edge environments do support the util module so they can list that. But edge environments don't support stream so it's not safe to mark that module as an external. It's not 100% accurate for modules that only have partial implementations. E.g. async_hooks is implemented in Bun and edge environments. But not all methods in that module (e.g. createHook) so you'd still have to be careful declaring async_hooks as an external in Bun.

I also removed some obvious unused externals from some bundles. We can't fail builds on unused externals since some bundle only use the external for a specific type e.g. react only uses ReactNativeInternalFeatureFlags for the RN bundles.

Error with unexpected externals
 BUILDING  react-server-dom-webpack-server.node.unbundled.development.js (node_dev)

"async_hooks" is imported by "packages/react-server/src/forks/ReactFlightServerConfig.dom-node.js", but could not be resolved – treating it as an external dependency.

error Command failed with exit code 1.

@meta-cla meta-cla bot added the CLA Signed label Aug 21, 2025
@github-actions github-actions bot added the React Core Team Opened by a member of the React Core Team label Aug 21, 2025
@eps1lon eps1lon force-pushed the sebbie/explicit-externals branch 2 times, most recently from 83114a7 to 994ebab Compare August 21, 2025 09:57
@react-sizebot
Copy link

react-sizebot commented Aug 21, 2025

Comparing: 698bb4d...d4f225f

Critical size changes

Includes critical production bundles, as well as any change greater than 2%:

Name +/- Base Current +/- gzip Base gzip Current gzip
oss-stable/react-dom/cjs/react-dom.production.js = 6.68 kB 6.68 kB = 1.83 kB 1.83 kB
oss-stable/react-dom/cjs/react-dom-client.production.js = 530.18 kB 530.18 kB = 93.39 kB 93.39 kB
oss-experimental/react-dom/cjs/react-dom.production.js = 6.69 kB 6.69 kB = 1.83 kB 1.83 kB
oss-experimental/react-dom/cjs/react-dom-client.production.js = 655.82 kB 655.82 kB = 115.31 kB 115.31 kB
facebook-www/ReactDOM-prod.classic.js = 675.59 kB 675.59 kB = 118.54 kB 118.54 kB
facebook-www/ReactDOM-prod.modern.js = 666.02 kB 666.02 kB = 116.87 kB 116.87 kB

Significant size changes

Includes any change greater than 0.2%:

(No significant changes)

Generated by 🚫 dangerJS against d4f225f

@eps1lon eps1lon changed the title Stop treating all Node.js modules implicitly as externals Stop treating all Node.js builtins implicitly as externals Aug 21, 2025
@eps1lon eps1lon marked this pull request as ready for review August 21, 2025 10:02
@eps1lon eps1lon requested review from unstubbable and gnoff August 21, 2025 10:02
@eps1lon eps1lon force-pushed the sebbie/explicit-externals branch 2 times, most recently from 1de4568 to 797f820 Compare August 22, 2025 07:30
@eps1lon eps1lon force-pushed the sebbie/explicit-externals branch from 797f820 to d4f225f Compare August 22, 2025 16:01
Copy link
Member

@josephsavona josephsavona left a comment

Choose a reason for hiding this comment

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

nice, thanks!

@@ -402,8 +400,8 @@ function getPlugins(
forbidFBJSImports(),
// Use Node resolution mechanism.
resolve({
// skip: externals, // TODO: options.skip was removed in @rollup/plugin-node-resolve 3.0.0
preferBuiltins: bundle.preferBuiltins,
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Started this PR before bundle.preferBuiltins was added. That's also a valid API (but should probably be bundle.targetsNodeJS or something).

@eps1lon eps1lon merged commit 9eede45 into facebook:main Aug 25, 2025
241 checks passed
@eps1lon eps1lon deleted the sebbie/explicit-externals branch August 25, 2025 07:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed React Core Team Opened by a member of the React Core Team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants