# rules_closure **Repository Path**: mirrors_bazelbuild/rules_closure ## Basic Information - **Project Name**: rules_closure - **Description**: Closure rules for Bazel - **Primary Language**: Unknown - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2020-08-08 - **Last Updated**: 2025-09-27 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Closure Rules for Bazel (αlpha) [![Bazel CI build status](https://badge.buildkite.com/7569410e2a2661076591897283051b6d137f35102167253fed.svg)](https://buildkite.com/bazel/closure-compiler-rules-closure-postsubmit) JavaScript | Stylesheets | Miscellaneous --- | --- | --- [closure_js_library] | [closure_css_library] | [phantomjs_test] [closure_js_binary] | [closure_css_binary] | | [closure_js_test] | | ## Overview Closure Rules provides a polished JavaScript build system for [Bazel] that emphasizes type safety, strictness, testability, and optimization. These rules are built with the [Closure Tools], which are what Google used to create websites like Google.com and Gmail. The goal of this project is to take the frontend development methodology that Google actually uses internally, and make it easily available to outside developers. Closure Rules is an *abstract* build system. This is what sets it apart from Grunt, Gulp, Webpacker, Brunch, Broccoli, etc. These projects all provide a concrete framework for explaining *how* to build your project. Closure Rules instead provides a framework for declaring *what* your project is. Closure Rules is then able to use this abstract definition to infer an optimal build strategy. Closure Rules is also an *austere* build system. The Closure Compiler doesn't play games. It enforces a type system that can be stricter than Java. From a stylistic perspective, Closure is [verbose] like Java; there's no cryptic symbols or implicit behavior; the code says exactly what it's doing. This sets Closure apart from traditional JavaScript development, where terseness was favored over readability, because minifiers weren't very good. ### What's Included Closure Rules bundles the following tools and makes them "just work." - [Bazel]: The build system Google uses to manage a repository with petabytes of code. - [Closure Compiler]: Type-safe, null-safe, optimizing JavaScript compiler that transpiles [ECMASCRIPT6] to minified ES3 JavaScript that can run in any browser. - [Closure Library]: Google's core JavaScript libraries. JavaScript and Java. This is one of the most secure templating systems available. It's where Google has put the most thought into preventing things like XSS attacks. It also supports i18n and l10n. - [Closure Stylesheets]: CSS compiler supporting class name minification, variables, functions, conditionals, mixins, and bidirectional layout. - [PhantomJS]: Headless web browser used for automating JavaScript unit tests in a command line environment. ### Mailing Lists - [closure-rules-announce](https://groups.google.com/forum/#!forum/closure-rules-announce) - [closure-rules-discuss](https://groups.google.com/forum/#!forum/closure-rules-discuss) ### Caveat Emptor Closure Rules is production ready, but its design is not yet finalized. Breaking changes will be introduced. However they will be well-documented in the release notes. ## Setup First you must [install Bazel]. Then you add the following to your MODULE.bazel file: ```bzl bazel_dep(name = "rules_closure", version = "0.15.0") ``` The root module has to declare the same override for rules_webtesting, rules_scala, and google_bazel_common temporarily until they are registered in BCR. You are not required to install the Closure Tools, PhantomJS, or anything else for that matter; they will be fetched automatically by Bazel. ## Examples Please see the test directories within this project for concrete examples of usage: - [//closure/testing/test](https://github.com/bazelbuild/rules_closure/tree/master/closure/testing/test) - [//closure/compiler/test](https://github.com/bazelbuild/rules_closure/tree/master/closure/compiler/test) - [//closure/stylesheets/test](https://github.com/bazelbuild/rules_closure/tree/master/closure/stylesheets/test) # Reference ## closure\_js\_library ```starlark load("@rules_closure//closure:defs.bzl", "closure_js_library") closure_js_library(name, srcs, data, deps, exports, suppress, convention, no_closure_library) ``` Defines a set of JavaScript sources. The purpose of this rule is to define an abstract graph of JavaScript sources. It must be used in conjunction with [closure_js_binary] to output a minified file. This rule will perform syntax checking and linting on your files. This can be tuned with the `suppress` attribute. To learn more about what the linter wants, read the [Google JavaScript Style Guide]. Strict dependency checking is performed on the sources listed in each library target. See the documentation of the `deps` attribute for further information. #### Rule Polymorphism This rule can be referenced as though it were the following: - [filegroup]: `srcs` will always be empty and `data` will contain all transitive JS sources and data. ### Arguments - **name:** ([Name]; required) A unique name for this rule. The standard convention is that this be the same name as the Bazel package with `srcs = glob(['*.js'])`. If it contains a subset of the `.js` srcs in the package, then convention states that the `_lib` suffix should be used. - **srcs:** (List of [labels]; optional) The list of `.js` source files that represent this library. This can include files marked as `@externs` or `@nocompile`. This attribute is required unless the `exports` attribute is being defined. - **data:** (List of [labels]; optional) Runfiles directly referenced by JS sources in this rule. For example, if the JS generated injected an img tag into the page with a hard coded image named foo.png, then you might want to list that image here, so it ends up in the webserver runfiles. - **deps:** (List of [labels]; optional) Direct [dependency] list. These can point to [closure_js_library], [closure_css_library] rules. - **exports:** (List of [labels]; optional) Listing dependencies here will cause them to become *direct* dependencies in parent rules. This functions similarly to [java_library.exports]. This can be used to create aliases for rules in another package. It can also be also be used to export private targets within the package. Another use is to roll up a bunch of fine-grained libraries into a single big one. - **suppress** (List of String; optional; default is `[]`) List of codes the linter should ignore. Warning and error messages that are allowed to be suppressed, will display the codes for disabling it. For example, if the linter says: ``` foo.js:123: WARNING lintChecks JSC_MUST_BE_PRIVATE - Property bar_ must be marked @private ``` Then the diagnostic code `"JSC_MUST_BE_PRIVATE"` can be used in the `suppress` list. It is also possible to use the group code `"lintChecks"` to disable all diagnostic codes associated with linting. If a code is used that isn't necessary, an error is raised. Therefore the use of fine-grained suppression codes is maintainable. - **convention** (String; optional; default is `"CLOSURE"`) Specifies the coding convention which affects how the linter operates. This can be the following values: - `NONE`: Don't take any special practices into consideration. - `CLOSURE`: Take [Closure coding conventions] into consideration when linting. See the [Google JavaScript Style Guide] for more information. - `GOOGLE`: Take [Google coding conventions] into consideration when linting. See the [Google JavaScript Style Guide] for more information. - **no_closure_library** (Boolean; optional; default is `False`) Do not link Closure Library [base.js]. If this flag is used, an error will be raised if any `deps` do not also specify this flag. All [closure_js_library] rules with nonempty `srcs` have an implicit dependency on `@closure_library//:closure/goog/base.js`. This is a lightweight file that boostraps very important functions, e.g. `goog.provide`. Linking this file by default is important because: 1. It is logically impossible to say `goog.require('goog')`. 2. The Closure Compiler will sometimes generate synthetic code that calls these functions. For example, the [ProcessEs6Modules] compiler pass turns ES6 module directives into `goog.provide` / `goog.require` statements. The only tradeoff is that when compiling in `WHITESPACE_ONLY` mode, this code will show up in the resulting binary. Therefore this flag provides the option to remove it. ## closure\_js\_binary ```starlark load("@rules_closure//closure:defs.bzl", "closure_js_binary") closure_js_binary(name, deps, css, debug, language, entry_points, dependency_mode, compilation_level, formatting, output_wrapper, property_renaming_report, defs) ``` Turns JavaScript libraries into a minified optimized blob of code. This rule must be used in conjunction with [closure_js_library]. #### Implicit Output Targets - *name*.js: A minified JavaScript file containing all transitive sources. - *name*.js.map: Sourcemap file mapping compiled sources to their raw sources. This file can be loaded into browsers such as Chrome and Firefox to view a stacktrace when an error is thrown by compiled sources. #### Rule Polymorphism This rule can be referenced as though it were the following: - [filegroup]: `srcs` will be the .js and .js.map output files and `data` will contain those files in addition to all transitive JS sources and data. - [closure_js_library]: `srcs` will be the .js output file, `language` will be the output language, `deps` will be empty, `data` will contain all transitive data, and `no_closure_library` will be `True`. ### Arguments - **name:** ([Name]; required) A unique name for this rule. Convention states that such rules be named `foo_bin` or `foo_dbg` if `debug = True`. - **deps:** (List of [labels]; required) Direct dependency list. This attribute has the same meaning as it does in [closure_js_library]. These can point to [closure_js_library] rules. - **css:** (Label; optional) CSS class renaming target, which must point to a [closure_css_binary] rule. This causes the CSS name mapping file generated by the CSS compiler to be included in the compiled JavaScript. This tells Closure Compiler how to minify CSS class names. This attribute is required if any of JavaScript sources depend on a [closure_css_library]. This rule will check that all the referenced CSS libraries are present in the CSS binary. - **debug:** (Boolean; optional; default is `False`) Enables debug mode. Many types of properties and variable names will be renamed to include `$` characters, to help you spot bugs when using `ADVANCED` compilation mode. Assert statements will not be stripped. Dependency directives will be removed. - **language:** (String; optional; default is `"ECMASCRIPT3"`) Output language variant to which library sources are transpiled. The default is ES3 because it works in all browsers. The input language is calculated automatically based on the `language` attribute of [closure_js_library] dependencies. - **entry_points:** (List of String; optional; default is `[]`) List of unreferenced namespaces that should *not* be pruned by the compiler. This should only be necessary when you want to invoke them from a `