diff --git a/bazel/BUILD.bazel b/bazel/BUILD.bazel index 677b6f76f..54ce8fc57 100644 --- a/bazel/BUILD.bazel +++ b/bazel/BUILD.bazel @@ -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", diff --git a/bazel/defaults.bzl b/bazel/defaults.bzl deleted file mode 100644 index 97aeffbac..000000000 --- a/bazel/defaults.bzl +++ /dev/null @@ -1,15 +0,0 @@ -load("@npm//@bazel/concatjs:index.bzl", _ts_library = "ts_library") - -def ts_library(name, testonly = False, deps = [], srcs = [], devmode_module = None, **kwargs): - _ts_library( - name = name, - devmode_module = devmode_module if devmode_module != None else "umd", - devmode_target = "es2020", - prodmode_module = "esnext", - prodmode_target = "es2020", - tsconfig = kwargs.pop("tsconfig", "//:tsconfig"), - testonly = testonly, - deps = deps, - srcs = srcs, - **kwargs - ) diff --git a/bazel/integration/tests/custom_env_variables/BUILD.bazel b/bazel/integration/tests/custom_env_variables/BUILD.bazel index 65910626e..8cc7ae8b4 100644 --- a/bazel/integration/tests/custom_env_variables/BUILD.bazel +++ b/bazel/integration/tests/custom_env_variables/BUILD.bazel @@ -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": "", "BAZELISK_HOME_2": "", }, diff --git a/bazel/integration/tests/custom_env_variables/test.js b/bazel/integration/tests/custom_env_variables/test.js index d8331768f..c912a1390 100644 --- a/bazel/integration/tests/custom_env_variables/test.js +++ b/bazel/integration/tests/custom_env_variables/test.js @@ -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); } diff --git a/bazel/package.json b/bazel/package.json index 8a2db8d0f..da04aeec6 100644 --- a/bazel/package.json +++ b/bazel/package.json @@ -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" }, diff --git a/bazel/pnpm-lock.yaml b/bazel/pnpm-lock.yaml index b038eb495..d9eca0e7f 100644 --- a/bazel/pnpm-lock.yaml +++ b/bazel/pnpm-lock.yaml @@ -47,6 +47,9 @@ importers: selenium-webdriver: specifier: 4.34.0 version: 4.34.0 + semver: + specifier: 7.7.2 + version: 7.7.2 send: specifier: 1.2.0 version: 1.2.0 @@ -1533,6 +1536,11 @@ packages: engines: {node: '>=10'} hasBin: true + semver@7.7.2: + resolution: {integrity: sha512-RF0Fw+rO5AMf9MAyaRXI4AV0Ulj5lMHqVxxdSgiVbixSCXoEmmX/jk0CuJw4+3SqroYO9VoUh+HcuJivvtJemA==} + engines: {node: '>=10'} + hasBin: true + send@0.19.0: resolution: {integrity: sha512-dW41u5VfLXu8SJh5bwRmyYUbAoSB3c9uQh6L8h/KtsFREPWpbX1lrljJo186Jc4nmci/sGUZ9a0a0J2zgfq2hw==} engines: {node: '>= 0.8.0'} @@ -3414,6 +3422,8 @@ snapshots: dependencies: lru-cache: 6.0.0 + semver@7.7.2: {} + send@0.19.0: dependencies: debug: 2.6.9 diff --git a/bazel/spec-bundling/test/core_apf_esm_test.ts b/bazel/spec-bundling/test/core_apf_esm_test.ts index 51d66a878..06f6854f0 100644 --- a/bazel/spec-bundling/test/core_apf_esm_test.ts +++ b/bazel/spec-bundling/test/core_apf_esm_test.ts @@ -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', () => { diff --git a/bazel/spec-bundling/test/core_invalid_linker_decl.spec.ts b/bazel/spec-bundling/test/core_invalid_linker_decl.spec.ts index 3297329b6..8d98f3caa 100644 --- a/bazel/spec-bundling/test/core_invalid_linker_decl.spec.ts +++ b/bazel/spec-bundling/test/core_invalid_linker_decl.spec.ts @@ -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', () => { diff --git a/bazel/test/BUILD.bazel b/bazel/test/BUILD.bazel index cf93de239..e255c528e 100644 --- a/bazel/test/BUILD.bazel +++ b/bazel/test/BUILD.bazel @@ -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( @@ -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)"], ) diff --git a/tools/esbuild.bzl b/tools/esbuild.bzl index 8cb0a7196..e1d7d6e64 100644 --- a/tools/esbuild.bzl +++ b/tools/esbuild.bzl @@ -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", diff --git a/tools/esbuild/index.bzl b/tools/esbuild/index.bzl new file mode 100644 index 000000000..e1d7d6e64 --- /dev/null +++ b/tools/esbuild/index.bzl @@ -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, + ) diff --git a/bazel/esbuild/BUILD.bazel b/tools/esbuild/old-esbuild/BUILD.bazel similarity index 100% rename from bazel/esbuild/BUILD.bazel rename to tools/esbuild/old-esbuild/BUILD.bazel diff --git a/bazel/esbuild/esbuild-amd-config.mjs b/tools/esbuild/old-esbuild/esbuild-amd-config.mjs similarity index 100% rename from bazel/esbuild/esbuild-amd-config.mjs rename to tools/esbuild/old-esbuild/esbuild-amd-config.mjs diff --git a/bazel/extract_js_module_output.bzl b/tools/esbuild/old-esbuild/extract_js_module_output.bzl similarity index 100% rename from bazel/extract_js_module_output.bzl rename to tools/esbuild/old-esbuild/extract_js_module_output.bzl index 1b69eeb37..30a9cebba 100644 --- a/bazel/extract_js_module_output.bzl +++ b/tools/esbuild/old-esbuild/extract_js_module_output.bzl @@ -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.""" diff --git a/bazel/esbuild/index.bzl b/tools/esbuild/old-esbuild/index.bzl similarity index 97% rename from bazel/esbuild/index.bzl rename to tools/esbuild/old-esbuild/index.bzl index 33483872b..d87f0bb8c 100644 --- a/bazel/esbuild/index.bzl +++ b/tools/esbuild/old-esbuild/index.bzl @@ -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, diff --git a/tools/jasmine/old-spec-bundling/spec_bundle.bzl b/tools/jasmine/old-spec-bundling/spec_bundle.bzl index aac702e9a..89acccf31 100644 --- a/tools/jasmine/old-spec-bundling/spec_bundle.bzl +++ b/tools/jasmine/old-spec-bundling/spec_bundle.bzl @@ -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(