examples/universal/template.md
This example shows the universal target — target: "universal" — which makes a single webpack compiler emit one ESM bundle that runs in the browser, web workers, Node.js, Electron and NW.js. Because the output is plain ECMAScript modules with no platform assumptions baked in, the same bundle also runs on other ESM runtimes such as Deno and Bun.
The universal preset is the intersection of every platform webpack knows about: it only uses runtime features (chunk loading, global object, etc.) that all of them support, and output is always ECMAScript modules. The bundle bakes in no platform-specific assumptions, so anything platform-dependent is resolved at runtime instead. Because output is always ESM, experiments.outputModule and output.module default to true for this target — no extra config needed.
This example demonstrates the full potential of that setup:
example.js is built once and runs unchanged on each platform.env.js reports the current platform (browser, Node.js, Deno, Bun, …) without any build-time target branch.import("./render") is emitted as a separate .mjs chunk, and webpack generates a chunk loader that works everywhere (native import() in the browser, dynamic import() of the emitted module in Node).render.js writes to the DOM in a browser and to stdout in Node._{{example.js}}_
_{{env.js}}_
_{{render.js}}_
_{{webpack.config.js}}_
_{{dist/output.mjs}}_
_{{dist/render_js.mjs}}_
_{{stdout}}_
_{{production:stdout}}_