Skip to content

Instantly share code, notes, and snippets.

@Gk0Wk
Created August 26, 2021 17:38
Show Gist options
  • Save Gk0Wk/594d469e1794930f332b0ed6ed10ec1c to your computer and use it in GitHub Desktop.
Save Gk0Wk/594d469e1794930f332b0ed6ed10ec1c to your computer and use it in GitHub Desktop.
UMD Module Template for JS
/* UMD 模块 https://blog.csdn.net/Real_Bird/article/details/54869066 不得不说没有ES6的export是真的麻烦 */
/* ()包住的是表达式,js会主动运行以求得值,所以其实不要一定是括号写法,也可以是 !f1(f2) 等等,目的是自动执行这段代码 */
(function(root, factory) {
/* 这段主要是用不同的方式去获取以一些依赖,如果没有依赖,其实可以不要这一段 */
if (typeof exports === "object" && typeof module === "object") // CommonJS规范(如NodeJS)
/* 依赖名称,相对路径../../lib/codemirror(.js)? (假设本文件是$:/plugins/tiddlywiki/codemirror/a/b/c.js) 和 绝对路径(如下)都可以 */
module.exports = factory(require("$:/plugins/tiddlywiki/codemirror/lib/codemirror.js"));
else if (typeof define === "function" && define.amd) // AMD规范(如require.js)
/* 同上,不过AMD的传参比较特殊,是把参数数组作为第一个参数,函数作为第二参数 */
define(["$:/plugins/tiddlywiki/codemirror/lib/codemirror.js"], factory);
else if (typeof define === "function" && define.cmd) // CMD规范(如sea.js)
/* 和ADM类似 */
define(function(require, exports, module) {
module.exports = factory(require("$:/plugins/tiddlywiki/codemirror/lib/codemirror.js"))
})
else // Plain browser env
/* 纯浏览器环境,CodeMirror已经被导入了,所以存在 */
root.模块名称 = factory(root.CodeMirror);
})(this, function(/* 依赖 */) {
"use strict";
/* 自己的初始化代码 */
/* 要导出/暴露的部分 */
return {}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment