我正在尝试为我们的服务编写可嵌入的扩展,以便人们可以通过<script src"/my/cds/path.js"></script>
导入它。作为示例,我一直在研究Stripe checkout。js的实现。当我将StripeCheckout添加到index.html中的脚本标记中时,下面的代码片段是从Stripe CDN加载的代码的一部分。
(function() {
var namespace = "StripeCheckout.require".split("."),
name = namespace[namespace.length - 1],
base = this,
i;
for (i = 0; i < namespace.length - 1; i++) { base = base[namespace[i]] = base[namespace[i]] || {} }
if (base[name] === undefined) {
base[name] = function() {
var modules = {},
cache = {};
var requireRelative = function(name, root) {
var path = expand(root, name),
indexPath = expand(path, "./index"),
module, fn;
module = cache[path] || cache[indexPath];
if (module) { return module } else if (fn = modules[path] || modules[path = indexPath]) {
module = { id: path, exports: {} };
cache[path] = module.exports;
fn(module.exports, function(name) { return require(name, dirname(path)) }, module);
return cache[path] = module.exports
} else { throw "module " + name + " not found" }
};
var expand = function(root, name) {
var results = [],
parts, part;
if (/^\.\.?(\/|$)/.test(name)) { parts = [root, name].join("/").split("/") } else { parts = name.split("/") }
for (var i = 0, length = parts.length; i < length; i++) { part = parts[i]; if (part == "..") { results.pop() } else if (part != "." && part != "") { results.push(part) } }
return results.join("/")
};
var dirname = function(path) { return path.split("/").slice(0, -1).join("/") };
var require = function(name) { return requireRelative(name, "") };
require.define = function(bundle) { for (var key in bundle) { modules[key] = bundle[key] } };
require.modules = modules;
require.cache = cache;
return require
}.call()
}
})();
,然后跟随多个类似于此的代码段:
StripeCheckout.require.define({
"lib/spellChecker": function(exports, require, module) {
(function() {
var levenshtein;
module.exports = {
levenshtein: levenshtein = function(str1, str2) {
},
suggest: function(dictionary, badword, threshold) {
}
}
}).call(this)
}
});
问题: