blog icon indicating copy to clipboard operation
blog copied to clipboard

umd 原理

Open nmsn opened this issue 3 years ago • 0 comments

核心代码

(function(root, factory) {
  if (typeof exports === "object" && typeof module === "object") {
    // CommonJS规范 node 环境 判断是否支持 module.exports 支持 require 这种方法
    module.exports = factory(require);
  } else if (typeof define === "function" && define.md) {
    // AMD 如果环境中有define函数,并且define函数具备amd属性,则可以判断当前环境满足AMD规范
    console.log("是AMD模块规范,如require.js");
    define(factory());
  } else if (typeof exports === "object") {
    // 不支持 module 但是支持 exports 的情况下使用 exports导出 是CommonJS 规范
    exports["jiang-hooks"] = factory();
  } else {
    // 直接挂载在全局对象上
    root.umdModule = factory();
  }
})(this, function() {
  return {
    name: "Umd模块",
  };
});

nmsn avatar Aug 16 '22 15:08 nmsn