Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Debugging Uncompiled Source Code

Kelvin Jin edited this pageFeb 8, 2021 ·5 revisions

When you need to debug code that uses Closure Compiler and Closure Library, considerdebugging uncompiled source code. This is useful because names are not obfuscated, source code line numbers are intact, so it is easy to interpret error messages or stack traces and use the browser's debugger.However, some people find thatdebugging compiled code works quite well.

FromClosure: The Definitive Guide by Michael Bolin, Chapter 1:

Code Must Work Without Compilation Although the Compiler provides many beneficial transformations to its input, the code for the Closure Library is also expected to be able to be run without being processed by the Compiler. This not only ensures that the input language is pure JavaScript, but also makes debugging easier, as it is always possible to use the deobfuscated code.

Running from uncompiled source code requires a different way of loading theClosure Library. The HTML file that loads and runs the application is differentin the uncompiled case (typically running directly from the source directory)versus the compiled case (running from the build directory).

The HTML file in the source directory should be set up for the uncompiled case. Here is an example:

<scriptsrc="../../../../javascript/closure-library/closure/goog/base.js"></script><scriptsrc="../../../build/deps.js"></script><script>goog.define('goog.LOCALE','de');goog.define('goog.DEBUG',true);goog.require('myProject.myPackage.myApp');</script><script>myApp=newmyProject.myPackage.myApp();myApp.start();</script>

The HTML file calls the Closure Library filebase.js to define the basic functions needed by Closure Library such asgoog.provide andgoog.require. Nexta dependency filedeps.js is loaded that tells for each class what it's requiredclasses are. Then the class of interest is loaded with agoog.require statement. Once all that has happened, you can start to use the classes and functions defined in your project.

The location ofclosure-library on your system is probably different than shown in the above example, so you would need to edit accordingly.

Thedeps.js file is created using thedepswriter.py tool. Thedeps.js file is not needed for compiled code or for the compiler. To updatedeps.js

python ../javascript/closure-library/closure/bin/build/depswriter.py \--root_with_prefix="src ../../../../myProject/src"> deps.js

Note that theroot_with_prefix has the name of themyProject source directory,and the path back to the source directory fromclosure-library/closure/bin.

Note: Thedeps.js file in Closure Library itself is implicitly loaded as well.In recent updates (Jan 2021 and newer) to the Closure Library GitHub repository,this file must be updated withnpm run gen_deps_js (ornpm install if runningfor the first time). These commands should be run from the closure-library rootdirectory, for example:

cd ~/src/closure-librarynpm run gen_deps_js # or `npm install` if it has not previously been run in this dir

That command should generate the fileclosure-library/closure/goog/deps.js.

Settinggoog.LOCALE (seen in the script above) is optional, if you leave that out thenthe defaultLOCALE will be used. Similarly withgoog.DEBUG.

In contrast, forcompiled code, the HTML file simply runs the compiled script which has boiledtogether everything needed for the application into a single script:

<scriptsrc="myProject.js"></script>

When running from source code, it's always a good idea to compile first, even if you are not running compiled code. The compiler has excellent checks that catch lots of errors.

Clone this wiki locally

[8]ページ先頭

©2009-2025 Movatter.jp