Skip to content
This repository has been archived by the owner on Sep 16, 2021. It is now read-only.

Commit

Permalink
Get AMD shims from the js_library in bootstrap[]
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeagle committed Apr 30, 2018
1 parent 1e0de5e commit 543aeba
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 9 deletions.
6 changes: 5 additions & 1 deletion BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
# https://github.com/bazelbuild/rules_go/blob/master/go/tools/gazelle/README.rst#directives
# gazelle:exclude node_modules
load("@io_bazel_rules_go//go:def.bzl", "gazelle")
load("@build_bazel_rules_nodejs//internal/js_library:js_library.bzl", "js_library")

gazelle(
name = "gazelle",
Expand All @@ -40,11 +41,14 @@ filegroup(

# Runtime libraries needed by the protobufjs library.
# Any JS code produced by the ts_proto_library rule has a runtime dependency on these scripts.
filegroup(
js_library(
name = "protobufjs_bootstrap_scripts",
srcs = [
"@build_bazel_rules_typescript_protobufs_compiletime_deps//:node_modules/long/dist/long.js",
"@build_bazel_rules_typescript_protobufs_compiletime_deps//:node_modules/protobufjs/dist/minimal/protobuf.min.js",
],
# Make devmode loading work when it does require("protobufjs/minimal")
# so this is shimmed to define it to equal global.protobuf
amd_names = {"protobufjs/minimal": "protobuf"},
visibility = ["//visibility:public"],
)
6 changes: 3 additions & 3 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ workspace(name = "build_bazel_rules_typescript")

http_archive(
name = "build_bazel_rules_nodejs",
url = "https://github.com/bazelbuild/rules_nodejs/archive/d32d51db230f7b4749682592e43b1dff92bdbb83.zip",
strip_prefix = "rules_nodejs-d32d51db230f7b4749682592e43b1dff92bdbb83",
sha256 = "7d8bfe77001dba22273b910a325042af4ad83deec5421fe666c03fbe019111dc",
url = "https://github.com/alexeagle/rules_nodejs/archive/a9b6b6517b92b3342686f6982e5c9d85c0c68cf3.zip",
strip_prefix = "rules_nodejs-a9b6b6517b92b3342686f6982e5c9d85c0c68cf3",
#sha256 = "7d8bfe77001dba22273b910a325042af4ad83deec5421fe666c03fbe019111dc",
)

load("@build_bazel_rules_nodejs//:defs.bzl", "node_repositories", "yarn_install")
Expand Down
1 change: 0 additions & 1 deletion examples/protocol_buffers/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,4 @@ ts_web_test(
name = "test",
deps = ["test_lib"],
bootstrap = ["@build_bazel_rules_typescript//:protobufjs_bootstrap_scripts"],
srcs = ["require.config.js"],
)
3 changes: 0 additions & 3 deletions examples/protocol_buffers/require.config.js

This file was deleted.

19 changes: 18 additions & 1 deletion internal/karma/ts_web_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ load("@build_bazel_rules_nodejs//internal:node.bzl",
"sources_aspect",
"expand_path_into_runfiles",
)
load("@build_bazel_rules_nodejs//internal/js_library:js_library.bzl", "AmdNames")

_CONF_TMPL = "//internal/karma:karma.conf.js"
# TODO(alexeagle): users will need some control over browser; needs design
Expand Down Expand Up @@ -45,6 +46,21 @@ def _ts_web_test_impl(ctx):
expand_path_into_runfiles(ctx, f.short_path)
for f in ctx.files.bootstrap
]
# Shim AMD names for UMD bundles that were shipped anonymous
# These are collected from our bootstrap deps (the only place global scripts should appear)
amd_names_shim = ctx.actions.declare_file(
"_%s.amd_names_shim.js" % ctx.label.name,
sibling=ctx.outputs.executable)
amd_names_shim_content = """// GENERATED by ts_web_test.bzl
// Shim these global symbols which were defined by a bootstrap script
// so that they can be loaded with named require statements.
"""
for b in ctx.attr.bootstrap:
if AmdNames in b:
for n in b[AmdNames].names.items():
amd_names_shim_content += "define(\"%s\", () => %s);\n" % n
ctx.actions.write(amd_names_shim, amd_names_shim_content)

# Explicitly list the requirejs library files here, rather than use
# `frameworks: ['requirejs']`
# so that we control the script order, and the bootstrap files come before
Expand All @@ -55,6 +71,7 @@ def _ts_web_test_impl(ctx):
bootstrap_entries += [
"build_bazel_rules_typescript_karma_deps/node_modules/requirejs/require.js",
"build_bazel_rules_typescript_karma_deps/node_modules/karma-requirejs/lib/adapter.js",
"/".join([ctx.workspace_name, amd_names_shim.short_path]),
]
# Finally we load the user's srcs and deps
user_entries = [
Expand Down Expand Up @@ -115,7 +132,7 @@ $KARMA ${{ARGV[@]}}
return [DefaultInfo(
runfiles = ctx.runfiles(
files = ctx.files.srcs + ctx.files.deps + ctx.files.bootstrap + [
conf,
conf, amd_names_shim
],
transitive_files = files,
# Propagate karma_bin and its runfiles
Expand Down

0 comments on commit 543aeba

Please sign in to comment.