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

Can this please (optionally) ignore UMD wrapped modules #288

Closed
gillyspy opened this issue Feb 2, 2022 · 5 comments
Closed

Can this please (optionally) ignore UMD wrapped modules #288

gillyspy opened this issue Feb 2, 2022 · 5 comments

Comments

@gillyspy
Copy link

gillyspy commented Feb 2, 2022

It would be great if there was a way to tell the transform to ignore UMD wrapped modules.

We deploy to an environment that uses AMD but historically our testing has been using requirejs in node. We're switching to this because it's embedded in another package that will make our lives easier overall, buuuuut when we require modules written as below it fails

it would be great if no matter which type of module we are passing it would survive.

The pattern we have is something like this (grabbed this from one of my templates):

((define) => {
  define([], () => {
    const exports = {};
   //write your code here

    return exports;
  });
})(
  /**
   * @description UMD
   * @param {Array<strings>} m list of modules
   * @param {function} cb callback for define
   * @return {undefined}
   */
  (m, cb /* jshint -W014, -W030 */) => {
    const mod = module.exports;
    // eslint-disable-next-line no-unused-expressions,implicit-arrow-linebreak
    (typeof define === 'function')
      ? define(m, cb)
      : Object.assign(mod, cb(...m.map(require)));
  }
);

I'd be happy with a comment declaration if i had to as a quick starter.

@msrose
Copy link
Owner

msrose commented Feb 3, 2022

Hi @gillyspy, thanks for opening the issue. Couple of clarifying questions:

  • "our testing has been using requirejs in node": does this mean you're using RequireJS in Node.js, or just Node's built-in require?
  • Can you please provide the full error that you're seeing for the example module?

@msrose
Copy link
Owner

msrose commented Feb 3, 2022

Additionally, the restrictToTopLevelDefine options is worth toggling to see if anything changes for your UMD modules. Check out https://github.com/msrose/babel-plugin-transform-amd-to-commonjs#options for more info.

@gillyspy
Copy link
Author

gillyspy commented Feb 3, 2022

hey, thanks for the quick reply. wow. 🙏🏻

the deploy target system uses requirejs (or a customized AMD loader) .

depending on what i'm testing I'm using requirejs or require. I prefer require when i can get it so that jest can mock (which uses hoisting) and the environment context doesn't need to be modified (known issue with requirejs not knowing what global is with Jest).

in the above UMD-stle module pattern I can load with either. Many modules, including the native modules, legacy modules are written in AMD style.

in short... we test with require using modules that are expected to be transformed regardless of what they are.

The challenge here is in testing in Node (using Jest incidentally). The other package is what is calling your transform. It is not using requirejs (in node)-- internally it is calling your transform first.

So, If your transform was able to ignore modules (like my pattern above) then I wouldn't have to jump through so many hoops to test scenarios where AMD-only modules are mixed with UMD modules (let alone mixed with CommonJS modules, but that should actually work too of course cuz that's what UMD is doing)

i'll try that option but i'm not optimistic.. my hypothesis is that there is only a top level here -- it's just not one this babel plugin recognizes. also, not sure where yet to try it cuz it's kind of of buried in that oracle package (link above)

@msrose
Copy link
Owner

msrose commented Feb 8, 2022

Okay I'm having trouble wrapping my head around the use case exactly, and it complicates things that you don't have the ability to configure the transform directly, but regardless I think the request to have a way to exclude modules (e.g. via a comment at the top of the file), is a reasonable one. I should be able to pick this up this weekend, but if you would like to pick it up yourself I would happily review a pull request. Please let me know if you decide to do so so we don't duplicate work.

@msrose
Copy link
Owner

msrose commented Feb 24, 2022

@gillyspy #303 has been merged which gives an escape hatch to let you ignore modules by adding /* transform-amd-to-commonjs-ignore */ at the top of the file. I believe this should help fix your problem for specific modules.

It might make sense to do more testing with UMD modules at some point and add additional handling to ignore them specifically -- if anyone else has more examples of modules that need ignoring, please post on this thread and I'll reopen this issue.

@msrose msrose closed this as completed Feb 24, 2022
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

No branches or pull requests

2 participants