Skip to content

Commit

Permalink
feat: add include_npm_sources to npm_package
Browse files Browse the repository at this point in the history
  • Loading branch information
gregmagolan committed May 28, 2024
1 parent 7951385 commit 9cf1f6b
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 45 deletions.
17 changes: 9 additions & 8 deletions docs/npm_package.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

56 changes: 20 additions & 36 deletions npm/private/npm_package.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -39,41 +39,21 @@ _NPM_PACKAGE_FILES_ATTRS = {
"include_sources": attr.bool(),
"include_transitive_types": attr.bool(),
"include_transitive_sources": attr.bool(),
"include_npm_sources": attr.bool(),
"srcs": attr.label_list(allow_files = True),
}

def _npm_package_files_impl(ctx):
files_depsets = []

if ctx.attr.include_transitive_sources:
# include all transitive sources (this includes direct sources)
files_depsets.extend([
target[JsInfo].transitive_sources
for target in ctx.attr.srcs
if JsInfo in target and hasattr(target[JsInfo], "transitive_sources")
])
elif ctx.attr.include_sources:
# include only direct sources
files_depsets.extend([
target[JsInfo].sources
for target in ctx.attr.srcs
if JsInfo in target and hasattr(target[JsInfo], "sources")
])

if ctx.attr.include_transitive_types:
# include all transitive types (this includes direct types)
files_depsets.extend([
target[JsInfo].transitive_types
for target in ctx.attr.srcs
if JsInfo in target and hasattr(target[JsInfo], "transitive_types")
])
elif ctx.attr.include_types:
# include only direct types
files_depsets.extend([
target[JsInfo].types
for target in ctx.attr.srcs
if JsInfo in target and hasattr(target[JsInfo], "types")
])
files_depsets.append(js_lib_helpers.gather_files_from_js_infos(
ctx.attr.srcs,
include_sources = ctx.attr.include_sources,
include_types = ctx.attr.include_types,
include_transitive_sources = ctx.attr.include_types,
include_transitive_types = ctx.attr.include_transitive_types,
include_npm_sources = ctx.attr.include_npm_sources,
))

if ctx.attr.include_runfiles:
# include default runfiles from srcs
Expand Down Expand Up @@ -171,6 +151,7 @@ def npm_package(
include_types = True,
include_transitive_sources = True,
include_transitive_types = True,
include_npm_sources = False,
include_runfiles = False,
hardlink = "auto",
publishable = False,
Expand Down Expand Up @@ -214,17 +195,17 @@ def npm_package(
`npm_package` makes use of `copy_to_directory`
(https://docs.aspect.build/rules/aspect_bazel_lib/docs/copy_to_directory) under the hood,
adopting its API and its copy action using composition. However, unlike `copy_to_directory`,
`npm_package` includes `transitive_sources` and `transitive_types` files from `JsInfo` providers in srcs
`npm_package` includes sources, types and npm package files from `JsInfo` providers in srcs
by default. The behavior of including sources and types from `JsInfo` can be configured
using the `include_sources`, `include_transitive_sources`, `include_types`, `include_transitive_types`
attributes.
and `include_npm_sources` attributes.
The two `include*_types` options may cause type-check actions to run, which slows down your
development round-trip.
You can pass the Bazel option `--@aspect_rules_js//npm:exclude_types_from_npm_packages`
to override these two attributes for an individual `bazel` invocation, avoiding the type-check.
`npm_package` also includes default runfiles from `srcs` by default which `copy_to_directory` does not. This behavior
`npm_package` can also include default runfiles from `srcs` by default which `copy_to_directory` does not. This behavior
can be configured with the `include_runfiles` attribute.
The default `include_srcs_packages`, `[".", "./**"]`, prevents files from outside of the target's
Expand Down Expand Up @@ -404,13 +385,15 @@ def npm_package(
since copies cannot be parallelized out as they are calculated. Instead all copy paths
must be calculated before any copies can be started.
include_sources: When True, `sources` from `JsInfo` providers in `data` targets are included in the list of available files to copy.
include_sources: When True, `sources` from `JsInfo` providers in `srcs` targets are included in the list of available files to copy.
include_types: When True, `types` from `JsInfo` providers in `srcs` targets are included in the list of available files to copy.
include_types: When True, `types` from `JsInfo` providers in `data` targets are included in the list of available files to copy.
include_transitive_sources: When True, `transitive_sources` from `JsInfo` providers in `srcs` targets are included in the list of available files to copy.
include_transitive_sources: When True, `transitive_sources` from `JsInfo` providers in `data` targets are included in the list of available files to copy.
include_transitive_types: When True, `transitive_types` from `JsInfo` providers in `srcs` targets are included in the list of available files to copy.
include_transitive_types: When True, `transitive_types` from `JsInfo` providers in `data` targets are included in the list of available files to copy.
include_npm_sources: When True, `npm_sources` from `JsInfo` providers in `srcs` targets are included in the list of available files to copy.
include_runfiles: When True, default runfiles from `srcs` targets are included in the list of available files to copy.
Expand Down Expand Up @@ -458,6 +441,7 @@ def npm_package(
Label("@aspect_rules_js//npm:exclude_types_from_npm_packages_flag"): False,
"//conditions:default": include_transitive_types,
}),
include_npm_sources = include_npm_sources,
include_runfiles = include_runfiles,
# Always tag the target manual since we should only build it when the final target is built.
tags = kwargs.get("tags", []) + ["manual"],
Expand Down
26 changes: 25 additions & 1 deletion npm/private/test/npm_package/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,31 @@ copy_to_directory(
)

diff_test(
name = "test",
name = "test_pkg",
file1 = ":pkg",
file2 = ":expected_pkg",
)

npm_package(
name = "pkg_with_node_modules",
srcs = [":lib_a"],
exclude_srcs_patterns = [],
include_npm_sources = True,
visibility = ["//visibility:public"],
)

copy_to_directory(
name = "expected_pkg_with_node_modules",
srcs = [
"index.js",
":node_modules/chalk",
":node_modules/chalk/dir",
":package.json",
],
)

diff_test(
name = "test_pkg_with_node_modules",
file1 = ":pkg",
file2 = ":expected_pkg",
)

0 comments on commit 9cf1f6b

Please sign in to comment.