Skip to content

refactor(bazel): complete removal of old @npm// and rules_nodejs usages from bazel #2936

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions bazel/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,11 @@ filegroup(
name = "static_files",
srcs = [
"BUILD.bazel",
"defaults.bzl",
"expand_template.bzl",
"extract_js_module_output.bzl",
"extract_types.bzl",
"filter_outputs.bzl",
"//bazel/api-golden:files",
"//bazel/constraints:files",
"//bazel/esbuild:files",
"//bazel/git-toolchain:files",
"//bazel/http-server:files",
"//bazel/private:files",
Expand Down
15 changes: 0 additions & 15 deletions bazel/defaults.bzl

This file was deleted.

6 changes: 3 additions & 3 deletions bazel/integration/tests/custom_env_variables/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ integration_test(
commands = [
"node ./test.js",
],
data = ["@npm//:node_modules/semver/package.json"],
data = ["@devinfra//bazel:node_modules/semver/dir"],
environment = {
"CUSTOM_VAR": "yes!",
"RESOLVED_BIN": "$(rootpath @npm//:node_modules/semver/package.json)",
"MANUAL_ROOT_PATH": "../npm/node_modules/semver/package.json",
"RESOLVED_BIN": "$(rootpath @devinfra//bazel:node_modules/semver/dir)/package.json",
"MANUAL_ROOT_PATH": "../bazel/node_modules/semver/package.json",
"BAZELISK_HOME": "<TMP>",
"BAZELISK_HOME_2": "<TMP>",
},
Expand Down
2 changes: 1 addition & 1 deletion bazel/integration/tests/custom_env_variables/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ if (require(process.env.RESOLVED_BIN).name !== 'semver') {
process.exit(1);
}

if (process.env.MANUAL_ROOT_PATH !== '../npm/node_modules/semver/package.json') {
if (process.env.MANUAL_ROOT_PATH !== '../bazel/node_modules/semver/package.json') {
console.error('Expected `MANUAL_ROOT_PATH` to be unmodified as it is not an expanded value.');
process.exit(1);
}
Expand Down
1 change: 1 addition & 0 deletions bazel/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"wait-on": "^8.0.3",
"yargs": "18.0.0",
"protractor": "7.0.0",
"semver": "7.7.2",
"selenium-webdriver": "4.34.0",
"tinyglobby": "0.2.14"
},
Expand Down
10 changes: 10 additions & 0 deletions bazel/pnpm-lock.yaml

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

5 changes: 1 addition & 4 deletions bazel/spec-bundling/test/core_apf_esm_test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
// This is an ESM import that would usually break within `jasmine_node_test` because it
// consumes devmode CommonJS sources and `rules_nodejs` does not support ESM well yet.
import {VERSION} from '@angular/core';
import * as core from '@angular/core';
import '@angular/compiler';

describe('@angular/core ESM import', () => {
it('should work', () => {
expect(VERSION.major).toBeGreaterThanOrEqual(13);
expect(core.VERSION.major).toBeGreaterThanOrEqual(13);
});

it('should have run the linker', () => {
Expand Down
5 changes: 1 addition & 4 deletions bazel/spec-bundling/test/core_invalid_linker_decl.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
// This is an ESM import that would usually break within `jasmine_node_test` because it
// consumes devmode CommonJS sources and `rules_nodejs` does not support ESM well yet.
import {VERSION} from '@angular/core';
import * as core from '@angular/core';
import '@angular/compiler';

describe('@angular/core ESM import', () => {
it('should work', () => {
expect(VERSION.major).toBeGreaterThanOrEqual(13);
expect(core.VERSION.major).toBeGreaterThanOrEqual(13);
});

it('should have run the linker', () => {
Expand Down
6 changes: 3 additions & 3 deletions bazel/test/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
load("@aspect_rules_js//js:defs.bzl", "js_test")
load("@aspect_rules_ts//ts:defs.bzl", "ts_project")
load("@build_bazel_rules_nodejs//:index.bzl", "nodejs_test")
load("//bazel:extract_types.bzl", "extract_types")

ts_project(
Expand Down Expand Up @@ -31,9 +31,9 @@ extract_types(
deps = [":test_lib"],
)

nodejs_test(
js_test(
name = "extract_types_test",
data = [":extract_types_target"],
entry_point = "extract_types_test.js",
templated_args = ["$(rootpaths :extract_types_target)"],
fixed_args = ["$(rootpaths :extract_types_target)"],
)
4 changes: 2 additions & 2 deletions tools/esbuild.bzl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
load("@build_bazel_rules_nodejs//:index.bzl", "generated_file_test")
load("//bazel:extract_js_module_output.bzl", "extract_js_module_output")
load("//tools/esbuild/old-esbuild:extract_js_module_output.bzl", "extract_js_module_output")
load(
"//bazel/esbuild:index.bzl",
"//tools/esbuild/old-esbuild:index.bzl",
_esbuild = "esbuild",
_esbuild_cjs_bundle = "esbuild_cjs_bundle",
_esbuild_config = "esbuild_config",
Expand Down
94 changes: 94 additions & 0 deletions tools/esbuild/index.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
load("@build_bazel_rules_nodejs//:index.bzl", "generated_file_test")
load("//tools/esbuild/old-esbuild:extract_js_module_output.bzl", "extract_js_module_output")
load(
"//tools/esbuild/old-esbuild:index.bzl",
_esbuild = "esbuild",
_esbuild_cjs_bundle = "esbuild_cjs_bundle",
_esbuild_config = "esbuild_config",
_esbuild_esm_bundle = "esbuild_esm_bundle",
)

esbuild_config = _esbuild_config

def _esbuild_devmode_prioritize(
esbuild_rule,
name,
testonly = False,
platform = "node",
target = "node20",
deps = [],
**kwargs):
# TODO: Rename once devmode and prodmode have been combined.
# This helps speeding up building as ESBuild (used internally by the rule) would
# request both devmode and prodmode output flavor (resulting in 2x TS compilations).
# Note: Devmode is equivalent to Prodmode and Prodmode breaks `.js` extensions.
extract_js_module_output(
name = "%s_devmode_deps" % name,
deps = deps,
testonly = testonly,
provider = "JSModuleInfo",
forward_linker_mappings = True,
include_external_npm_packages = True,
include_default_files = False,
include_declarations = False,
)

esbuild_rule(
name = name,
platform = platform,
target = target,
testonly = testonly,
deps = [":%s_devmode_deps" % name],
**kwargs
)

def esbuild(**kwargs):
_esbuild_devmode_prioritize(
_esbuild,
**kwargs
)

def esbuild_esm_bundle(**kwargs):
_esbuild_devmode_prioritize(
_esbuild_esm_bundle,
**kwargs
)

def esbuild_cjs_bundle(**kwargs):
_esbuild_devmode_prioritize(
_esbuild_cjs_bundle,
**kwargs
)

def esbuild_checked_in(name, **kwargs):
esbuild_esm_bundle(
name = "%s_generated" % name,
# Unfortunately we need to omit source maps from the checked-in files as these
# will vary based on the platform. See more details below in the sanitization
# genrule transformation. It is acceptable not having source-maps for the checked-in
# files as those are not minified and its to debug, the checked-in file can be visited.
sourcemap = "external",
# We always disable minification for checked-in files as otherwise it will
# become difficult determining potential differences. e.g. on Windows ESBuild
# accidentally included `source-map-support` due to the missing sandbox.
minify = False,
**kwargs
)

# ESBuild adds comments and function identifiers with the name of their module
# location. e.g. `"bazel-out/x64_windows-fastbuild/bin/node_modules/a"function(exports)`.
# We strip all of these paths as that would break approval of the he checked-in files within
# different platforms (e.g. RBE running with K8). Additionally these paths depend
# on the non-deterministic hoisting of the package manager across all platforms.
native.genrule(
name = "%s_sanitized" % name,
srcs = ["%s_generated.js" % name],
outs = ["%s_sanitized.js" % name],
cmd = """cat $< | sed -E "s#(bazel-out|node_modules)/[^\\"']+##g" > $@""",
)

generated_file_test(
name = name,
src = "%s.js" % name,
generated = "%s_sanitized.js" % name,
)
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
load("@rules_nodejs//nodejs:providers.bzl", "JSModuleInfo")
load("@build_bazel_rules_nodejs//:providers.bzl", "DeclarationInfo", "ExternalNpmPackageInfo", "JSEcmaScriptModuleInfo", "JSNamedModuleInfo", "node_modules_aspect")
load("@build_bazel_rules_nodejs//internal/linker:link_node_modules.bzl", "LinkerPackageMappingInfo", "module_mappings_aspect")
load("@rules_nodejs//nodejs:providers.bzl", "JSModuleInfo")

"""Converts a provider name to its actually Starlark provider instance."""

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def esbuild_amd(name, entry_point, module_name, testonly = False, config = None,
expand_template(
name = "%s_config" % name,
testonly = testonly,
template = "//bazel/esbuild:esbuild-amd-config.mjs",
template = "//tools/esbuild/old-esbuild:esbuild-amd-config.mjs",
output_name = "%s_config.mjs" % name,
substitutions = {
"TMPL_MODULE_NAME": module_name,
Expand Down
2 changes: 1 addition & 1 deletion tools/jasmine/old-spec-bundling/spec_bundle.bzl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
load("@build_bazel_rules_nodejs//:index.bzl", "js_library")
load("//bazel/esbuild:index.bzl", "esbuild", "esbuild_amd", "esbuild_config", "esbuild_esm_bundle")
load("//tools/esbuild/old-esbuild:index.bzl", "esbuild", "esbuild_amd", "esbuild_config", "esbuild_esm_bundle")
load("//tools/jasmine/old-spec-bundling:spec-entrypoint.bzl", "spec_entrypoint")

def spec_bundle(
Expand Down