You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat: add universal target preset (browser + web worker + Node.js + Electron + NW.js) (#21214)
* feat: add universal target preset and comprehensive all-module-types test
Add a dedicated `universal` target that merges `web` and `node` features,
so users can write `target: "universal"` instead of `target: ["web", "node"]`.
Add a comprehensive configCase exercising most module types webpack supports
(esm, commonjs, json, deferred imports, source-phase imports, externals,
dynamic import, top-level await, asset modules, css + css modules, html, wasm,
workers) run under node, web and the universal target.
Update the test runner to expand the `universal` preset to web + node when
running universal bundles once per environment.
* feat: universal target covers web workers and defaults to ESM output
Expand the `universal` preset to merge web + webworker + node so it also
covers Web Worker environments (previously `webworker` was `false`), and have
it always declare ECMAScript module support.
Default `experiments.outputModule` to true for universal targets: the target
only works as ESM, so `target: "universal"` now builds out of the box instead
of failing chunk-format selection.
* perf: inline universal target properties instead of merging at runtime
The universal preset resolved its properties by calling getTargetProperties
for web/webworker/node and merging on every lookup. Replace that with the
equivalent static literal (byte-identical result).
* feat: universal target also covers Electron/NW.js and never exposes require
Extend the universal preset to span Electron (all contexts) and NW.js in
addition to browser, web worker and Node.js, so it externalizes the matching
built-ins. Since universal output is always ESM, set `require` (synchronous
CommonJS require) to false; node built-ins and `global` stay ambiguous (null)
because they remain available under Node.js ESM.
* test: cover all asset module types and verify universal runs per-env
Add the remaining asset cases to the comprehensive universal test: asset/bytes,
auto asset, and the `type: text` / `type: bytes` import attributes, plus
css/global and import.meta.webpackContext.
Add a per-config environment assertion; the universal config executes the same
bundle in both a node-like and a web-like environment.
Copy file name to clipboardExpand all lines: lib/config/target.js
+31Lines changed: 31 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -174,6 +174,37 @@ You can also more options via the 'target' option: 'browserslist' / 'browserslis
174
174
global: false
175
175
})
176
176
],
177
+
[
178
+
"universal",
179
+
"Universal target running in browser, web worker, Node.js, Electron and NW.js. Output is always ECMAScript modules.",
180
+
/^universal$/,
181
+
// merged web + webworker + node + electron + nwjs properties; output is
182
+
// always ESM, so `require` (sync CommonJS) is never available
183
+
()=>({
184
+
node: null,
185
+
web: null,
186
+
webworker: null,
187
+
browser: null,
188
+
electron: null,
189
+
nwjs: null,
190
+
191
+
electronMain: null,
192
+
electronPreload: null,
193
+
electronRenderer: null,
194
+
195
+
document: null,
196
+
importScriptsInWorker: null,
197
+
fetchWasm: null,
198
+
nodeBuiltins: null,
199
+
importScripts: null,
200
+
require: false,
201
+
global: null,
202
+
203
+
module: true,
204
+
dynamicImport: true,
205
+
dynamicImportInWorker: true
206
+
})
207
+
],
177
208
[
178
209
"[async-]node[X[.Y]]",
179
210
"Node.js in version X.Y. The 'async-' prefix will load chunks asynchronously via 'fs' and 'vm' instead of 'require()'. Examples: node14.5, async-node10.",
0 commit comments