如何使用闭包编译器从另一个压缩文件中获取变量
How to get variable from another compressed file with closure compiler
你好,我想通过 Google 集成 Closure 编译器,以使用 ADVANCED_OPTIMIZATIONS 模式压缩我的文件,但我有 2 个压缩文件,我需要在两者之间共享变量。
我阅读了这份文档https://developers.google.com/closure/compiler/docs/api-tutorial3
问题
我收到这个错误:
ReferenceError: getValue is not defined
所以我尝试用 window['getValue'] 替换 getValue 但它不起作用。
基本代码
第一个 JS 文件:
var nb0 = 0;
var nb1 = 1;
var nb2 = 2;
var nb3 = 3;
function getValue( nb ) {
return nb;
}
window['nb0'] = nb0;
window['nb1'] = nb1;
window['nb2'] = nb2;
window['nb3'] = nb3;
window['getValue'] = getValue;
第二个 JS 文件:
document.addEventListener("DOMContentLoaded", function() {
var val = getValue;
document.querySelector( ".button" ).addEventListener( "click", upButton );
function upButton() {
val++;
document.querySelector( ".show" ).innerText = val;
}
} );
该问题的解决方案在副标题下简要说明
Solution for Calling out from Compiled Code to External Code: Externs 在您链接的文档中。
我想您的困惑来自 "third-party" 和 "external" 这两个词。在本文档的上下文中,您可以假设 "third-party" 和 "external" 指的是其他人编写的代码, 和 指的是来自单独编译的任何文件的任何代码(由您或其他人)。
然后解决方案是在您不希望重命名的变量前添加 /** @export */
,或者为您的源代码定义一个 externs 文件。
备用 1
如果您希望继续以这种方式使用 window
(这可能很难看,恕我直言,有时这很合适),您应该更改
var val = getValue;
至
var val = window['getValue'];
例如:
document.addEventListener("DOMContentLoaded", function() {
var val = window['getValue'];
document.querySelector( ".button" ).addEventListener( "click", upButton );
function upButton() {
val++;
document.querySelector( ".show" ).innerText = val;
}
} );
编译为
document.addEventListener("DOMContentLoaded", function() {
var a = window.getValue;
document.querySelector(".button").addEventListener("click", function() {
a++;
document.querySelector(".show").innerText = a;
});
});
备用 2
使用ES6 modules. Closure Compiler supports these with the module_resolution
flag.
一般阅读:用模块封装代码
:
备用 3
使用 Google Closure Library's modules(goog.module、goog.require 和(已弃用)goog.provide)。
你好,我想通过 Google 集成 Closure 编译器,以使用 ADVANCED_OPTIMIZATIONS 模式压缩我的文件,但我有 2 个压缩文件,我需要在两者之间共享变量。
我阅读了这份文档https://developers.google.com/closure/compiler/docs/api-tutorial3
问题
我收到这个错误:
ReferenceError: getValue is not defined
所以我尝试用 window['getValue'] 替换 getValue 但它不起作用。
基本代码
第一个 JS 文件:
var nb0 = 0;
var nb1 = 1;
var nb2 = 2;
var nb3 = 3;
function getValue( nb ) {
return nb;
}
window['nb0'] = nb0;
window['nb1'] = nb1;
window['nb2'] = nb2;
window['nb3'] = nb3;
window['getValue'] = getValue;
第二个 JS 文件:
document.addEventListener("DOMContentLoaded", function() {
var val = getValue;
document.querySelector( ".button" ).addEventListener( "click", upButton );
function upButton() {
val++;
document.querySelector( ".show" ).innerText = val;
}
} );
该问题的解决方案在副标题下简要说明 Solution for Calling out from Compiled Code to External Code: Externs 在您链接的文档中。
我想您的困惑来自 "third-party" 和 "external" 这两个词。在本文档的上下文中,您可以假设 "third-party" 和 "external" 指的是其他人编写的代码, 和 指的是来自单独编译的任何文件的任何代码(由您或其他人)。
然后解决方案是在您不希望重命名的变量前添加 /** @export */
,或者为您的源代码定义一个 externs 文件。
备用 1
如果您希望继续以这种方式使用 window
(这可能很难看,恕我直言,有时这很合适),您应该更改
var val = getValue;
至
var val = window['getValue'];
例如:
document.addEventListener("DOMContentLoaded", function() {
var val = window['getValue'];
document.querySelector( ".button" ).addEventListener( "click", upButton );
function upButton() {
val++;
document.querySelector( ".show" ).innerText = val;
}
} );
编译为
document.addEventListener("DOMContentLoaded", function() {
var a = window.getValue;
document.querySelector(".button").addEventListener("click", function() {
a++;
document.querySelector(".show").innerText = a;
});
});
备用 2
使用ES6 modules. Closure Compiler supports these with the module_resolution
flag.
一般阅读:用模块封装代码 :
备用 3
使用 Google Closure Library's modules(goog.module、goog.require 和(已弃用)goog.provide)。