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

Missing documentation on UMD format webpack uses to export libraries #5826

Closed
maxkoretskyi opened this issue Oct 14, 2017 · 6 comments
Closed

Comments

@maxkoretskyi
Copy link

Webpack generates the following UMD definition:

(function webpackUniversalModuleDefinition(root, factory) {
    // this is CommonJS/Node
    if(typeof exports === 'object' && typeof module === 'object')
        module.exports = factory();
    // this is AMD
    else if(typeof define === 'function' && define.amd)
        define([], factory);
    // what is this environment or standard?
    else if(typeof exports === 'object')        <------------- ???
        exports["rx-core-libs"] = factory();
    // Window/Global
    else
        root["rx-core-libs"] = factory();
})

My question is what is this standard or environment for?

else if(typeof exports === 'object')

It's like CommonJS but without module.

@sokra
Copy link
Member

sokra commented Oct 15, 2017

It looks like you just deleted/ignored our lovely crafted issue template. It was there for good reasons. Please help us solving your issue by answering the questions asked in this template. I'm closing this. Please open a new issue with filled issue template. Also make sure your issue is not a question. Questions should be posted on Stack Overflow.

@sokra sokra closed this as completed Oct 15, 2017
@maxkoretskyi
Copy link
Author

maxkoretskyi commented Oct 15, 2017

@sokra , what's the point of asking a question on SO if there's no one from the core team there? Webpack's documentation is so lacking and on top of that it's impossible to get an answer from the core team here. It would probably take you the same amount of time to put a short answer to my question that it took you to write that I should ask a question on SO.

@sokra
Copy link
Member

sokra commented Oct 16, 2017

Did you try to google UMD?

@maxkoretskyi
Copy link
Author

maxkoretskyi commented Oct 17, 2017

@sokra , if you're asking if I know what is UMD, the answer is yes. Moreover, I even looked through the common patterns here but couldn't identify what environment the line

else if(typeof exports === 'object')

expects.

As I understand CommonJS & Node are handled by this line in webpack's UMD implementation:

if(typeof exports === 'object' && typeof module === 'object')

@sokra
Copy link
Member

sokra commented Nov 2, 2017

There are two different CommonJs specs. CommonJS strict has only exports and no module. Node.js added module.exports but that's not part of the original spec.

@maxkoretskyi
Copy link
Author

maxkoretskyi commented Nov 2, 2017

@sokra , thanks for the answer. It's almost clear now. Just a few points to clarify:

this commonjs spec states that:

  • In a module, there is a free variable called "exports", that is an object that the module may add its API to as it executes.
    modules must use the "exports" object as the only means of exporting.
  • In a module, there must be a free variable "module", that is an Object.
    The "module" object must have a read-only, don't delete "id" property that is the top-level "id" of the module. The "id" property must be such that require(module.id) will return the exports object from which the module.id originated. (That is to say module.id can be passed to another module, and requiring that must return the original module).
    The "module" object may have a "uri" String that is the fully-qualified URI to the resource from which the module was created. The "uri" property must not exist in a sandbox.

So is my understanding correct that it's the module.exports that's missing in the spec, not module itself?

And also here:

exports["rx-core-libs"] = factory();

you can't do it like this:

exports = factory();

because that is illegal according to the spec? Or is it simply how in Node.js doing exports = {} doesn't have any effect since module.exports continues to point to the original object.

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