Skip to content

feat: improve external loading for universal (node + web) targets#21187

Merged
alexander-akait merged 6 commits into
mainfrom
claude/commonjs-universal-target-loading-9i96be
Jun 15, 2026
Merged

feat: improve external loading for universal (node + web) targets#21187
alexander-akait merged 6 commits into
mainfrom
claude/commonjs-universal-target-loading-9i96be

Conversation

@alexander-akait

Copy link
Copy Markdown
Member

Summary

In a universal (target: ["node", "web"]) ESM build, a node-commonjs external emitted a hoisted import { createRequire } from "module", which crashes the whole bundle when it loads in a browser — even when the require() sits behind a runtime guard like const m = isNode ? require("fs") : require("memory-fs"). This obtains createRequire lazily from process.getBuiltinModule() instead, guarded with typeof process (browser) and RuntimeTemplate.optionalChaining (old node without getBuiltinModule), so nothing is hoisted that breaks the browser. The same defensive loader now also covers the commonjs/commonjs2/commonjs-module/commonjs-static family in ESM output (gated on node being available, so pure-web bundles keep the plain require form), and output.globalObject resolves to globalThis for a version-less universal module target so global externals work in both node and the browser.

What kind of change does this PR introduce?

feat

Did you add tests for your changes?

Yes — test/configCases/externals/node-commonjs-universal (loader strategies), node-commonjs-universal-concatenated (concatenation path), universal-dynamic-import, universal-external-types (every supported external type) and universal-unsupported-externals (the unsupported ones), plus the test/helpers/supportsProcessGetBuiltinModule.js filter helper.

Does this PR introduce a breaking change?

No.

If relevant, what needs to be documented once your changes are merged or what have you already documented?

Document that commonjs/node-commonjs externals are supported in ESM output (loaded via createRequire) and that a universal module target uses globalThis as the global object.

Use of AI

Yes — implemented with Claude (Claude Code): design, code and tests were generated with AI and reviewed/validated by the author per the webpack AI policy.

https://claude.ai/code/session_0194U8nMBSXzmUv9ex86frCQ


Generated by Claude Code

In a universal target (e.g. `["node", "web"]` with `output.module`), a
`node-commonjs` external emitted a hoisted `import { createRequire } from
"module"`, which crashes the whole ESM bundle when loaded in a browser even
when the `require()` sits behind a runtime guard.

Obtain `createRequire` via `process.getBuiltinModule("module")` instead:
inline-guarded when the node version is known to support it
(`environment.nodeBuiltinModuleGetter`), otherwise wrapped in try/catch so the
bundle still loads where `process` is absent. Pure-node builds are unchanged.
Verifies the other safe universal external path: a dynamic `import()` of a
node built-in stays lazy (no hoisted static import that would crash a browser
at load) and resolves correctly when run in node.
… buildInfo

Read `externalsPresets` from `runtimeTemplate.compilation` at code generation
instead of stashing a `universalExternal` flag on every external module's
`buildInfo`. `runtimeTemplate` is threaded into every `codeGeneration` call
(including concatenated inner modules, where the context `compilation` is
absent), so the universal target is detected directly without per-module state.

Add a regression test covering a node-commonjs external that gets concatenated.
Adds a matrix config case asserting the external types usable in a universal
ESM build run correctly: node-commonjs (require), var (expression), module
(static import) and import (dynamic).
…universal

Expands the matrix case to every external type usable in a universal ESM build
(node-commonjs, var, assign, module, promise, import) and adds a negative case
asserting require-based externals (commonjs family, this) throw there.
@changeset-bot

changeset-bot Bot commented Jun 15, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: cf27ea1

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

This PR includes changesets to release 1 package
Name Type
webpack Minor

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 commented Jun 15, 2026

Copy link
Copy Markdown
Contributor

This PR is packaged and the instant preview is available (877f3dc).

Install it locally:

  • npm
npm i -D webpack@https://pkg.pr.new/webpack@877f3dc
  • yarn
yarn add -D webpack@https://pkg.pr.new/webpack@877f3dc
  • pnpm
pnpm add -D webpack@https://pkg.pr.new/webpack@877f3dc

@codecov

codecov Bot commented Jun 15, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 94.73684% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 92.69%. Comparing base (793aae6) to head (cf27ea1).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
lib/ExternalModule.js 94.11% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #21187      +/-   ##
==========================================
- Coverage   92.70%   92.69%   -0.01%     
==========================================
  Files         588      588              
  Lines       64079    64091      +12     
  Branches    17780    17785       +5     
==========================================
+ Hits        59407    59412       +5     
- Misses       4672     4679       +7     
Flag Coverage Δ
css-parsing 28.69% <10.52%> (-0.01%) ⬇️
html5lib 31.19% <10.52%> (-0.01%) ⬇️
integration 88.67% <94.73%> (-0.01%) ⬇️
test262 45.54% <10.52%> (-0.05%) ⬇️
unit 41.03% <15.78%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

… ESM

Two more external types now work in a universal ESM target:

- commonjs/commonjs2/commonjs-module/commonjs-static load via `createRequire`
  in ESM output (with the defensive universal loader), gated on node being
  available so pure-web bundles keep the plain `require` form.
- `output.globalObject` resolves to `globalThis` for a version-less universal
  module target, so `global` externals work in both node and the browser.

The defensive node-commonjs loader obtains `createRequire` from
`process.getBuiltinModule()`, guarded with `typeof process` for the browser and
`RuntimeTemplate.optionalChaining` (optional chaining or an `&&` fallback) for
old node without `getBuiltinModule`.
@alexander-akait alexander-akait force-pushed the claude/commonjs-universal-target-loading-9i96be branch from c817c89 to cf27ea1 Compare June 15, 2026 13:49
@codspeed-hq

codspeed-hq Bot commented Jun 15, 2026

Copy link
Copy Markdown

Merging this PR will improve performance by 85.08%

⚠️ Different runtime environments detected

Some benchmarks with significant performance changes were compared across different runtime environments,
which may affect the accuracy of the results.

Open the report in CodSpeed to investigate

⚡ 3 improved benchmarks
✅ 141 untouched benchmarks

Performance Changes

Mode Benchmark BASE HEAD Efficiency
Memory benchmark "asset-modules-inline", scenario '{"name":"mode-development-rebuild","mode":"development","watch":true}' 1,294.9 KB 325.1 KB ×4
Memory benchmark "many-modules-commonjs", scenario '{"name":"mode-production","mode":"production"}' 9.2 MB 7.2 MB +27.4%
Memory benchmark "wasm-modules-sync", scenario '{"name":"mode-production","mode":"production"}' 8 MB 6.4 MB +24.93%

Tip

Curious why this is faster? Comment @codspeedbot explain why this is faster on this PR, or directly use the CodSpeed MCP with your agent.


Comparing claude/commonjs-universal-target-loading-9i96be (cf27ea1) with main (793aae6)

Open in CodSpeed

@github-actions

Copy link
Copy Markdown
Contributor

Types Coverage

Coverage after merging claude/commonjs-universal-target-loading-9i96be into main will be
99.33%
Coverage Report<
FileStmtsBranchesFuncsLinesUncovered Lines
bin
   webpack.js98.77%100%100%98.77%91
examples
   build-common.js100%100%100%100%
   buildAll.js100%100%100%100%
   examples.js100%100%100%100%
   template-common.js98.21%100%100%98.21%72
examples/custom-javascript-parser
   test.filter.js100%100%100%100%
examples/custom-javascript-parser/internals
   acorn-parse.js100%100%100%100%
   meriyah-parse.js100%100%100%100%
   oxc-parse.js91.30%100%100%91.30%140, 142–143, 145, 147, 153–154, 161, 168, 90
examples/markdown
   webpack.config.mjs100%100%100%100%
examples/typescript
   test.filter.js100%100%100%100%
examples/typescript-non-erasable
   test.filter.js50%100%100%50%5
examples/virtual-modules
   test.filter.js100%100%100%100%
examples/wasm-bindgen-esm
   test.filter.js100%100%100%100%
examples/wasm-complex
   test.filter.js100%100%100%100%
examples/wasm-simple
   test.filter.js100%100%100%100%
examples/wasm-simple-source-phase
   test.filter.js100%100%100%100%
lib
   APIPlugin.js100%100%100%100%
   AsyncDependenciesBlock.js100%100%100%100%
   AutomaticPrefetchPlugin.js100%100%100%100%
   BannerPlugin.js100%100%100%100%
   Cache.js98.21%100%100%98.21%101
   CacheFacade.js100%100%100%100%
   Chunk.js99.72%100%100%99.72%39
   ChunkGraph.js100%100%100%100%
   ChunkGroup.js100%100%100%100%
   ChunkTemplate.js100%100%100%100%
   CleanPlugin.js99.15%100%100%99.15%206, 226
   CodeGenerationResults.js100%100%100%100%
   CompatibilityPlugin.js100%100%100%100%
   Compilation.js98.44%100%100%98.44%1609, 1905, 1912, 1920, 1942, 2838, 3317–3318, 3350, 4014, 4044, 4097–4098, 4102, 4107, 4123–4124, 4138–4139, 4144–4145, 4622, 4648, 513, 518, 5456, 5488, 5505, 5521, 5537, 5552, 5577–5578, 5580, 5908, 5913, 5919, 5922, 5934, 5936, 5940, 5956, 5971, 6003, 6057, 6081, 6195, 763–764
   Compiler.js99.56%100%100%99.56%1135–1136, 1144
   ConcatenationScope.js98.59%100%100%98.59%189
   ConditionalInitFragment.js100%100%100%100%
   ConstPlugin.js100%100%100%100%
   ContextExclusionPlugin.js100%100%100%100%
   ContextModule.js100%100%100%100%
   ContextModuleFactory.js97.40%100%100%97.40%258, 395, 418, 420, 424, 433–434
   ContextReplacementPlugin.js100%100%100%100%
   DefinePlugin.js99%100%100%99%171–172, 188, 207, 281
   DependenciesBlock.js100%100%100%100%
   Dependency.js98.15%100%100%98.15%382, 428
   DependencyTemplate.js100%100%100%100%
   DependencyTemplates.js100%100%100%100%
   DotenvPlugin.js98.41%100%100%98.41%378, 391–392
   DynamicEntryPlugin.js100%100%100%100%
   EntryOptionPlugin.js100%100%100%100%
   EntryPlugin.js100%100%100%100%
   Entrypoint.js100%100%100%100%
   EnvironmentPlugin.js97.14%100%100%97.14%49
   ErrorHelpers.js100%100%100%100%
   EvalDevToolModulePlugin.js100%100%100%100%
   EvalSourceMapDevToolPlugin.js100%100%100%100%
   ExportsInfo.js100%100%100%100%
   ExportsInfoApiPlugin.js100%100%100%100%
   ExternalModule.js98.67%100%100%98.67%1062, 1065, 448–452, 600
   ExternalModuleFactoryPlugin.js100%100%100%100%
   ExternalsPlugin.js100%100%100%100%
   FileSystemInfo.js99.50%100%100%99.50%182, 2252–2253, 2256, 2267, 2278, 2289, 278, 3693, 3708, 3732
   FlagAllModulesAsUsedPlugin.js100%100%100%100%
   FlagDependencyExportsPlugin.js98.46%100%100%98.46%425, 434, 436, 440
   FlagDependencyUsagePlugin.js100%100%100%100%
   FlagEntryExportAsUsedPlugin.js100%100%100%100%
   Generator.js100%100%100%100%
   HotModuleReplacementPlugin.js100%100%100%100%
   HotUpdateChunk.js100%100%100%100%
   IgnorePlugin.js100%100%100%100%
   IgnoreWarningsPlugin.js100%100%100%100%
   InitFragment.js100%100%100%100%
   JavascriptMetaInfoPlugin.js100%100%100%100%
   LibraryTemplatePlugin.js100%100%100%100%
   LoaderOptionsPlugin.js100%100%100%100%
   LoaderTargetPlugin.js100%100%100%100%
   MainTemplate.js100%100%100%100%
   ManifestPlugin.js100%100%100%100%
   Module.js98.50%100%100%98.50%1285, 1290, 1350, 1364, 1426, 1435
   ModuleFactory.js100%100%100%100%
   ModuleFilenameHelpers.js98.85%100%100%98.85%106, 108
   ModuleGraph.js99.73%100%100%99.73%1005
   ModuleGraphConnection.js100%100%100%100%
   ModuleInfoHeaderPlugin.js100%100%100%100%
   ModuleNotFoundError.js100%100%100%100%
   ModuleProfile.js100%100%100%100%
   ModuleSourceTypeConstants.js100%100%100%100%
   ModuleTemplate.js100%100%100%100%
   ModuleTypeConstants.js100%100%100%100%
   MultiCompiler.js99.69%100%100%99.69%659
   MultiStats.js100%100%100%100%
   MultiWatching.js100%100%100%100%
   NoEmitOnErrorsPlugin.js100%100%100%100%
   NodeStuffPlugin.js100%100%100%100%
   NormalModule.js97.90%100%100%97.90%1240, 1243, 1260, 1277, 1524, 1558, 1574, 1661, 2017, 2316, 2321–2331, 419, 423, 577
   NormalModuleFactory.js99.47%100%100%99.47%1083, 1392, 486, 498
   NormalModuleReplacementPlugin.js100%100%100%100%
   NullFactory.js100%100%100%100%
   OptimizationStages.js100%100%100%100%
   OptionsApply.js100%100%100%100%
   Parser.js100%100%100%100%
   PlatformPlugin.js100%100%100%100%
   PrefetchPlugin.js100%100%100%100%
   ProgressPlugin.js98.85%100%100%98.85%519–520, 525, 527, 591
   ProvidePlugin.js100%100%100%100%
   RawModule.js100%100%100%100%
   RecordIdsPlugin.js100%100%100%100%
   RequestShortener.js100%100%100%100%
   ResolverFactory.js100%100%100%100%
   RuntimeGlobals.js100%100%100%100%
   RuntimeModule.js100%100%100%100%
   RuntimePlugin.js100%100%100%100%
   RuntimeTemplate.js100%100%100%100%
   SelfModuleFactory.js100%100%100%100%
   SingleEntryPlugin.js100%100%100%100%
   SourceMapDevToolModuleOptionsPlugin.js100%100%100%100%
   SourceMapDevToolPlugin.js98.62%100%100%98.62%220, 224, 226, 419, 430, 891
   Stats.js100%100%100%100%
   Template.js100%100%100%100%
   TemplatedPathPlugin.js99.17%100%100%99.17%176–177
   UseStrictPlugin.js100%100%100%100%
   WarnCaseSensitiveModulesPlugin.js100%100%100%100%
   WarnDeprecatedOptionPlugin.js100%100%100%100%
   WarnNoModeSetPlugin.js100%100%100%100%
   WatchIgnorePlugin.js100%100%100%100%
   Watching.js100%100%100%100%
   WebpackError.js100%100%100%100%
   WebpackIsIncludedPlugin.js100%100%100%100%
   WebpackOptionsApply.js100%100%100%100%
   WebpackOptionsDefaulter.js100%100%100%100%
   buildChunkGraph.js99.87%100%100%99.87%371
   cli.js98.62%100%100%98.62%10, 119, 545, 577, 627, 897
   index.js99.72%100%100%99.72%179
   validateSchema.js94.67%100%100%94.67%100, 87, 89, 98
   webpack.js96.33%100%100%96.33%10, 198, 220, 222
lib/asset
   AssetBytesGenerator.js100%100%100%100%
   AssetBytesParser.js100%100%100%100%
   AssetGenerator.js100%100%100%100%
   AssetModule.js100%100%100%100%
   AssetModulesPlugin.js97.32%100%100%97.32%281, 305, 308, 36, 360, 41
   AssetParser.js100%100%100%100%
   AssetSourceGenerator.js100%100%100%100%
   AssetSourceParser.js100%100%100%100%
   RawDataUrlModule.js100%100%100%100%
lib/async-modules
   AsyncModuleHelpers.js100%100%100%100%
   AwaitDependenciesInitFragment.js100%100%100%100%
   InferAsyncModulesPlugin.js100%100%100%100%
lib/cache
   AddBuildDependenciesPlugin.js100%100%100%100%
   AddManagedPathsPlugin.js100%100%100%100%
   IdleFileCachePlugin.js97.92%100%100%97.92%71, 83, 91
   MemoryCachePlugin.js95.83%100%100%95.83%33
   MemoryWithGcCachePlugin.js93.15%100%100%93.15%106, 113–114, 122, 89
   PackFileCacheStrategy.js96.40%100%100%96.40%1250, 1350, 1354, 1416, 628, 647, 657–659, 661, 677–678, 683, 686, 688, 693, 698, 722, 728, 762, 768, 774, 779, 790, 799, 804–805, 807, 824, 830–831, 833
   ResolverCachePlugin.js100%100%100%100%
   getLazyHashedEtag.js100%100%100%100%
   mergeEtags.js100%100%100%100%
lib/config
   browserslistTargetHandler.js100%100%100%100%
   defaults.js99.30%100%100%99.30%1439–1441, 1449, 274, 277, 282, 286
   normalization.js99.01%100%100%99.01%191–192, 258, 273
   target.js100%100%100%100%
lib/container
   ContainerEntryDependency.js100%100%100%100%
   ContainerEntryModule.js100%100%100%100%
   ContainerEntryModuleFactory.js100%100%100%

@alexander-akait alexander-akait merged commit 877f3dc into main Jun 15, 2026
64 of 65 checks passed
@alexander-akait alexander-akait deleted the claude/commonjs-universal-target-loading-9i96be branch June 15, 2026 14:24
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.

1 participant