-
Notifications
You must be signed in to change notification settings - Fork 522
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(builtin): introduce npm_package_bin (#1139)
This is a genrule-like rule that runs any npm bin We generate one of these in the @npm workspace illustrate its usage by replacing stylus and less rules Those rules are now deprecated, will be deprecated on npm, and their sources deleted soon. Design doc: https://hackmd.angular.io/f8OBi9KMTNKButSWsh1gCA?view
- Loading branch information
Showing
21 changed files
with
204 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
"A generic rule to run a tool that appears in node_modules/.bin" | ||
|
||
load("@build_bazel_rules_nodejs//internal/linker:link_node_modules.bzl", "module_mappings_aspect", "register_node_modules_linker") | ||
|
||
_ATTRS = { | ||
"srcs": attr.label_list(allow_files = True), | ||
"outs": attr.output_list(), | ||
"args": attr.string_list(mandatory = True), | ||
"tool": attr.label( | ||
executable = True, | ||
cfg = "host", | ||
mandatory = True, | ||
), | ||
"deps": attr.label_list(aspects = [module_mappings_aspect]), | ||
} | ||
|
||
def _impl(ctx): | ||
args = ctx.actions.args() | ||
inputs = ctx.files.srcs + ctx.files.deps | ||
outputs = ctx.outputs.outs | ||
register_node_modules_linker(ctx, args, inputs) | ||
for a in ctx.attr.args: | ||
args.add(ctx.expand_location(a)) | ||
ctx.actions.run( | ||
executable = ctx.executable.tool, | ||
inputs = inputs, | ||
outputs = outputs, | ||
arguments = [args], | ||
) | ||
|
||
_npm_genrule = rule( | ||
_impl, | ||
attrs = _ATTRS, | ||
) | ||
|
||
def npm_package_bin(tool = None, package = None, package_bin = None, **kwargs): | ||
"""Run an arbitrary npm package binary (anything under node_modules/.bin/*) under Bazel. | ||
This is like a genrule() except that it runs our launcher script that first | ||
links the node_modules tree before running the program. | ||
It's probably easy to wrap this with macros, so something with no rule logic | ||
like stylus_binary could probably just be a macro wrapping this. | ||
Args: | ||
srcs: identical to [genrule.srcs](https://docs.bazel.build/versions/master/be/general.html#genrule.srcs) | ||
deps: Targets that produce or reference npm packages which are needed by the tool | ||
outs: identical to [genrule.outs](https://docs.bazel.build/versions/master/be/general.html#genrule.outs) | ||
args: Command-line arguments to the tool. | ||
Subject to 'Make variable' substitution. | ||
Can use $(location) expansion. See https://docs.bazel.build/versions/master/be/make-variables.html | ||
package: an npm package whose binary to run, like "terser". Assumes your node_modules are installed in a workspace called "npm" | ||
package_bin: the "bin" entry from `package` that should be run. By default package_bin is the same string as `package` | ||
tool: a label for a binary to run, like `@npm//terser/bin:terser`. This is the longer form of package/package_bin | ||
""" | ||
if not tool: | ||
if not package: | ||
fail("You must supply either the tool or package attribute") | ||
if not package_bin: | ||
package_bin = package | ||
tool = "@npm//%s/bin:%s" % (package, package_bin) | ||
|
||
_npm_genrule( | ||
tool = tool, | ||
**kwargs | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
|
||
const content = fs.readFileSync(path.join(require.resolve(__dirname + '/minified.js')), 'utf-8'); | ||
if (!content.includes('{console.error("thing")}')) { | ||
console.error(content); | ||
process.exitCode = 1; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class A { | ||
doThing() { | ||
console.error('thing'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
internal/npm_install/test/golden/@gregmagolan/test-a/index.bzl.golden
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
load("@build_bazel_rules_nodejs//:defs.bzl", "npm_package_bin") | ||
def test(**kwargs): | ||
npm_package_bin(tool = "@fine_grained_goldens//@gregmagolan/test-a/bin:test", **kwargs) |
2 changes: 0 additions & 2 deletions
2
internal/npm_install/test/golden/@gregmagolan/test-b/bin/BUILD.bazel.golden
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
load("@build_bazel_rules_nodejs//:defs.bzl", "npm_package_bin") | ||
def jasmine(**kwargs): | ||
npm_package_bin(tool = "@fine_grained_goldens//jasmine/bin:jasmine", **kwargs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 0 additions & 2 deletions
2
internal/npm_install/test/golden/unidiff/bin/BUILD.bazel.golden
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters