From 543aebabf6ba42ef4014b30bb0c375ced319aab7 Mon Sep 17 00:00:00 2001 From: Alex Eagle Date: Mon, 30 Apr 2018 16:36:11 -0700 Subject: [PATCH] Get AMD shims from the js_library in bootstrap[] --- BUILD.bazel | 6 +++++- WORKSPACE | 6 +++--- examples/protocol_buffers/BUILD.bazel | 1 - examples/protocol_buffers/require.config.js | 3 --- internal/karma/ts_web_test.bzl | 19 ++++++++++++++++++- 5 files changed, 26 insertions(+), 9 deletions(-) delete mode 100644 examples/protocol_buffers/require.config.js diff --git a/BUILD.bazel b/BUILD.bazel index 5b9e298d..770b4159 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -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", @@ -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"], ) diff --git a/WORKSPACE b/WORKSPACE index 0a6138d6..766ec3e7 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -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") diff --git a/examples/protocol_buffers/BUILD.bazel b/examples/protocol_buffers/BUILD.bazel index 3e4e082b..3a99f8dd 100644 --- a/examples/protocol_buffers/BUILD.bazel +++ b/examples/protocol_buffers/BUILD.bazel @@ -30,5 +30,4 @@ ts_web_test( name = "test", deps = ["test_lib"], bootstrap = ["@build_bazel_rules_typescript//:protobufjs_bootstrap_scripts"], - srcs = ["require.config.js"], ) diff --git a/examples/protocol_buffers/require.config.js b/examples/protocol_buffers/require.config.js deleted file mode 100644 index 73548842..00000000 --- a/examples/protocol_buffers/require.config.js +++ /dev/null @@ -1,3 +0,0 @@ -// Shim the protobuf global symbol which was loaded by the bootstrap script -// so that it can be loaded with a named require statement. -define("protobufjs/minimal", () => protobuf); diff --git a/internal/karma/ts_web_test.bzl b/internal/karma/ts_web_test.bzl index bf0d6ce0..4800dd38 100644 --- a/internal/karma/ts_web_test.bzl +++ b/internal/karma/ts_web_test.bzl @@ -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 @@ -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 @@ -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 = [ @@ -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