Skip to content

Commit

Permalink
feat: support --stamp env vars in npm_package_bin
Browse files Browse the repository at this point in the history
  • Loading branch information
gregmagolan committed Dec 22, 2021

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 90c7fe0 commit dc7b5ad
Showing 25 changed files with 229 additions and 201 deletions.
62 changes: 36 additions & 26 deletions docs/Built-ins.md
Original file line number Diff line number Diff line change
@@ -1176,8 +1176,8 @@ Defaults to `3600`
**USAGE**

<pre>
pkg_npm(<a href="#pkg_npm-name">name</a>, <a href="#pkg_npm-deps">deps</a>, <a href="#pkg_npm-nested_packages">nested_packages</a>, <a href="#pkg_npm-node_context_data">node_context_data</a>, <a href="#pkg_npm-package_name">package_name</a>, <a href="#pkg_npm-package_path">package_path</a>, <a href="#pkg_npm-srcs">srcs</a>,
<a href="#pkg_npm-substitutions">substitutions</a>, <a href="#pkg_npm-tgz">tgz</a>, <a href="#pkg_npm-validate">validate</a>, <a href="#pkg_npm-vendor_external">vendor_external</a>)
pkg_npm(<a href="#pkg_npm-name">name</a>, <a href="#pkg_npm-deps">deps</a>, <a href="#pkg_npm-nested_packages">nested_packages</a>, <a href="#pkg_npm-package_name">package_name</a>, <a href="#pkg_npm-package_path">package_path</a>, <a href="#pkg_npm-srcs">srcs</a>, <a href="#pkg_npm-stamp">stamp</a>, <a href="#pkg_npm-substitutions">substitutions</a>, <a href="#pkg_npm-tgz">tgz</a>,
<a href="#pkg_npm-validate">validate</a>, <a href="#pkg_npm-vendor_external">vendor_external</a>)
</pre>

The pkg_npm rule creates a directory containing a publishable npm artifact.
@@ -1284,18 +1284,6 @@ Defaults to `[]`

Defaults to `[]`

<h4 id="pkg_npm-node_context_data">node_context_data</h4>

(*<a href="https://bazel.build/docs/build-ref.html#labels">Label</a>*): Provides info about the build context, such as stamping.

By default it reads from the bazel command line, such as the `--stamp` argument.
Use this to override values for this target, such as enabling or disabling stamping.
You can use the `node_context_data` rule in `@build_bazel_rules_nodejs//internal/node:context.bzl`
to create a NodeContextInfo. The dependencies of this attribute must provide: NodeContextInfo


Defaults to `@build_bazel_rules_nodejs//internal:node_context_data`

<h4 id="pkg_npm-package_name">package_name</h4>

(*String*): The package name that the linker will link this npm package as.
@@ -1320,6 +1308,23 @@ Defaults to `""`

Defaults to `[]`

<h4 id="pkg_npm-stamp">stamp</h4>

(*<a href="https://bazel.build/docs/build-ref.html#labels">Label</a>*): Whether to encode build information into the output. Possible values:
- `@build_bazel_rules_nodejs//nodejs:always_stamp`:
Always stamp the build information into the output, even in [--nostamp][stamp] builds.
This setting should be avoided, since it potentially causes cache misses remote caching for
any downstream actions that depend on it.
- `@build_bazel_rules_nodejs//nodejs:never_stamp`:
Always replace build information by constant values. This gives good build result caching.
- `@build_bazel_rules_nodejs//nodejs:use_stamp_flag`:
Embedding of build information is controlled by the [--[no]stamp][stamp] flag.
Stamped binaries are not rebuilt unless their dependencies change.
[stamp]: https://docs.bazel.build/versions/main/user-manual.html#flag--stamp The dependencies of this attribute must provide: StampSettingInfo


Defaults to `@build_bazel_rules_nodejs//nodejs:use_stamp_flag`

<h4 id="pkg_npm-substitutions">substitutions</h4>

(*<a href="https://bazel.build/docs/skylark/lib/dict.html">Dictionary: String -> String</a>*): Key-value pairs which are replaced in all the files while building the package.
@@ -1358,7 +1363,7 @@ Defaults to `[]`
**USAGE**

<pre>
pkg_web(<a href="#pkg_web-name">name</a>, <a href="#pkg_web-additional_root_paths">additional_root_paths</a>, <a href="#pkg_web-node_context_data">node_context_data</a>, <a href="#pkg_web-srcs">srcs</a>, <a href="#pkg_web-substitutions">substitutions</a>)
pkg_web(<a href="#pkg_web-name">name</a>, <a href="#pkg_web-additional_root_paths">additional_root_paths</a>, <a href="#pkg_web-srcs">srcs</a>, <a href="#pkg_web-stamp">stamp</a>, <a href="#pkg_web-substitutions">substitutions</a>)
</pre>

Assembles a web application from source files.
@@ -1377,23 +1382,28 @@ Assembles a web application from source files.

Defaults to `[]`

<h4 id="pkg_web-node_context_data">node_context_data</h4>
<h4 id="pkg_web-srcs">srcs</h4>

(*<a href="https://bazel.build/docs/build-ref.html#labels">Label</a>*): Provides info about the build context, such as stamping.

By default it reads from the bazel command line, such as the `--stamp` argument.
Use this to override values for this target, such as enabling or disabling stamping.
You can use the `node_context_data` rule in `@build_bazel_rules_nodejs//internal/node:context.bzl`
to create a NodeContextInfo. The dependencies of this attribute must provide: NodeContextInfo
(*<a href="https://bazel.build/docs/build-ref.html#labels">List of labels</a>*): Files which should be copied into the package

Defaults to `[]`

Defaults to `@build_bazel_rules_nodejs//internal:node_context_data`
<h4 id="pkg_web-stamp">stamp</h4>

<h4 id="pkg_web-srcs">srcs</h4>
(*<a href="https://bazel.build/docs/build-ref.html#labels">Label</a>*): Whether to encode build information into the output. Possible values:
- `@build_bazel_rules_nodejs//nodejs:always_stamp`:
Always stamp the build information into the output, even in [--nostamp][stamp] builds.
This setting should be avoided, since it potentially causes cache misses remote caching for
any downstream actions that depend on it.
- `@build_bazel_rules_nodejs//nodejs:never_stamp`:
Always replace build information by constant values. This gives good build result caching.
- `@build_bazel_rules_nodejs//nodejs:use_stamp_flag`:
Embedding of build information is controlled by the [--[no]stamp][stamp] flag.
Stamped binaries are not rebuilt unless their dependencies change.
[stamp]: https://docs.bazel.build/versions/main/user-manual.html#flag--stamp The dependencies of this attribute must provide: StampSettingInfo

(*<a href="https://bazel.build/docs/build-ref.html#labels">List of labels</a>*): Files which should be copied into the package

Defaults to `[]`
Defaults to `@build_bazel_rules_nodejs//nodejs:use_stamp_flag`

<h4 id="pkg_web-substitutions">substitutions</h4>

34 changes: 17 additions & 17 deletions docs/Providers.md
Original file line number Diff line number Diff line change
@@ -204,23 +204,6 @@ or a source file path such as,
`external/<external_wksp>/path/to/package`


## NodeContextInfo

**USAGE**

<pre>
NodeContextInfo(<a href="#NodeContextInfo-stamp">stamp</a>)
</pre>

Provides data about the build context, like config_setting's

**FIELDS**

<h4 id="NodeContextInfo-stamp">stamp</h4>

If stamping is enabled for this build


## NodeRuntimeDepsInfo

**USAGE**
@@ -284,6 +267,23 @@ Provides information about one or more external npm packages
The workspace name that these external npm package(s) are provided from


## StampSettingInfo

**USAGE**

<pre>
StampSettingInfo(<a href="#StampSettingInfo-value">value</a>)
</pre>



**FIELDS**

<h4 id="StampSettingInfo-value">value</h4>

Whether stamping is enabled


## declaration_info

**USAGE**
31 changes: 18 additions & 13 deletions docs/Rollup.md
Original file line number Diff line number Diff line change
@@ -198,7 +198,7 @@ rollup_bundle(

<pre>
rollup_bundle(<a href="#rollup_bundle-name">name</a>, <a href="#rollup_bundle-args">args</a>, <a href="#rollup_bundle-config_file">config_file</a>, <a href="#rollup_bundle-deps">deps</a>, <a href="#rollup_bundle-entry_point">entry_point</a>, <a href="#rollup_bundle-entry_points">entry_points</a>, <a href="#rollup_bundle-format">format</a>, <a href="#rollup_bundle-link_workspace_root">link_workspace_root</a>,
<a href="#rollup_bundle-node_context_data">node_context_data</a>, <a href="#rollup_bundle-output_dir">output_dir</a>, <a href="#rollup_bundle-rollup_bin">rollup_bin</a>, <a href="#rollup_bundle-rollup_worker_bin">rollup_worker_bin</a>, <a href="#rollup_bundle-silent">silent</a>, <a href="#rollup_bundle-sourcemap">sourcemap</a>, <a href="#rollup_bundle-srcs">srcs</a>,
<a href="#rollup_bundle-output_dir">output_dir</a>, <a href="#rollup_bundle-rollup_bin">rollup_bin</a>, <a href="#rollup_bundle-rollup_worker_bin">rollup_worker_bin</a>, <a href="#rollup_bundle-silent">silent</a>, <a href="#rollup_bundle-sourcemap">sourcemap</a>, <a href="#rollup_bundle-srcs">srcs</a>, <a href="#rollup_bundle-stamp">stamp</a>,
<a href="#rollup_bundle-supports_workers">supports_workers</a>)
</pre>

@@ -319,18 +319,6 @@ If source files need to be required then they can be copied to the bin_dir with

Defaults to `False`

<h4 id="rollup_bundle-node_context_data">node_context_data</h4>

(*<a href="https://bazel.build/docs/build-ref.html#labels">Label</a>*): Provides info about the build context, such as stamping.

By default it reads from the bazel command line, such as the `--stamp` argument.
Use this to override values for this target, such as enabling or disabling stamping.
You can use the `node_context_data` rule in `@build_bazel_rules_nodejs//internal/node:context.bzl`
to create a NodeContextInfo. The dependencies of this attribute must provide: NodeContextInfo


Defaults to `@build_bazel_rules_nodejs//internal:node_context_data`

<h4 id="rollup_bundle-output_dir">output_dir</h4>

(*Boolean*): Whether to produce a directory output.
@@ -381,6 +369,23 @@ You must not repeat file(s) passed to entry_point/entry_points.

Defaults to `[]`

<h4 id="rollup_bundle-stamp">stamp</h4>

(*<a href="https://bazel.build/docs/build-ref.html#labels">Label</a>*): Whether to encode build information into the output. Possible values:
- `@build_bazel_rules_nodejs//nodejs:always_stamp`:
Always stamp the build information into the output, even in [--nostamp][stamp] builds.
This setting should be avoided, since it potentially causes cache misses remote caching for
any downstream actions that depend on it.
- `@build_bazel_rules_nodejs//nodejs:never_stamp`:
Always replace build information by constant values. This gives good build result caching.
- `@build_bazel_rules_nodejs//nodejs:use_stamp_flag`:
Embedding of build information is controlled by the [--[no]stamp][stamp] flag.
Stamped binaries are not rebuilt unless their dependencies change.
[stamp]: https://docs.bazel.build/versions/main/user-manual.html#flag--stamp The dependencies of this attribute must provide: StampSettingInfo


Defaults to `@build_bazel_rules_nodejs//nodejs:use_stamp_flag`

<h4 id="rollup_bundle-supports_workers">supports_workers</h4>

(*Boolean*): Experimental! Use only with caution.
35 changes: 20 additions & 15 deletions docs/esbuild.md
Original file line number Diff line number Diff line change
@@ -103,9 +103,9 @@ This will create an output directory containing all the code split chunks, along

<pre>
esbuild(<a href="#esbuild-name">name</a>, <a href="#esbuild-args">args</a>, <a href="#esbuild-args_json">args_json</a>, <a href="#esbuild-config">config</a>, <a href="#esbuild-define">define</a>, <a href="#esbuild-define_settings">define_settings</a>, <a href="#esbuild-deps">deps</a>, <a href="#esbuild-entry_point">entry_point</a>, <a href="#esbuild-entry_points">entry_points</a>,
<a href="#esbuild-external">external</a>, <a href="#esbuild-format">format</a>, <a href="#esbuild-launcher">launcher</a>, <a href="#esbuild-link_workspace_root">link_workspace_root</a>, <a href="#esbuild-max_threads">max_threads</a>, <a href="#esbuild-metafile">metafile</a>, <a href="#esbuild-minify">minify</a>,
<a href="#esbuild-node_context_data">node_context_data</a>, <a href="#esbuild-output">output</a>, <a href="#esbuild-output_css">output_css</a>, <a href="#esbuild-output_dir">output_dir</a>, <a href="#esbuild-output_map">output_map</a>, <a href="#esbuild-platform">platform</a>, <a href="#esbuild-sourcemap">sourcemap</a>,
<a href="#esbuild-sources_content">sources_content</a>, <a href="#esbuild-splitting">splitting</a>, <a href="#esbuild-srcs">srcs</a>, <a href="#esbuild-target">target</a>)
<a href="#esbuild-external">external</a>, <a href="#esbuild-format">format</a>, <a href="#esbuild-launcher">launcher</a>, <a href="#esbuild-link_workspace_root">link_workspace_root</a>, <a href="#esbuild-max_threads">max_threads</a>, <a href="#esbuild-metafile">metafile</a>, <a href="#esbuild-minify">minify</a>, <a href="#esbuild-output">output</a>,
<a href="#esbuild-output_css">output_css</a>, <a href="#esbuild-output_dir">output_dir</a>, <a href="#esbuild-output_map">output_map</a>, <a href="#esbuild-platform">platform</a>, <a href="#esbuild-sourcemap">sourcemap</a>, <a href="#esbuild-sources_content">sources_content</a>, <a href="#esbuild-splitting">splitting</a>, <a href="#esbuild-srcs">srcs</a>,
<a href="#esbuild-stamp">stamp</a>, <a href="#esbuild-target">target</a>)
</pre>

Runs the esbuild bundler under Bazel
@@ -263,18 +263,6 @@ See https://esbuild.github.io/api/#minify for more details

Defaults to `False`

<h4 id="esbuild-node_context_data">node_context_data</h4>

(*<a href="https://bazel.build/docs/build-ref.html#labels">Label</a>*): Provides info about the build context, such as stamping.

By default it reads from the bazel command line, such as the `--stamp` argument.
Use this to override values for this target, such as enabling or disabling stamping.
You can use the `node_context_data` rule in `@build_bazel_rules_nodejs//internal/node:context.bzl`
to create a NodeContextInfo. The dependencies of this attribute must provide: NodeContextInfo


Defaults to `@build_bazel_rules_nodejs//internal:node_context_data`

<h4 id="esbuild-output">output</h4>

(*<a href="https://bazel.build/docs/build-ref.html#labels">Label</a>*): Name of the output file when bundling
@@ -338,6 +326,23 @@ Defaults to `False`

Defaults to `[]`

<h4 id="esbuild-stamp">stamp</h4>

(*<a href="https://bazel.build/docs/build-ref.html#labels">Label</a>*): Whether to encode build information into the output. Possible values:
- `@build_bazel_rules_nodejs//nodejs:always_stamp`:
Always stamp the build information into the output, even in [--nostamp][stamp] builds.
This setting should be avoided, since it potentially causes cache misses remote caching for
any downstream actions that depend on it.
- `@build_bazel_rules_nodejs//nodejs:never_stamp`:
Always replace build information by constant values. This gives good build result caching.
- `@build_bazel_rules_nodejs//nodejs:use_stamp_flag`:
Embedding of build information is controlled by the [--[no]stamp][stamp] flag.
Stamped binaries are not rebuilt unless their dependencies change.
[stamp]: https://docs.bazel.build/versions/main/user-manual.html#flag--stamp The dependencies of this attribute must provide: StampSettingInfo


Defaults to `@build_bazel_rules_nodejs//nodejs:use_stamp_flag`

<h4 id="esbuild-target">target</h4>

(*String*): Environment target (e.g. es2017, chrome58, firefox57, safari11,
1 change: 1 addition & 0 deletions examples/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -22,6 +22,7 @@ example_integration_test(
"//packages/rollup:npm_package": "@bazel/rollup",
"//packages/terser:npm_package": "@bazel/terser",
"//packages/typescript:npm_package": "@bazel/typescript",
"//packages/esbuild:npm_package": "@bazel/esbuild",
},
owners = [
"@alan-agius4",
17 changes: 0 additions & 17 deletions internal/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -13,7 +13,6 @@
# limitations under the License.

load("@build_bazel_rules_nodejs//:index.bzl", "BAZEL_VERSION", "nodejs_test")
load("//internal/node:context.bzl", "node_context_data")

package(default_visibility = ["//visibility:public"])

@@ -34,19 +33,3 @@ nodejs_test(
entry_point = ":check_bazel_version.js",
templated_args = [BAZEL_VERSION],
)

# Detect if the build is running under --stamp
config_setting(
name = "stamp",
values = {"stamp": "true"},
)

# Modelled after go_context_data from rules_go
# passes config values to starlark via a provider
node_context_data(
name = "node_context_data",
stamp = select({
"@build_bazel_rules_nodejs//internal:stamp": True,
"//conditions:default": False,
}),
)
20 changes: 0 additions & 20 deletions internal/node/context.bzl

This file was deleted.

3 changes: 2 additions & 1 deletion internal/node/npm_package_bin.bzl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"A generic rule to run a tool that appears in node_modules/.bin"

load("//:providers.bzl", "DeclarationInfo", "ExternalNpmPackageInfo", "JSModuleInfo", "node_modules_aspect", "run_node")
load("//:providers.bzl", "DeclarationInfo", "ExternalNpmPackageInfo", "JSModuleInfo", "STAMP_ATTR", "node_modules_aspect", "run_node")
load("//internal/common:expand_variables.bzl", "expand_variables")
load("//internal/linker:link_node_modules.bzl", "module_mappings_aspect")

@@ -23,6 +23,7 @@ _ATTRS = {
cfg = "host",
mandatory = True,
),
"stamp": STAMP_ATTR,
}

def _expand_locations(ctx, s):
9 changes: 5 additions & 4 deletions internal/pkg_npm/pkg_npm.bzl
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@ If all users of your library code use Bazel, they should just add your library
to the `deps` of one of their targets.
"""

load("//:providers.bzl", "DeclarationInfo", "JSEcmaScriptModuleInfo", "JSModuleInfo", "JSNamedModuleInfo", "LinkablePackageInfo", "NODE_CONTEXT_ATTRS", "NodeContextInfo")
load("//:providers.bzl", "DeclarationInfo", "JSEcmaScriptModuleInfo", "JSModuleInfo", "JSNamedModuleInfo", "LinkablePackageInfo", "STAMP_ATTR", "StampSettingInfo")

_DOC = """The pkg_npm rule creates a directory containing a publishable npm artifact.
@@ -93,7 +93,7 @@ my_rule(
"""

# Used in angular/angular /packages/bazel/src/ng_package/ng_package.bzl
PKG_NPM_ATTRS = dict(NODE_CONTEXT_ATTRS, **{
PKG_NPM_ATTRS = {
"deps": attr.label_list(
doc = """Other targets which produce files that should be included in the package, such as `rollup_bundle`""",
allow_files = True,
@@ -118,6 +118,7 @@ If package_path is not set the this will be the root node_modules of the workspa
doc = """Files inside this directory which are simply copied into the package.""",
allow_files = True,
),
"stamp": STAMP_ATTR,
"substitutions": attr.string_dict(
doc = """Key-value pairs which are replaced in all the files while building the package.
@@ -159,7 +160,7 @@ See the section on stamping in the [README](stamping)
default = Label("@nodejs//:run_npm.sh.template"),
allow_single_file = True,
),
})
}

# Used in angular/angular /packages/bazel/src/ng_package/ng_package.bzl
PKG_NPM_OUTPUTS = {
@@ -215,7 +216,7 @@ def create_package(ctx, deps_files, nested_packages):
The tree artifact which is the publishable directory.
"""

stamp = ctx.attr.node_context_data[NodeContextInfo].stamp
stamp = ctx.attr.stamp[StampSettingInfo].value

all_files = deps_files + ctx.files.srcs

Loading

0 comments on commit dc7b5ad

Please sign in to comment.