简体   繁体   English

WIKI:如何使用Lime(如何在第三方(闭包)库中使用closure-compiler)

[英]WIKI: How to use Lime (how to use closure-compiler with 3rd party (closure) libraries)

The following post inspired me to have a look at limeJS, as a side project I'm working on and off an a Yatzee game (mostly off) and thought that might be a nice library to use. 下面这篇文章激励我看看limeJS,作为一个侧面项目,我正在研究Yatzee游戏(大部分是关闭),并认为这可能是一个很好的库。

As a beginner in google-closure I had some difficulties running uncompiled code and getting code compiled, mostly due to not knowing what the options and commands are and because of dependencies. 作为google-closure的初学者,我在运行未编译的代码和编译代码时遇到了一些困难,主要是因为不知道选项和命令是什么以及因为依赖性。

For other beginners with google-closuse I have written this tutorial. 对于其他使用google-closuse的初学者,我已经编写了本教程。

Note that jQuery can be used by your closure compiled code but you need the jQuery externs file . 请注意,闭包编译代码可以使用jQuery,但是您需要jQuery externs文件 You can't compile jQuery into your code, closure library has a dressed down dojo that can be found in third_party. 你不能将jQuery编译到你的代码中,封闭库有一个可以在third_party中找到的打扮的dojo。 When using that you can compile your code to one file. 使用它时,您可以将代码编译为一个文件。

What you need: 你需要什么:

  1. Python 蟒蛇
  2. Git client Git客户端
  3. Closure compiler for compiling code (minifying and merging all files into one) 用于编译代码的Closure编译器 (将所有文件缩小并合并为一个)
  4. Closure library like jQuery and jQuery ui but can be compiled along with your code 像jQuery和jQuery ui这样的Closure库 ,但可以和你的代码一起编译
  5. Python (I use 2.7) Python(我使用2.7)
  6. LimeJS As a 3rd party library (use git client to get it, the url: https://github.com/digitalfruit/limejs.git ) LimeJS作为第三方库(使用git客户端获取它,网址: https//github.com/digitalfruit/limejs.git
  7. JAVA JRE JAVA JRE

Directory structure 目录结构

I'm using Windows and have my projects in D:\\projects, if you have your projects somewhere else you have to replace D:\\projects references to your own. 我正在使用Windows并在D:\\ projects中拥有我的项目,如果你在其他地方有你的项目,你必须将D:\\ projects引用替换为你自己的项目。

In D:\\projects I make a directory called libraries in that directory I copy the goog and third_party directories from closure library (goog is under the closure directory) since you'll use them for many projects I keep it at projects root, you can include a copy of it in every project you create but in this case I'll share the library among all projects. 在D:\\ projects中,我在该目录中创建了一个名为libraries的目录,我从闭包库中复制了goog和third_party目录(goog在闭包目录下),因为你将它们用于许多项目,我将它保存在项目的根目录下,你可以在您创建的每个项目中包含它的副本,但在这种情况下,我将在所有项目中共享库。

Now I copy the contents of the src directory from limeJS ([lime clone dir]\\lime\\src) to D:\\projects\\libraries\\lime (the one containing the sub directories called animation, audio ...). 现在我将src目录的内容从limeJS([lime clone dir] \\ lime \\ src)复制到D:\\ projects \\ libraries \\ lime(包含名为animation,audio ...的子目录的目录)。

Next I'll use an existing project from the limeJS library, copy the [lime clone dir]\\lime\\demos\\roundball to D:\\projects\\roundball 接下来我将使用来自limeJS库的现有项目,将[lime clone dir] \\ lime \\ demos \\ roundball复制到D:\\ projects \\ roundball

At this time your directory structure should look like this: 此时您的目录结构应如下所示:

  • D: d:
    • projects 项目
      • libraries 图书馆
        • goog goog
        • lime 酸橙
          • animation 动画
          • audio 音频
          • css CSS
          • ... ...
        • third_party 第三方
          • closure 关闭
            • ... ...
      • roundball 圆球
        • assets 资产
        • ... other roundball stuff ......其他圆球的东西

cacldeps.py cacldeps.py

When you open D:\\projects\\roundball\\rb.html and check out the console (press F12 in Chrome or in FireFox preferably having firebug plugin installed) you'll see an error: "ReferenceError: goog is not defined" 当你打开D:\\ projects \\ roundball \\ rb.html并检查控制台(按Chrome中的F12或FireFox中最好安装了firebug插件)时,你会看到一个错误: “ReferenceError:goog未定义”

Open up D:\\projects\\roundball\\rb.html and change: 打开D:\\ projects \\ roundball \\ rb.html并更改:

<script type="text/javascript" src="../../../closure/closure/goog/base.js"></script>

to

<script type="text/javascript" src="../libraries/goog/base.js"></script>

Now when you open up rb.html again you get a different error: "goog.require could not find: lime.Director" 现在,当你再次打开rb.html时,你会得到一个不同的错误: “goog.require找不到:lime.Director”

This is because closure uses deps.js to find dependencies and since lime is not in there it can't find it. 这是因为闭包使用deps.js来查找依赖关系,因为lime不在那里它找不到它。 Lucky for us there is a tool called calcdeps.py in the bin directory of the closure library that can create this file for us. 幸运的是,在闭包库的bin目录中有一个名为calcdeps.py的工具,可以为我们创建这个文件。 It'll look in your code and and use goog.require to figure out what other files are needed. 它将查看您的代码并使用goog.require来确定需要哪些其他文件。 If your file structure is good than this tool will find it. 如果你的文件结构好,这个工具就会找到它。

It will expect that Director is in a file called director.js in a directory called lime (and it is). 它期望Director位于名为lime(和它)的目录中名为director.js的文件中。 That js file should have a goog.provide statement providing lime.Director. 那个js文件应该有一个提供lime.Director的goog.provide语句。

You can add directories for calcdeps.py to look in with the --path parameter. 您可以为calcdeps.py添加目录以查看--path参数。

In D:\\projects\\roundball I'll create a makedeps.bat with the following content: 在D:\\ projects \\ roundball中,我将使用以下内容创建一个makedeps.bat:

set calc="D:\software\closure compiler\library\closure\bin\calcdeps.py"
c:\Python27\python.exe %calc% ^
--path D:\projects\roundball\ ^
--path D:\projects\libraries\ ^
--input D:\projects\roundball\rb.js ^
--output_mode deps ^
--output_file D:\projects\libraries\goog\deps.js
pause

Uncompiled code uses /goog/deps.js to load dependencies. 未编译的代码使用/goog/deps.js来加载依赖项。 caclcdeps.py will check your code starting with rb.js (as this is the starting point of the code) and add entries to deps.js according to what your project uses. caclcdeps.py将检查以rb.js开头的代码(因为这是代码的起点),并根据项目使用的内容向deps.js添加条目。

Once again, cacldeps.py can be found in the bin directory of closure library. 再次,cacldeps.py可以在闭包库的bin目录中找到。

Note that when you have another project; 请注意,当您有另一个项目时; let's say D:\\projects\\project2 then you have to create a makedeps.bat in that project directory that re creates the deps.js for you before you can run the uncompiled code. 让我们说D:\\ projects \\ project2然后你必须在该项目目录中创建一个makedeps.bat,它可以为你重新创建deps.js,然后才能运行未编译的代码。 This because multiple projects share one google closure library so when you switch projects make sure you'll build the deps.js first before running your uncompiled code. 这是因为多个项目共享一个谷歌闭包库,因此当您切换项目时,请确保在运行未编译的代码之前首先构建deps.js。

Resolving missing dependency 解决遗漏的依赖关系

Opening the rt.html we still get an error but a different one: Error: "Undefined nameToPath for lime.css" 打开rt.html我们仍然会得到一个错误,但是另一个错误:错误: “未定义的nameToPath for lime.css”

Looking in goog/deps.js we can find that lime.css is needed by lime but doesn't have an entry in deps.js. 查看goog / deps.js,我们可以发现lime需要lime.css,但deps.js中没有条目。 Why is this? 为什么是这样? Looking in D:\\projects\\libraries\\lime there is no css directory or css.js file. 查看D:\\ projects \\ libraries \\ lime,没有css目录或css.js文件。 But in the directory where you cloned the git lime project there is a directory called css. 但是在克隆git lime项目的目录中有一个名为css的目录。

Copy the css directory to D:\\projects\\libraries\\lime and run makedeps.bat again, now when you open rt.html it will run. 将css目录复制到D:\\ projects \\ libraries \\ lime并再次运行makedeps.bat,现在打开rt.html它将运行。

The whole thing is uncompiled and you can set breakpoints to step into 3rd party code. 整个过程是未编译的,您可以设置断点以进入第三方代码。

Compile your code 编译你的代码

In production you would want to compile the code to one file. 在生产中,您需要将代码编译为一个文件。 To compile the code I created a compile.bat in D:\\projects\\roundball with the following content: 为了编译代码,我在D:\\ projects \\ roundball中创建了一个compile.bat,其中包含以下内容:

set calc="D:\software\closure compiler\library\closure\bin\calcdeps.py"
c:\Python27\python.exe %calc% ^
--path D:\projects\roundball\ ^
--path D:\projects\libraries\ ^
--input D:\projects\roundball\rb.js ^
--compiler_jar "D:\software\closure compiler\compiler.jar" ^
--output_mode compiled ^
--compiler_flags="--compilation_level=ADVANCED_OPTIMIZATIONS" ^
--compiler_flags="--formatting=PRETTY_PRINT" ^
--output_file D:\projects\roundball\compiled\roundball.js
pause

;Need this flag for production compile:
;--compiler_flags="--define goog.DEBUG=false" ^
;Remove the following flag from production compile:
;--compiler_flags="--formatting=PRETTY_PRINT" ^
;lime libraries is giving me errors with stricter compilation
;  so had to remove this flag (have to fix the errors or no type checking for your code)
;--compiler_flags="--warning_level=VERBOSE" ^

The file compiler.jar can be found where you downloaded (and extracted) closure compiler 可以在下载(和提取)闭包编译器的位置找到文件compiler.jar

Now when you run the compile.bat and open D:\\projects\\roundball\\compiled\\roundball.html you'll see in the developer tools of your browser that only roundball.js is loaded. 现在当你运行compile.bat并打开D:\\ projects \\ roundball \\ compiled \\ roundball.html时,你会在浏览器的开发者工具中看到只加载了roundball.js。

答案就在这个问题中,因为它是一篇可以帮助某人的文章(就像我以后忘记怎么做的时候一样)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM