Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce UMD as a new output format #1331

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open

Conversation

gwuhaolin
Copy link

cherry-pick from #513 with conflicts resolved

prantlf and others added 5 commits May 25, 2021 15:08
    --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
    let 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.
@pateketrueke
Copy link

pateketrueke commented Jul 7, 2021

Definitely having no UMD support breaks legacy projects on some users, so much thanks @gwuhaolin for your effort on this dutie!

cc: @jridgewell

@pateketrueke
Copy link

For the record, you can achieve this in a hacky way by using the footer.js option with a snippet like this, e.g.

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

Of course, it would be better to having UMD support without having to worry about this nuance.

@ninjakuro
Copy link

rebase and try to push this request to master @gwuhaolin

by the way thanks a lot

@elbkind
Copy link

elbkind commented Mar 8, 2022

I would like to see this merged

@spencercap
Copy link

agreed! merge ETA?
thanks @prantlf 👏 + @gwuhaolin 👏

@mourner
Copy link

mourner commented Mar 29, 2022

This is the only feature preventing me from switching from Rollup to ESBuild as the author of 50+ open source JS libraries. Gently bringing this to your attention @evanw 🙏

@mistic100
Copy link

really would like this to be added

@mistic100
Copy link

mistic100 commented Nov 6, 2022

@gwuhaolin does this PR supports externals ?

for example https://github.com/mistic100/Photo-Sphere-Viewer/blob/master/dist/photo-sphere-viewer.js#L7-L11 (this is rollup.js output)

edit : well.... as currently a plugin is required to have external globals (esbuild-plugin-external-global), it may not work out of the box. Or said plugin will have to be merged in the core.

@DualWield
Copy link

why is this pull request still open?

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.

None yet

9 participants