简体   繁体   English

如何使用Closure Compiler优化Javascript并保留函数名称?

[英]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();}
  • Function dog is removed since I don't use it. 功能狗已删除,因为我不使用它。
  • Function famous_human is is optimized. 优化了函数Famous_human。
  • Function human has its original name since I want to call it from other code. 人的功能有其原始名称,因为我想从其他代码中调用它。
  • No code from code-calling-functions-in-lib.js 没有来自lib.js中的code-calling-functions-in的代码

.

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. 由于导出符号会阻止消除死代码,因此最佳实践是将特定于项目的导出保存在单独的文件中,而不包含在库中。

Example

Library Source - liba.js 图书馆资源-liba.js

function dog() {
  return 'Fido';
}

function famous_human() {
  return 'Winston';
}

function human() {
  return famous_human();
}

Project Specific Exports - project_exports.js 项目特定的导出-project_exports.js

window['human'] = human;

Compile command 编译命令

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.

相关问题 如何帮助Google的Closure Compiler优化我的JavaScript代码? - How can I help Google's Closure Compiler optimize my JavaScript code? 如何优化此Javascript函数? - How do I optimize this Javascript function? 闭包编译器:保留函数参数名称? - Closure Compiler: Preserve function argument names? 如何防止闭包保留javascript函数的参数? - How do I prevent an argument to a javascript function being retained by a closure? 如何使用Google Closure编译器删除未使用的JavaScript代码? - How do I use Google Closure compiler to remove unused JavaScript code? 如何使用Google Closure编译器使巨大的javascript文件更加有效和可维护? - How do I use google closure compiler to make a gigantic javascript file more efficient and maintainable? 如何使用 Google 的 Closure Compiler 将我的 javascript 拆分为模块? - How do I split my javascript into modules using Google's Closure Compiler? 在调用函数时,如何让Closure Compiler停止抱怨union类型? - How do I get the Closure Compiler to stop complaining about union types when calling a function? 如何优化此Javascript? - How do I optimize this Javascript? 如何在JavaScript中为Google闭包编译器设置参数类型? - How can I set type of arguments in JavaScript for Google closure compiler?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM