Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.3k views
in Technique[技术] by (71.8m points)

javascript - Google Closure Compiler with advanced optimization for multiple scripts

I've recently found the Google closure compiler, and it seems very good at what it does.

That being said I'm running into issues when using multiple external code files at once. In my html file I might for example have two different javascript codes embedded:

<script src="code1.js"></script>
<script src="code2.js"></script>

Now I can use the Closure compiler easily to compile and compress both of these scripts individually with advanced optimization, but there might (and most likely will) be issues by both of these scripts overwriting each other/functions being overwritten.

From my own research (reading all the guides on developers.google.com/closure/compiler), there seem to be three solutions to this:

  • Concatenate the two javascript files into one. The compiler even has a handy function to do this for you

Issue here is that I can't do this, because one part of this javascript code is specific to this html file, while the other part is used elsewhere as well.

  • Define all variables and functions of one script as externs for the script.

Unless there is some way that does this automatically this is too much manual work in my case to make it feasible.

  • Use SIMPLE_OPTIMIZATIONS as optimization level

This is what I am currently doing, but obviously if there was a (realistic) way to use the more advanced optimizations, I would prefer using those.

To clarify, the two scripts do not reference each other, the only reason manually optimizing both individually doesn't work is because closure compiler might name a function a(b,c) in the first script and then another function in the second script a() as well, thus overwriting the references and functions of the first.

If there was some way


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...