我正在使用 Google Closure,并且正在尝试制作 Chrome 打包应用程序。
我的调用goog.require
导致错误:
Uncaught document.write() is not available in packaged apps.
罪魁祸首在base.js
goog.writeScriptTag_ = function(src) {
if (goog.inHtmlDocument_()) {
var doc = goog.global.document;
// If the user tries to require a new symbol after document load,
// something has gone terribly wrong. Doing a document.write would
// wipe out the page.
if (doc.readyState == 'complete') {
// Certain test frameworks load base.js multiple times, which tries
// to write deps.js each time. If that happens, just fail silently.
// These frameworks wipe the page between each load of base.js, so this
// is OK.
var isDeps = /\bdeps.js$/.test(src);
if (isDeps) {
return false;
} else {
throw Error('Cannot write "' + src + '" after document load');
}
}
doc.write(
'<script type="text/javascript" src="' + src + '"></' + 'script>');
return true;
} else {
return false;
}
};
Google Closure 是否与 Google Chrome 打包应用不兼容?Closure 对于大型 Javascript 项目有很多好处,很难放弃这样一个有价值的工具。
编辑:我知道如果除了 Closure 库之外还使用 Closure Compiler,则没有 goog.require,但这显然会使开发和调试变得更加困难。