Skip to content

Commit c060a22

Browse files
Alex Eaglealexeagle
Alex Eagle
authored andcommitted
fix(builtin): js_library supports --output_groups=types
If providing DeclarationInfo we should also give this way for filegroup/bazel CLI to select them
1 parent 75bf720 commit c060a22

File tree

1 file changed

+40
-25
lines changed

1 file changed

+40
-25
lines changed

internal/js_library/js_library.bzl

Lines changed: 40 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

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."
1916

2017
load(
2118
"//:providers.bzl",
@@ -215,10 +212,12 @@ def _impl(ctx):
215212
# Don't provide DeclarationInfo if there are no typings to provide.
216213
# Improves error messaging downstream if DeclarationInfo is required.
217214
if len(typings) or len(typings_depsets) > 1:
215+
decls = depset(transitive = typings_depsets)
218216
providers.append(declaration_info(
219-
declarations = depset(transitive = typings_depsets),
217+
declarations = decls,
220218
deps = ctx.attr.deps,
221219
))
220+
providers.append(OutputGroupInfo(types = decls))
222221

223222
return providers
224223

@@ -235,20 +234,14 @@ def js_library(
235234
**kwargs):
236235
"""Groups JavaScript code so that it can be depended on like an npm package.
237236
238-
### Behavior
239-
237+
`js_library` is intended to be used internally within Bazel, such as between two libraries in your monorepo.
240238
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.
242240
243241
> Compare this to `pkg_npm` which just produces a directory output, and therefore can't expose individual
244242
> 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.
252245
253246
`js_library` also copies any source files into the bazel-out folder.
254247
This is the same behavior as the `copy_to_bin` rule.
@@ -258,14 +251,9 @@ def js_library(
258251
rather than being forced to use Bazel's "Runfiles" semantics where any program might need a helper library
259252
to resolve files between the logical union of the source tree and the output tree.
260253
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
267255
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:
269257
270258
```python
271259
ts_project(
@@ -284,9 +272,36 @@ def js_library(
284272
)
285273
```
286274
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
290305
291306
Args:
292307
name: a name for the target

0 commit comments

Comments
 (0)