Google Closure Compiler - 基于 extern 的死代码删除
我正在尝试使用 Google Closure Compiler 通过单个变量根据应用程序代码的运行位置(在服务器上与客户端上)来拆分我的应用程序代码。在此示例中,将在服务器上调用的所有内容都位于 isServerSide var 后面,但是正在为客户端编译代码。因此,我将 isServerSide
设置为 false,并让编译器删除客户端不会运行的所有内容...
app.js
内部:
goog.provide('my.app');
my.app.log = function(message) {
document.write(message);
}
my.app.initClientSide = function() {
my.app.log('hello client');
}
my.app.initServerSide = function() {
my.app.log('hello server');
}
if (isServerSide) {
my.app.log('initing server');
my.app.initServerSide()
}else my.app.initClientSide();
内部>externs.js
:
/**
* @define {boolean} is server side?
*/
var isServerSide=false;
命令:
java -jar bin/compiler.jar --js closure-library/closure/goog/base.js --js app.js --externs externs.js --manage_closure_dependencies true --process_closure_primitives true --summary_detail_level 3 --warning_level VERBOSE --compilation_level=ADVANCED_OPTIMIZATIONS --closure_entry_point my.app
预期输出:
document.write("hello client");
实际输出:
isServerSide?(document.write("initing server"),document.write("hello server")):document.write("hello client");
如果我在 app.js
中手动输入 isServerSide=false;
那么我可以编译它对此:
isServerSide=false;document.write("hello client");
这让我觉得我正在建立我的externs.js
错误(或者我只是不明白 externs 实际用途是什么)。
关于如何让它发挥作用有什么建议吗?
I'm trying to use the Google Closure Compiler to split my application code based on where it's going to be run (on the server vs the client) via a single variable. In this example, everything that's going to be called on the server is behind an isServerSide
var, BUT, the code is being compiled for the client. So I'll set isServerSide
to false and let the compiler remove everything that wouldn't be run by the client...
Inside of app.js
:
goog.provide('my.app');
my.app.log = function(message) {
document.write(message);
}
my.app.initClientSide = function() {
my.app.log('hello client');
}
my.app.initServerSide = function() {
my.app.log('hello server');
}
if (isServerSide) {
my.app.log('initing server');
my.app.initServerSide()
}else my.app.initClientSide();
Inside of externs.js
:
/**
* @define {boolean} is server side?
*/
var isServerSide=false;
Command:
java -jar bin/compiler.jar --js closure-library/closure/goog/base.js --js app.js --externs externs.js --manage_closure_dependencies true --process_closure_primitives true --summary_detail_level 3 --warning_level VERBOSE --compilation_level=ADVANCED_OPTIMIZATIONS --closure_entry_point my.app
Expected output:
document.write("hello client");
Actual output:
isServerSide?(document.write("initing server"),document.write("hello server")):document.write("hello client");
If I manually type isServerSide=false;
in app.js
then I can get it to compile to this:
isServerSide=false;document.write("hello client");
Which makes me think I'm setting up my externs.js
wrong (or I just don't understand what externs should actually be used for).
Any suggestions on how to get this working?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以通过直接在编译器调用中设置 @define 值来指定它们。
外部程序有不同的目的,就像 hyperslug 正确指出的那样。
您可以通过将 @define 定义(来自您的 extern)放入 app.js 中,然后像这样调用编译器来实现预期结果:
You specify @define values by setting them in the compiler call directly.
Externs serve a different purpose like hyperslug correctly states.
You achieve the expected result by putting the @define definition(from your extern) into app.js and then calling the compiler like this: