Skip to content

Commit 4dcb37f

Browse files
gregmagolanalexeagle
authored andcommitted
feat: add link_workspace_root to nodejs_binary, npm_package_bin, rollup_bundle, terser_minified, ts_project
Link the workspace root to the bin_dir to support absolute requires like 'my_wksp/path/to/file'. If source files need to be required then they can be copied to the bin_dir with copy_to_bin.
1 parent df18c61 commit 4dcb37f

File tree

23 files changed

+196
-5
lines changed

23 files changed

+196
-5
lines changed

internal/linker/link_node_modules.bzl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,18 @@ def _link_mapping(label, mappings, k, v):
5656
else:
5757
return True
5858

59-
def write_node_modules_manifest(ctx, extra_data = [], mnemonic = None):
59+
def write_node_modules_manifest(ctx, extra_data = [], mnemonic = None, link_workspace_root = False):
6060
"""Writes a manifest file read by the linker, containing info about resolving runtime dependencies
6161
6262
Args:
6363
ctx: starlark rule execution context
6464
extra_data: labels to search for npm packages that need to be linked (ctx.attr.deps and ctx.attr.data will always be searched)
6565
mnemonic: optional action mnemonic, used to differentiate module mapping files from the same rule context
66+
link_workspace_root: Link the workspace root to the bin_dir to support absolute requires like 'my_wksp/path/to/file'.
67+
If source files need to be required then they can be copied to the bin_dir with copy_to_bin.
6668
"""
6769

68-
mappings = {}
70+
mappings = {ctx.workspace_name: ["execroot", ctx.bin_dir.path]} if link_workspace_root else {}
6971
node_modules_root = ""
7072

7173
# Look through data/deps attributes to find...
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
load("//packages/jasmine:index.bzl", "jasmine_node_test")
2+
3+
jasmine_node_test(
4+
name = "test",
5+
srcs = ["test.js"],
6+
link_workspace_root = True,
7+
templated_args = ["--nobazel_patch_module_resolver"],
8+
deps = [
9+
"//internal/linker/test/workspace_link/bar",
10+
"//internal/linker/test/workspace_link/foo",
11+
],
12+
)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
load("@build_bazel_rules_nodejs//:index.bzl", "copy_to_bin")
2+
3+
copy_to_bin(
4+
name = "bar",
5+
srcs = [
6+
"main.js",
7+
"package.json",
8+
],
9+
visibility = ["//internal/linker/test/workspace_link:__pkg__"],
10+
)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
bar: 'bar',
3+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"name": "bar",
3+
"main": "main.js",
4+
"typings": "main.d.ts"
5+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
load("@build_bazel_rules_nodejs//:index.bzl", "copy_to_bin")
2+
load("@npm//typescript:index.bzl", "tsc")
3+
4+
tsc(
5+
name = "foo_lib",
6+
outs = [
7+
"main.d.ts",
8+
"main.js",
9+
],
10+
args = [
11+
"-p",
12+
"$(execpath tsconfig.json)",
13+
"--outDir",
14+
# $(RULEDIR) is a shorthand for the dist/bin directory where Bazel requires we write outputs
15+
"$(RULEDIR)",
16+
],
17+
data = [
18+
"main.ts",
19+
"tsconfig.json",
20+
],
21+
)
22+
23+
copy_to_bin(
24+
name = "foo_files",
25+
srcs = ["package.json"],
26+
)
27+
28+
filegroup(
29+
name = "foo",
30+
srcs = [
31+
":foo_files",
32+
":foo_lib",
33+
],
34+
visibility = ["//internal/linker/test/workspace_link:__pkg__"],
35+
)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const foo: string = 'foo';
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"name": "foo",
3+
"main": "main.js",
4+
"typings": "main.d.ts"
5+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"compilerOptions": {
3+
"declaration": true,
4+
"types": []
5+
}
6+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
describe('linker', () => {
2+
it('should be able to require by absolute path when link_workspace_root is True', () => {
3+
const foo = require('build_bazel_rules_nodejs/internal/linker/test/workspace_link/foo');
4+
expect(foo.foo).toBe('foo');
5+
const bar = require('build_bazel_rules_nodejs/internal/linker/test/workspace_link/bar');
6+
expect(bar.bar).toBe('bar');
7+
});
8+
});

0 commit comments

Comments
 (0)