esbuild icon indicating copy to clipboard operation
esbuild copied to clipboard

Introduce UMD as a new output format

Open prantlf opened this issue 5 years ago • 8 comments

Attempts to fix #507.

--format=...  Output format (iife | cjs | umd | esm, no default
              when not bundling, otherwise default is iife when
              platform is browser and cjs when platform is node)

UMD format is similar to the IIFE format. Just the module wrapper and setting the global variable differs. For example:

// IIFE
var moduleName = (() => {
  ... bundled code ...
  return exports;
})();

// UMD
(function(root, factory) {
  if (typeof define === 'function' && define.amd) {
    define(factory);
  } else if (typeof module === 'object' && module.exports) {
    module.exports = factory();
  } else {
    root.moduleName = factory();
  }
}(typeof self !== 'undefined' ? self : this, () => {
  ... bundled code ...
  return exports;
}));

The module contents is generated with the help of the CJS wrapper, like IIFE.

This change supports only building of standalone applications or libraries. All dependencies have to be included inside the output bundle or loaded by global variables. Referring to external dependencies using AMD or CJS is not supported. Internal dependencies can be referred to according to input module formats supported by esbuild

prantlf avatar Nov 09 '20 13:11 prantlf

Would be super cool to see this merged. Thanks for your work on this PR @prantlf ! 🙇🏽

imbhargav5 avatar Apr 21 '21 11:04 imbhargav5

oh my god, i want this

cloverhearts avatar Apr 26 '22 01:04 cloverhearts

i want this too

langhuihui avatar Jun 14 '22 04:06 langhuihui

Would be very useful to me too, what is blocking this to get merged?

EDIT: Definitely would like to get this merged, but in the meantime i used babel + babel-plugin-transform-umd as a post-processing phase after esbuild.

EDIT2: In case anyone is interested in how to do that (⚠️ not super clean code ahead, need to refactor/test/document it yet, but it works in a stable way anyway):

WNemencha avatar Jun 19 '22 14:06 WNemencha

Go for it bois

Vanilagy avatar Nov 06 '22 22:11 Vanilagy

lesss goooo

klarstrup avatar Mar 08 '23 15:03 klarstrup

image

mlshv avatar Jun 05 '23 15:06 mlshv