0
我正在使用Google Closure,並試圖製作Chrome打包的應用程序。谷歌關閉和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;
}
};
是谷歌關閉谷歌瀏覽器打包應用程序不兼容? 關閉對於大型Javascript項目有很多好處,因此很難放棄這樣一個有價值的工具。
編輯:我知道,如果除了Closure庫使用Closure編譯器,沒有goog.require,但這顯然使開發和調試更困難。
它保證工作?看起來,當我嘗試在另一個項目上動態加載Javascript時,腳本以不確定的順序異步執行。 –
我證實了這一觀察。它可能適用於小文件或其他內容,但是JavaScript是非順序加載和評估的。 –
這必須是谷歌使用document.write的原因。您可以嘗試calcdeps.py併爲所需的所有文件創建<腳本標記。您可以使用輸出列表來獲取列表,然後創建<腳本標記以在使用它們之前加載所有需要的js文件。 goog.require應該檢測到文件已經加載並且不會執行document.write。 https://developers.google.com/closure/library/docs/calcdeps#opts – HMR