问题
I am developing a javascript library which uses closure compiler to combine/minify & typecheck. To avoid pouting global namespace I want to use UMD pattern & closue @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;
}
I have used an output-wrapper-file to generate the UMD wrapper
//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;
}));
Ref: https://medium.com/reflecting-on-bits/how-to-solve-missing-output-error-when-using-closure-compiler-7de6eac29776?swoff=true#.ntq9vav6s
- Is this the correct approach for UMD pattern in closure, there seems to be internal support in the compiler to detect UMD but I can't find any examples. https://groups.google.com/forum/#!topic/closure-compiler-discuss/-M1HBUn35fs https://github.com/google/closure-compiler/pull/1048
- I still get crashes seems workspace can't be located.
EXAMPLE
start.js
goog.provide('workspace');
/**
* Exposed external access point
* @export
* @return {number}
*/
var workspace = function() {
console.log('My workspace')
return 0;
}
Compilation flags
closure_entry_point: 'workspace',
compilation_level: ADVANCED_OPTIMIZATION,
only_closure_dependencies: true,
generate_exports :true,
language_in : 'ECMASCRIPT5_STRICT',
language_out : 'ES5_STRICT',
Output with UMD wrapper
(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;
}));
Error:
Uncaught TypeError: Cannot use 'in' operator to search for 'workspace' in undefined
Uncaught ReferenceError: workspace is not defined
回答1:
The only native support for the UMD pattern the compiler has is with the --process_common_js_modules
. That flag is used to bundle modules together and will remove the pattern - so not what you want.
Your problems are with your output wrapper. The compiler attempts to export workspace
by creating it as a property on the global this
object. Your output wrapper doesn't specify a this
object. Since you are in strict mode, it's also not auto-coerced to the global this
object.
Update your output wrapper to something like:
//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;
}.bind(this));
来源:https://stackoverflow.com/questions/39847332/google-closure-compiler-and-umd-pattern