Google Closure Library

repository·master·Indexed 26 days ago

https://github.com/google/closure-library

A collection of JavaScript utilities and components. The library includes the google-closure-deps package for managing dependency graphs and parsing Closure files (including ES6 modules), as well as CLI tools like closure-make-deps and get-js-version. It provides APIs for IndexedDB lookups (goog.db.Index), filesystem operations (goog.fs.Entry), and Node.js bootstrapping. Note: This project has been archived and is no longer actively maintained for modern JavaScript development.

Tokens
5.8K
Snippets
14
Records
43
Agent score
89%

What's inside google-closure-library

  1. Avoid global declarations and loose types

    master
    To prevent global scope pollution, global functions and var declarations are not allowed. Use goog.provide or goog.module for top-level namespaces. Additionally, use loose types like ? (unknown), * (all), Object, and Function sparingly to maintain type safety. Using ? as a this type is strictly forbidden.
  2. Set up an HTML file for uncompiled development

    master

    To run your application in a development environment using the Closure debug loader, your HTML file must follow this loading order:

    1. Load base.js from the Closure library (defines goog.require).
    2. Load your generated deps.js (provides dependency mapping).
    3. Call goog.require('your_module_name') to trigger the evaluation of your code.
    <!DOCTYPE html>
    <meta charset="UTF-8">
    <script src="node_modules/google-closure-library/closure/goog/base.js"></script>
    <script src="deps.js"></script>
    <div>Content to be rendered below</div>
    <script>
      goog.require('hello');
    </script>
  3. Generate a deps.js file using closure-make-deps

    master

    Use the closure-make-deps tool to create a deps.js file. This file maps the dependency tree for uncompiled code, allowing the Closure debug loader to find necessary files.

    When running the command:

    • Use -f <file> to include your source files.
    • Include the Closure library's own deps.js file to help the tool resolve namespaces like goog.dom.
    • Use --closure-path to point to the library's goog directory.
    • For larger projects, consider using --root to include directories and --exclude to skip node_modules/.
    $(npm bin)/closure-make-deps \
      -f hello.js \
      -f node_modules/google-closure-library/closure/goog/deps.js \
      --closure-path node_modules/google-closure-library/closure/goog \
      > deps.js
  4. Compile your application with Closure Compiler

    master

    For production, use google-closure-compiler to create a single-file, optimized bundle. This process performs type checking, dead code removal, and identifier shortening.

    1. Install the compiler: npm install --save-dev google-closure-compiler.
    2. Run the compiler with the following flags:
      • --js: Your source file.
      • --js: The Closure library source files (e.g., node_modules/google-closure-library/**/*.js).
      • --dependency_mode=PRUNE: Tells the compiler to only include dependencies required by the entry point.
      • --entry_point=goog:<module_name>: The namespace of your entry point.
      • --js_output_file: The name of the resulting bundle.
    3. In your production HTML, replace the base.js and deps.js scripts with a single <script> tag pointing to your compiled file.
    # 1. Install
    npm install --save-dev google-closure-compiler
    
    # 2. Compile
    $(npm bin)/google-closure-compiler \
      --js hello.js \
      --js node_modules/google-closure-library/**/*.js \
      --dependency_mode=PRUNE \
      --entry_point=goog:hello \
      --js_output_file hello_compiled.js
  5. Create typed elements using goog.dom

    master
    To ensure conformance rules can correctly identify dangerous properties (like script.src), avoid using document.createElement('script') which returns a generic Element. Instead, use goog.dom.createElement or goog.dom.createDom with goog.dom.TagName to ensure the returned type is specific (e.g., HTMLScriptElement).
  6. Use safe wrappers for Location.href and Window.location

    master
    Direct assignment to Location.prototype.href or Window.prototype.location is banned. Use goog.dom.safe.setLocationHref. If you pass a string, it is sanitized; if you pass a goog.html.SafeUrl, it is assigned without further sanitization. Note that reading these properties is permitted.