Skip to content

Commit

Permalink
feat(bazel/spec-bundling): support bundling ESM into CJS for legacy t…
Browse files Browse the repository at this point in the history
…ests like protractor
  • Loading branch information
devversion committed Jun 16, 2022
1 parent 3c881f1 commit b6213c5
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 13 deletions.
41 changes: 28 additions & 13 deletions bazel/spec-bundling/index.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_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")

Expand All @@ -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.
Expand All @@ -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,
Expand All @@ -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(
Expand Down
14 changes: 14 additions & 0 deletions bazel/spec-bundling/test/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
],
)
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
Expand Down

0 comments on commit b6213c5

Please sign in to comment.