From b6213c5a2f89f353fd848a78afa0636e5dee793a Mon Sep 17 00:00:00 2001 From: Paul Gschwendtner Date: Thu, 16 Jun 2022 12:55:56 +0000 Subject: [PATCH] feat(bazel/spec-bundling): support bundling ESM into CJS for legacy tests like protractor --- bazel/spec-bundling/index.bzl | 41 +++++++++++++++++++--------- bazel/spec-bundling/test/BUILD.bazel | 14 ++++++++++ tsconfig.json | 1 + 3 files changed, 43 insertions(+), 13 deletions(-) diff --git a/bazel/spec-bundling/index.bzl b/bazel/spec-bundling/index.bzl index 02db12727..40ecd3116 100644 --- a/bazel/spec-bundling/index.bzl +++ b/bazel/spec-bundling/index.bzl @@ -1,5 +1,5 @@ load("@build_bazel_rules_nodejs//:index.bzl", "js_library") -load("//bazel/esbuild:index.bzl", "esbuild_amd", "esbuild_config", "esbuild_esm_bundle") +load("//bazel/esbuild:index.bzl", "esbuild", "esbuild_amd", "esbuild_config", "esbuild_esm_bundle") load("//bazel/spec-bundling:spec-entrypoint.bzl", "spec_entrypoint") load("//bazel/spec-bundling:bundle-config.bzl", "spec_bundle_config_file") @@ -12,8 +12,8 @@ def spec_bundle( # We cannot use `ES2017` or higher as that would result in `async/await` not being downleveled. # ZoneJS needs to be able to intercept these as otherwise change detection would not work properly. target = "es2016", - workspace_name = None, - **kwargs): + external = [], + workspace_name = None): """Macro that will bundle all test files with their respective transitive dependencies. Bundled specs end up in a single bundle file that can be loaded within Karma or NodeJS directly. @@ -24,17 +24,21 @@ def spec_bundle( name: Name of the spec bundle target deps: Targets that contain all spec files. Files ending with `spec.js` are picked up. - platform: Platform for which spec should be bundled. i.e. `node` or `browser`. + platform: Platform for which spec should be bundled. i.e. `node`, `browser` or `cjs-legacy`. bootstrap: Targets providing bootstrap scripts that run before the specs. Files ending with `init.js` are picked up. target: Target ECMAScript to use for the specs bundle. run_angular_linker: Whether the Angular linker should process the bundled code. + external: List of modules/packages which should not be bundled. workspace_name: Workspace name that needs to be provided for the AMD module name. - **kwargs: Other arguments to be passed to ESBuild. """ is_browser_test = platform == "browser" + is_legacy_cjs = platform == "cjs-legacy" + is_node = platform == "node" + package_name = native.package_name() + esbuild_attrs = dict() spec_entrypoint( name = "%s_spec_entrypoint" % name, @@ -61,23 +65,34 @@ def spec_bundle( fail("The spec-bundling target %s is declared as browser test. In order to be able " + "to construct an AMD module name, the `workspace_name` attribute needs to be set.") - # Browser tests (Karma) need named AMD modules to load. - # TODO(devversion): consider updating `@bazel/concatjs` to support loading JS files directly. - esbuild_rule = esbuild_amd if is_browser_test else esbuild_esm_bundle - amd_name = "%s/%s/%s" % (workspace_name, package_name, name + "_spec") if is_browser_test else None + esbuild_rule = None + + if is_browser_test: + esbuild_rule = esbuild_amd + amd_name = "%s/%s/%s" % (workspace_name, package_name, name + "_spec") + esbuild_attrs["platform"] = "browser" + esbuild_attrs["output"] = "%s_spec.js" % name + esbuild_attrs["module_name"] = amd_name + elif is_legacy_cjs: + esbuild_rule = esbuild + esbuild_attrs["format"] = "iife" + esbuild_attrs["platform"] = "node" + esbuild_attrs["output"] = "%s_spec.js" % name + elif is_node: + esbuild_rule = esbuild_esm_bundle + esbuild_attrs["platform"] = "node" + esbuild_attrs["output"] = "%s_spec.mjs" % name esbuild_rule( name = "%s_bundle" % name, testonly = True, config = ":%s_config" % name, - output = "%s_spec.%s" % (name, "js" if is_browser_test else "mjs"), entry_point = ":%s_spec_entrypoint" % name, - module_name = amd_name, target = target, - platform = platform, deps = deps + [":%s_spec_entrypoint" % name], link_workspace_root = True, - **kwargs + external = external, + **esbuild_attrs ) js_library( diff --git a/bazel/spec-bundling/test/BUILD.bazel b/bazel/spec-bundling/test/BUILD.bazel index ac5cc0213..d7e3bbfe3 100644 --- a/bazel/spec-bundling/test/BUILD.bazel +++ b/bazel/spec-bundling/test/BUILD.bazel @@ -20,9 +20,23 @@ spec_bundle( deps = [":test_lib"], ) +spec_bundle( + name = "test_bundle_legacy_cjs", + platform = "cjs-legacy", + run_angular_linker = True, + deps = [":test_lib"], +) + jasmine_node_test( name = "test", deps = [ ":test_bundle", ], ) + +jasmine_node_test( + name = "test_legacy_cjs", + deps = [ + ":test_bundle_legacy_cjs", + ], +) diff --git a/tsconfig.json b/tsconfig.json index 786db6599..3ca1b31d4 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -8,6 +8,7 @@ "outDir": "./dist/tsc-non-bazel-out", "skipLibCheck": true, "noImplicitOverride": true, + // TODO(ESM): Remove this when Bazel also uses the Node resolution. "esModuleInterop": true, "noImplicitReturns": true },