What environment is expected by `if(typeof exports === 'object')` in UMD definition
Asked Answered
F

1

6

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.

Felodese answered 12/10, 2017 at 11:6 Comment(0)
F
7

According to this comment by @sokra:

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

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.

That's why webpack exports dependencies through exports object:

else if(typeof exports === 'object')
    exports["rx-core-libs"] = factory()
Felodese answered 22/11, 2017 at 7:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.