diff --git a/WORKSPACE b/WORKSPACE index 766ec3e7..c346b7f3 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/alexeagle/rules_nodejs/archive/a9b6b6517b92b3342686f6982e5c9d85c0c68cf3.zip", - strip_prefix = "rules_nodejs-a9b6b6517b92b3342686f6982e5c9d85c0c68cf3", - #sha256 = "7d8bfe77001dba22273b910a325042af4ad83deec5421fe666c03fbe019111dc", + url = "https://github.com/bazelbuild/rules_nodejs/archive/092404e3b47e1144ecfc2937d3729b717b1052bf.zip", + strip_prefix = "rules_nodejs-092404e3b47e1144ecfc2937d3729b717b1052bf", + sha256 = "5e3dd3f76a043687939a14ce6aee3049f8bd97d2cd885ef2105ac344d05213a3", ) 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 3a99f8dd..68949f49 100644 --- a/examples/protocol_buffers/BUILD.bazel +++ b/examples/protocol_buffers/BUILD.bazel @@ -1,4 +1,9 @@ -load("@build_bazel_rules_typescript//:defs.bzl", "ts_library", "ts_proto_library", "ts_web_test") +load("@build_bazel_rules_typescript//:defs.bzl", + "ts_library", + "ts_proto_library", + "ts_web_test", + "ts_devserver", +) proto_library( name = "tire_proto", @@ -31,3 +36,16 @@ ts_web_test( deps = ["test_lib"], bootstrap = ["@build_bazel_rules_typescript//:protobufjs_bootstrap_scripts"], ) + +ts_library( + name = "app", + srcs = ["app.ts"], + deps = [":car"], +) + +ts_devserver( + name = "devserver", + deps = [":app"], + entry_module = "build_bazel_rules_typescript/examples/protocol_buffers/app", + bootstrap = ["@build_bazel_rules_typescript//:protobufjs_bootstrap_scripts"], +) diff --git a/examples/protocol_buffers/app.ts b/examples/protocol_buffers/app.ts new file mode 100644 index 00000000..f7628395 --- /dev/null +++ b/examples/protocol_buffers/app.ts @@ -0,0 +1,7 @@ +import {Car} from './car'; + +const serverResponse = `{"make": "Porsche"}`; +const car = Car.create(JSON.parse(serverResponse)); +const el: HTMLDivElement = document.createElement('div'); +el.innerText = `Car from server: ${car.make}`; +document.body.appendChild(el); diff --git a/internal/devserver/ts_devserver.bzl b/internal/devserver/ts_devserver.bzl index 4cceb1ad..0ab6de3a 100644 --- a/internal/devserver/ts_devserver.bzl +++ b/internal/devserver/ts_devserver.bzl @@ -20,6 +20,9 @@ See the README.md. load("@build_bazel_rules_nodejs//internal:node.bzl", "sources_aspect", ) +load("@build_bazel_rules_nodejs//internal/js_library:js_library.bzl", + "write_amd_names_shim", +) def _ts_devserver(ctx): files = depset() @@ -46,12 +49,19 @@ def _ts_devserver(ctx): workspace_name + "/" + f.short_path + "\n" for f in files ])) + amd_names_shim = ctx.actions.declare_file( + "_%s.amd_names_shim.js" % ctx.label.name, + sibling = ctx.outputs.executable) + write_amd_names_shim(ctx.actions, amd_names_shim, ctx.attr.bootstrap) + # Requirejs is always needed so its included as the first script # in script_files before any user specified scripts for the devserver # to concat in order. - script_files = depset() - script_files += ctx.files._requirejs_script - script_files += ctx.files.scripts + script_files = [] + script_files.extend(ctx.files.bootstrap) + script_files.append(ctx.file._requirejs_script) + script_files.append(amd_names_shim) + script_files.extend(ctx.files.scripts) ctx.actions.write(ctx.outputs.scripts_manifest, "".join([ workspace_name + "/" + f.short_path + "\n" for f in script_files ])) @@ -60,9 +70,9 @@ def _ts_devserver(ctx): ctx.executable._devserver, ctx.outputs.manifest, ctx.outputs.scripts_manifest, - ctx.file._requirejs_script] + ] devserver_runfiles += ctx.files.static_files - devserver_runfiles += ctx.files.scripts + devserver_runfiles += script_files serving_arg = "" if ctx.attr.serving_path: @@ -106,6 +116,7 @@ ts_devserver = rule( "serving_path": attr.string(), "data": attr.label_list(allow_files = True, cfg = "data"), "static_files": attr.label_list(allow_files = True), + "bootstrap": attr.label_list(allow_files = [".js"]), # User scripts for the devserver to concat before the source files "scripts": attr.label_list(allow_files = True), # The entry_module should be the AMD module name of the entry module such as "__main__/src/index" diff --git a/internal/karma/ts_web_test.bzl b/internal/karma/ts_web_test.bzl index 4800dd38..3a1e442c 100644 --- a/internal/karma/ts_web_test.bzl +++ b/internal/karma/ts_web_test.bzl @@ -17,7 +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") +load("@build_bazel_rules_nodejs//internal/js_library:js_library.bzl", "write_amd_names_shim") _CONF_TMPL = "//internal/karma:karma.conf.js" # TODO(alexeagle): users will need some control over browser; needs design @@ -46,20 +46,11 @@ 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) + sibling = ctx.outputs.executable) + write_amd_names_shim(ctx.actions, amd_names_shim, ctx.attr.bootstrap) # Explicitly list the requirejs library files here, rather than use # `frameworks: ['requirejs']`