UMD 定义中的 if(typeof exports === 'object') 期望什么环境

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

Webpack 生成以下 UMD 定义:

(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();
})

我的问题是这个标准或环境有什么用?

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

它类似于 CommonJS,但没有 module

根据@sokra this comment

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 规范指出:

  • 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.

这就是为什么webpack通过exports对象导出依赖:

else if(typeof exports === 'object')
    exports["rx-core-libs"] = factory()