React Shared Library vs UMD

 

A React shared library is a modular package designed to be imported into modern build pipelines, while a UMD (Universal Module Definition) build is a standalone script designed to run directly in a browser via a <script> tag or legacy module loaders. [1, 2, 3]
Key Differences
  • React Shared Library (ESM / CJS):
    • Intended Use: Modern applications using bundlers like Vite, Webpack, or Rollup.
    • Module System: Uses standard import/export (ESM) or Node's require (CommonJS).
    • Optimization: Fully supports tree-shaking (removing unused code) to keep production bundles small.
    • Workflow: Requires a build/bundling step before running in a browser. [1, 2, 3, 4, 5, 6]
  • UMD (Universal Module Definition):
    • Intended Use: Quick prototyping, legacy environments, or loading scripts directly via a CDN (Content Delivery Network).
    • Module System: Universal; it checks at runtime if module.exports, define (AMD), or a global window variable is available and attaches itself accordingly.
    • Optimization: No tree-shaking; ships as a single, self-contained monolithic file.
    • Workflow: Works instantly in a browser without a build system, exposing a global variable (like window.React). [1, 2, 3, 4, 5]
When to Use Which
  • Choose a shared library (ESM) format when building modern React monorepos, NPM packages, or standard web apps. [1, 2]
  • Choose UMD only when you need to drop React or a component directly into an old HTML page via a <script> tag without setting up Node.js or a bundler. [1, 2, 3]

Comments

Popular posts from this blog

LLM Engineering course : Day 1

Agentic AI Course : Week 1

SpringSecurity 1