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

Commit

Permalink
Add ts_devserver bootstrap attribute for UMD scripts that must come b…
Browse files Browse the repository at this point in the history
…efore requirejs
  • Loading branch information
alexeagle committed May 1, 2018
1 parent 543aeba commit 41f499c
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 22 deletions.
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/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")
Expand Down
20 changes: 19 additions & 1 deletion examples/protocol_buffers/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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"],
)
7 changes: 7 additions & 0 deletions examples/protocol_buffers/app.ts
Original file line number Diff line number Diff line change
@@ -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);
21 changes: 16 additions & 5 deletions internal/devserver/ts_devserver.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
]))
Expand All @@ -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:
Expand Down Expand Up @@ -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"
Expand Down
17 changes: 4 additions & 13 deletions internal/karma/ts_web_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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']`
Expand Down

0 comments on commit 41f499c

Please sign in to comment.