question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

RequireJS environment in browser will break the UMD module conversion

See original GitHub issue

Describe the bug

Say I have an umd module

;(function(root, factory) {
    // istanbul ignore next
    if (typeof define === "function" && define.amd) {
        // istanbul ignore next
        define([], factory)
    } else if (typeof module === "object" && module.exports) {
        module.exports = factory()
    } else {
        // istanbul ignore next
        root.regexpToAst = factory()
    }
})(
    typeof self !== "undefined"
        ? // istanbul ignore next
          self
        : this,
    function() {}
)

With the commonjs conversion, it returns code like

var require_regexp_to_ast = __commonJS({
  "../../node_modules/regexp-to-ast/lib/regexp-to-ast.js"(exports, module) {
    (function(root, factory) {
      if (typeof define === "function" && define.amd) {
        define([], factory);
      } else if (typeof module === "object" && module.exports) {
        module.exports = factory();
      } else {
        root.regexpToAst = factory();
      }
    })(typeof self !== "undefined" ? self : exports, function() {
      function RegExpParser2() {
      }

But this will break in runtime because there is global define in the browser since I have a requireJS environment. This module will be registered in the global AMD module instead of vite ESM module.

Validations

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
pakholeung37commented, Dec 4, 2021

Published a plugin to handle this case vite-plugin-treat-umd-as-commonjs.

0reactions
jkone27commented, Jun 29, 2022

this would be very useful for migrating legacy projects like apps using durandaljs and so… or other apps using requirejs amd modules

Read more comments on GitHub >

github_iconTop Results From Across the Web

vite-plugin-treat-umd-as-commonjs - npm
This plugin is for a paticular edge case. check RequireJS environment in browser will break the UMD module conversion.
Read more >
vite-plugin-treat-umd-as-commonjs - npm package - Snyk
This plugin is for a paticular edge case. check RequireJS environment in browser will break the UMD module conversion.
Read more >
Why AMD? - RequireJS
This page talks about the design forces and use of the Asynchronous Module Definition (AMD) API for JavaScript modules, the module API supported...
Read more >
How JavaScript works: the module pattern + comparing ...
How JavaScript works: the module pattern + comparing CommonJS, AMD, UMD, and ES6 Modules. This is post # 50 of the series, dedicated...
Read more >
JavaScript Reference Guide: JS module ecosystem
JavaScript modules are individual parts of a JavaScript program that ... environment could be Node.js // RequireJS (AMD) or a Web browser.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found