12
12
# See the License for the specific language governing permissions and
13
13
# limitations under the License.
14
14
15
- """js_library can be used to expose and share any library package.
16
-
17
- DO NOT USE - this is not fully designed yet and it is a work in progress.
18
- """
15
+ "js_library can be used to expose and share any library package."
19
16
20
17
load (
21
18
"//:providers.bzl" ,
@@ -215,10 +212,12 @@ def _impl(ctx):
215
212
# Don't provide DeclarationInfo if there are no typings to provide.
216
213
# Improves error messaging downstream if DeclarationInfo is required.
217
214
if len (typings ) or len (typings_depsets ) > 1 :
215
+ decls = depset (transitive = typings_depsets )
218
216
providers .append (declaration_info (
219
- declarations = depset ( transitive = typings_depsets ) ,
217
+ declarations = decls ,
220
218
deps = ctx .attr .deps ,
221
219
))
220
+ providers .append (OutputGroupInfo (types = decls ))
222
221
223
222
return providers
224
223
@@ -235,20 +234,14 @@ def js_library(
235
234
** kwargs ):
236
235
"""Groups JavaScript code so that it can be depended on like an npm package.
237
236
238
- ### Behavior
239
-
237
+ `js_library` is intended to be used internally within Bazel, such as between two libraries in your monorepo.
240
238
This rule doesn't perform any build steps ("actions") so it is similar to a `filegroup`.
241
- However it produces several Bazel "Providers" for interop with other rules.
239
+ However it provides several Bazel "Providers" for interop with other rules.
242
240
243
241
> Compare this to `pkg_npm` which just produces a directory output, and therefore can't expose individual
244
242
> files to downstream targets and causes a cascading re-build of all transitive dependencies when any file
245
- > changes
246
-
247
- These providers are:
248
- - DeclarationInfo so this target can be a dependency of a TypeScript rule
249
- - NpmPackageInfo so this target can interop with rules that expect third-party npm packages
250
- - LinkablePackageInfo for use with our "linker" that makes this package importable (similar to `npm link`)
251
- - JsModuleInfo so rules like bundlers can collect the transitive set of .js files
243
+ > changes. Also `pkg_npm` is intended to publish your code for external usage outside of Bazel, like
244
+ > by publishing to npm or artifactory, while `js_library` is for internal dependencies within your repo.
252
245
253
246
`js_library` also copies any source files into the bazel-out folder.
254
247
This is the same behavior as the `copy_to_bin` rule.
@@ -258,14 +251,9 @@ def js_library(
258
251
rather than being forced to use Bazel's "Runfiles" semantics where any program might need a helper library
259
252
to resolve files between the logical union of the source tree and the output tree.
260
253
261
- ### Usage
262
-
263
- `js_library` is intended to be used internally within Bazel, such as between two libraries in your monorepo.
264
-
265
- > Compare this to `pkg_npm` which is intended to publish your code for external usage outside of Bazel, like
266
- > by publishing to npm or artifactory.
254
+ ### Example
267
255
268
- The typical example usage of `js_library` is to expose some sources with a package name:
256
+ A typical example usage of `js_library` is to expose some sources with a package name:
269
257
270
258
```python
271
259
ts_project(
@@ -284,9 +272,36 @@ def js_library(
284
272
)
285
273
```
286
274
287
- To help work with "named AMD" modules as required by `ts_devserver` and other Google-style "concatjs" rules,
288
- `js_library` has some undocumented advanced features you can find in the source code or in our examples.
289
- These should not be considered a public API and aren't subject to our usual support and semver guarantees.
275
+ > To help work with "named AMD" modules as required by `ts_devserver` and other Google-style "concatjs" rules,
276
+ > `js_library` has some undocumented advanced features you can find in the source code or in our examples.
277
+ > These should not be considered a public API and aren't subject to our usual support and semver guarantees.
278
+
279
+ ### Outputs
280
+
281
+ Like all Bazel rules it produces a default output by providing [DefaultInfo].
282
+ You'll get these outputs if you include this in the `srcs` of a typical rule like `filegroup`,
283
+ and these will be the printed result when you `bazel build //some:js_library_target`.
284
+ The default outputs are all of:
285
+ - [DefaultInfo] produced by targets in `deps`
286
+ - A copy of all sources (InputArtifacts from your source tree) in the bazel-out directory
287
+
288
+ When there are TypeScript typings files, `js_library` provides [DeclarationInfo](#declarationinfo)
289
+ so this target can be a dependency of a TypeScript rule. This includes any `.d.ts` files in `srcs` as well
290
+ as transitive ones from `deps`.
291
+ It will also provide [OutputGroupInfo] with a "types" field, so you can select the typings outputs with
292
+ `bazel build //some:js_library_target --output_groups=types` or with a `filegroup` rule using the
293
+ [output_group](https://docs.bazel.build/versions/master/be/general.html#filegroup.output_group) attribute.
294
+
295
+ In order to work with the linker (similar to `npm link` for first-party monorepo deps), `js_library` provides
296
+ [LinkablePackageInfo](#linkablepackageinfo) for use with our "linker" that makes this package importable.
297
+
298
+ It also provides:
299
+ - [NpmPackageInfo](#npmpackageinfo) to interop with rules that expect third-party npm packages.
300
+ - [JsModuleInfo](#jsmoduleinfo) so rules like bundlers can collect the transitive set of .js files
301
+ - [JsNamedModuleInfo](#jsnamedmoduleinfo) for rules that expect named AMD or `goog.module` format JS
302
+
303
+ [OutputGroupInfo]: https://docs.bazel.build/versions/master/skylark/lib/OutputGroupInfo.html
304
+ [DefaultInfo]: https://docs.bazel.build/versions/master/skylark/lib/DefaultInfo.html
290
305
291
306
Args:
292
307
name: a name for the target
0 commit comments