Skip to content

Commit

Permalink
Require js_import_ts for nodejs_install
Browse files Browse the repository at this point in the history
  • Loading branch information
pauldraper committed Jul 10, 2023
1 parent 43047a7 commit 42fc7a0
Show file tree
Hide file tree
Showing 11 changed files with 69 additions and 32 deletions.
1 change: 0 additions & 1 deletion angular/rules.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,6 @@ def _angular_library(ctx):
label = label,
cjs_root = cjs_root,
deps = cjs_deps,
files = declarations + js,
)

js_info = create_js_info(
Expand Down
2 changes: 1 addition & 1 deletion javascript/providers.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ JsInfo = provider(

def create_js_info(cjs_root, files = [], deps = []):
return JsInfo(
transitive_files = depset(files, transitive = [js_info.transitive_files for js_info in ([cjs_root] if cjs_root else []) + deps]),
transitive_files = depset(files, transitive = [js_info.transitive_files for js_info in ([cjs_root] if cjs_root and files else []) + deps]),
)

def js_npm_label(repo):
Expand Down
1 change: 0 additions & 1 deletion javascript/rules.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ def _js_library_impl(ctx):
cjs_info = cjs_root and create_cjs_info(
cjs_root = cjs_root,
deps = cjs_deps,
files = js,
globals = cjs_globals,
label = label,
)
Expand Down
32 changes: 15 additions & 17 deletions nodejs/rules.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ load("@rules_pkg//pkg:providers.bzl", "PackageFilegroupInfo", "PackageFilesInfo"
load("@rules_pkg//pkg:tar.bzl", "pkg_tar")
load("//commonjs:providers.bzl", "CjsInfo", "create_globals", "gen_manifest", "package_path")
load("//javascript:providers.bzl", "JsInfo")
load("//util:path.bzl", "relativize", "runfile_path")
load("//util:path.bzl", "nearest", "relativize", "runfile_path")
load(":providers.bzl", "NodejsInfo", "NodejsRuntimeInfo")

def configure_nodejs_runtime(name, repo_name, nodejs_runtime_rule, visibility = None):
Expand Down Expand Up @@ -370,14 +370,15 @@ def nodejs_modules(name, deps, **kwargs):
)

def _nodejs_modules_package_impl(ctx):
deps = [target[CjsInfo] for target in ctx.attr.deps]
deps_cjs = [target[CjsInfo] for target in ctx.attr.deps]
deps_js = [target[JsInfo] for target in ctx.attr.deps]
label = ctx.label
links_cjs = [target[CjsInfo] for target in ctx.attr.links]
workspace = ctx.workspace_name

transitive_packages = depset(
[cjs.package for cjs in links_cjs],
transitive = [cjs.transitive_packages for cjs in deps],
transitive = [cjs_info.transitive_packages for cjs_info in deps_cjs],
)
package_paths = {
package.path: ".content/%s" % runfile_path(workspace, package)
Expand All @@ -386,35 +387,32 @@ def _nodejs_modules_package_impl(ctx):
cjs.package.path: "../%s" % cjs.package.short_path
for cjs in links_cjs
}
package_paths_nonempty = {}

transitive_files = depset(
transitive = [cjs_info.transitive_files for cjs_info in deps],
transitive = [js_info.transitive_files for js_info in deps_js],
)
files_map = {}
for file in transitive_files.to_list():
parts = file.path.split("/")
linked = False
for i in range(len(parts), -1, -1):
p = package_paths.get("/".join(parts[:i]))
if p == None:
package_path = nearest(package_paths, file.path)
if package_path:
if package_paths[package_path].startswith("../"):
continue
if p.startswith("../"):
linked = True
break
if linked:
continue
package_paths_nonempty[package_path] = None
files_map[".content/%s" % runfile_path(workspace, file)] = file
files = PackageFilesInfo(attributes = {}, dest_src_map = files_map)

symlinks = []
transitive_links = depset(
create_globals(label, deps + links_cjs),
transitive = [cjs_info.transitive_links for cjs_info in deps],
create_globals(label, deps_cjs + links_cjs),
transitive = [cjs_info.transitive_links for cjs_info in deps_cjs],
)
for link in transitive_links.to_list():
if link.path == None:
destination = link.name
elif package_paths[link.path].startswith("../"):
elif link.path not in package_paths_nonempty:
continue
elif not package_paths[link.dep].startswith("../") and link.dep not in package_paths_nonempty:
continue
else:
destination = "%s/node_modules/%s" % (package_paths[link.path], link.name)
Expand Down
14 changes: 12 additions & 2 deletions tools/nodejs/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
load("//nodejs:rules.bzl", "nodejs_install", "nodejs_modules_package")
load("//tools/npm:npm.bzl", "ROOTS")
load("//typescript:rules.bzl", "js_import_ts")
load("//tools/npm:npm.bzl", NPM_ROOTS = "ROOTS")

nodejs_install(
name = "install0",
Expand Down Expand Up @@ -30,11 +31,20 @@ nodejs_modules_package(
"//webpack/config:root",
],
tags = ["no-cache"],
deps = ["@npm//%s:lib" % dep["name"] for dep in ROOTS],
deps = [":npm_%s" % dep["name"] for dep in NPM_ROOTS],
)

alias(
name = "nodejs",
actual = "//nodejs:nodejs",
visibility = ["//visibility:public"],
)


[
js_import_ts(
name = "npm_%s" % dep["name"],
dep = "@npm//%s:lib" % dep["name"],
)
for dep in NPM_ROOTS
]
4 changes: 2 additions & 2 deletions tools/nodejs/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ cd "$BUILD_WORKSPACE_DIRECTORY"

# fix Angular language service
rm -fr node_modules/@angular/core
mv node_modules/.content/npm_angular_core_13.1.1-dc3fc578/files node_modules/@angular/core
ln -s ../../@angular/core node_modules/.content/npm_angular_core_13.1.1-dc3fc578/files
mv node_modules/.content/npm_angular_core_16.0.0-dc3fc578/files node_modules/@angular/core
ln -s ../../@angular/core node_modules/.content/npm_angular_core_16.0.0-dc3fc578/files
rm -r node_modules/@angular/core/node_modules
mkdir -p node_modules/@angular/core/node_modules
ln -s ../../../.content/npm_rxjs_7.4.0/files node_modules/@angular/core/node_modules/rxjs
Expand Down
1 change: 0 additions & 1 deletion ts-proto/rules.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,6 @@ def _ts_proto_libraries_impl(ctx):
cjs_infos[lib.label] = create_cjs_info(
cjs_root = cjs_root,
deps = ts_proto.deps_cjs + ts_proto.tsc.runtime_cjs + [cjs_infos[label] for label in lib.deps],
files = declarations,
label = label,
)

Expand Down
31 changes: 29 additions & 2 deletions typescript/rules.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,35 @@ def configure_ts_compiler(name, ts, tslib = None, visibility = None):
visibility = visibility,
)

def _js_import_ts_impl(ctx):
cjs_info = ctx.attr.dep[CjsInfo] if CjsInfo in ctx.attr.dep else None
js_info = ctx.attr.dep[JsInfo] if JsInfo in ctx.attr.dep else None
ts_info = ctx.attr.dep[TsInfo]

default_info = DefaultInfo(
files = ts_info.transitive_files,
)

js_info = JsInfo(
transitive_files = depset(
transitive = [ts_info.transitive_files] + ([js_info.transitive_files] if js_info.transitive_files else []),
),
)

return [default_info, js_info] + ([cjs_info] if cjs_info else [])

js_import_ts = rule(
attrs = {
"dep": attr.label(
mandatory = True,
providers = [TsInfo],
),
},
doc = "Use TS as JS.",
implementation = _js_import_ts_impl,
provides = [JsInfo],
)

def _ts_compiler_impl(ctx):
bin = ctx.attr.bin[DefaultInfo]
cjs_runtime = ctx.attr.runtime and ctx.attr.runtime[CjsInfo]
Expand Down Expand Up @@ -317,7 +346,6 @@ def _ts_library_impl(ctx):
)

cjs_info = cjs_root and create_cjs_info(
files = declarations + js,
label = label,
cjs_root = cjs_root,
deps = cjs_deps,
Expand Down Expand Up @@ -465,7 +493,6 @@ def _ts_import_impl(ctx):
cjs_info = cjs_root and create_cjs_info(
cjs_root = cjs_root,
deps = cjs_deps,
files = declarations,
label = label,
)

Expand Down
6 changes: 2 additions & 4 deletions typescript/workspace.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,14 @@ def _ts_directory_npm_package_build(package, files):
if not any([file.endswith(".d.ts") for file in files]):
return js_directory_npm_plugin().package_build(package, files)

compile_deps = []
deps = []
exports = []
for i, dep in enumerate(package.deps):
dep_list = compile_deps if dep.id.startswith("@types/") else deps
if not dep.name:
dep_list.append(js_npm_label(dep.id))
deps.append(js_npm_label(dep.id))
continue
name = "import%s" % i
dep_list.append(name)
deps.append(name)
exports.append(
"""
ts_export(
Expand Down
7 changes: 7 additions & 0 deletions util/path.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ def link_file(file, actions, label, output, strip_prefix = "", prefix = ""):
)
return result

def nearest(ancestors, path):
parts = path.split("/")
for i in range(len(parts), -1, -1):
ancestor = "/".join(parts[:i])
if ancestor in ancestors:
return ancestor

def relativize(path, start):
if not start:
return path
Expand Down
2 changes: 1 addition & 1 deletion webpack/rules.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ def _webpack_bundle_impl(ctx):

default_info = DefaultInfo(files = depset([output]))

cjs_info = create_cjs_info(cjs_root = cjs_root, files = [output], label = label)
cjs_info = create_cjs_info(cjs_root = cjs_root, label = label)

js_info = create_js_info(cjs_root = cjs_root, files = [output])

Expand Down

0 comments on commit 42fc7a0

Please sign in to comment.