我正在开发一个 javascript 库,它使用闭包编译器来组合/缩小和类型检查。为了避免噘嘴全局命名空间,我想使用 UMD 模式并关闭@export(or goog.exportSymbol('workspace', lkr.workspace)
goog.provide('workspace');
goog.require('lkr.workspace');
/**
* Exposed external access point
* @export
* @return {component}
*/
workspace = function() {
return lkr.workspace.Core;
}
我使用了一个输出包装器文件来生成 UMD 包装器
//UMD bundling closure code inside.
;(function (root, factory) {
if (typeof define === 'function' && define.amd) {
define([], factory);
} else if (typeof module === 'object' && module.exports) {
module.exports = factory();
} else {
root.workspace = factory();
}
}(this, function () {
%output%
return workspace;
}));
- 这是关闭 UMD 模式的正确方法吗,编译器中似乎有内部支持来检测 UMD,但我找不到任何示例。 https://groups.google.com/forum/#!topic/closure-compiler-discuss/-M1HBUn35fs https://github.com/google/closure-compiler/pull/1048
- 我仍然会崩溃,似乎找不到工作区。
例子
开始.js
goog.provide('workspace');
/**
* Exposed external access point
* @export
* @return {number}
*/
var workspace = function() {
console.log('My workspace')
return 0;
}
编译标志
closure_entry_point: 'workspace',
compilation_level: ADVANCED_OPTIMIZATION,
only_closure_dependencies: true,
generate_exports :true,
language_in : 'ECMASCRIPT5_STRICT',
language_out : 'ES5_STRICT',
使用 UMD 包装器输出
(function(root, factory) {
if (typeof define === 'function' && define.amd) {
define([], factory);
} else if (typeof exports === 'object') {
module.exports = factory();
} else {
root.workspace = factory();
}
}(this, function() {
'use strict';
'use strict';
function a() {
console.log("My workspace");
return 0
}
var b = ["workspace"]
, c = this;
b[0]in c || !c.execScript || c.execScript("var " + b[0]);
for (var d; b.length && (d = b.shift()); )
b.length || void 0 === a ? c[d] ? c = c[d] : c = c[d] = {} : c[d] = a;
return workspace;
}));
错误:
Uncaught TypeError: Cannot use 'in' operator to search for 'workspace' in undefined
Uncaught ReferenceError: workspace is not defined