Google Closure Compiler - 基于 extern 的死代码删除

发布于 2024-12-14 04:38:32 字数 1546 浏览 9 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

巷子口的你 2024-12-21 04:38:32

您可以通过直接在编译器调用中设置 @define 值来指定它们。
外部程序有不同的目的,就像 hyperslug 正确指出的那样。

您可以通过将 @define 定义(来自您的 extern)放入 app.js 中,然后像这样调用编译器来实现预期结果:

java -jar compiler.jar \
--define "isServerSide=false" \
--js closure-library/closure/goog/base.js \
--js app.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

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:

java -jar compiler.jar \
--define "isServerSide=false" \
--js closure-library/closure/goog/base.js \
--js app.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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文