[英]How do I optimize Javascript with Closure Compiler and keep function names?
I have a javascript-file (lib.js) and I want to use some of the functions in an web page but I don't want to load the full lib.js. 我有一个javascript文件(lib.js),我想使用网页中的某些功能,但我不想加载完整的lib.js。
However, I have not figured out how to do what I want. 但是,我还没有弄清楚该怎么做。 I want to use command line. 我想使用命令行。
lib.js lib.js
function dog() {
return 'Fido';
}
function famous_human() {
return 'Winston';
}
function human() {
return famous_human();
}
code-calling-functions-in-lib.js 代码调用的函数式,lib.js
alert(human());
Desired result, lib-compiled.js 所需的结果,lib-compiled.js
function a() {return 'Winston';}function human() {return a();}
. 。
java -jar compiler.jar --compilation_level ADVANCED_OPTIMIZATIONS --js lib.js --XXXXXXXX code-calling-functions-in-lib.js --js_output_file lib-compiled.js
Is there a simple answer to my question? 我的问题有一个简单的答案吗?
You can export human: 您可以导出人:
function human() {
return famous_human();
}
window['human'] = human;
More information on exports 有关出口的更多信息
Since exporting symbols blocks dead-code elimination, a best practice is to keep the project-specific exports in a separate file and not included in your library. 由于导出符号会阻止消除死代码,因此最佳实践是将特定于项目的导出保存在单独的文件中,而不包含在库中。
function dog() {
return 'Fido';
}
function famous_human() {
return 'Winston';
}
function human() {
return famous_human();
}
window['human'] = human;
java -jar compiler.jar \
--compilation_level ADVANCED_OPTIMIZATIONS \
--js liba.js \
--js project_exports.js \
--js project_source.js \
--js_output_file project_compiled.js
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.