From dbb2a900ec2c191c39141efcb49c91c1753b8323 Mon Sep 17 00:00:00 2001 From: Cooper <5051159+CoopeyB@users.noreply.github.com> Date: Wed, 5 Feb 2020 12:45:15 -0800 Subject: [PATCH] change ts_proto_library to default es5 wrapping (#1593) * change ts_proto_library to default es5 wrapping This change wraps es5 sources generated with `ts_proto_library` to be wrapped in "default" mode. The "default" mode behaves like UMD, supporting both AMD and CommonJS. This change is necessary to support Node, as Node does not use AMD. See https://github.com/bazelbuild/rules_nodejs/issues/562 --- WORKSPACE | 5 +++ .../labs/src/protobufjs/ts_proto_library.bzl | 2 +- packages/labs/test/protobufjs/BUILD.bazel | 31 +++++++++++++++++++ packages/labs/test/protobufjs/test.proto | 5 +++ packages/labs/test/protobufjs/test.ts | 9 ++++++ 5 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 packages/labs/test/protobufjs/BUILD.bazel create mode 100644 packages/labs/test/protobufjs/test.proto create mode 100644 packages/labs/test/protobufjs/test.ts diff --git a/WORKSPACE b/WORKSPACE index 0eec2953a6..bb0b861823 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -286,3 +286,8 @@ load("@build_bazel_integration_testing//tools:repositories.bzl", "bazel_binaries # Depend on the Bazel binaries bazel_binaries(versions = [BAZEL_VERSION]) + +# Install labs dependencies +load("@npm_bazel_labs//:package.bzl", "npm_bazel_labs_dependencies") + +npm_bazel_labs_dependencies() diff --git a/packages/labs/src/protobufjs/ts_proto_library.bzl b/packages/labs/src/protobufjs/ts_proto_library.bzl index 6d2fa229b1..a25100efa1 100644 --- a/packages/labs/src/protobufjs/ts_proto_library.bzl +++ b/packages/labs/src/protobufjs/ts_proto_library.bzl @@ -15,7 +15,7 @@ load("@build_bazel_rules_nodejs//:providers.bzl", "DeclarationInfo", "JSEcmaScriptModuleInfo", "JSNamedModuleInfo") -def _run_pbjs(actions, executable, var, output_name, proto_files, suffix = ".js", wrap = "amd", amd_name = ""): +def _run_pbjs(actions, executable, var, output_name, proto_files, suffix = ".js", wrap = "default", amd_name = ""): js_file = actions.declare_file(output_name + suffix) # Create an intermediate file so that we can do some manipulation of the diff --git a/packages/labs/test/protobufjs/BUILD.bazel b/packages/labs/test/protobufjs/BUILD.bazel new file mode 100644 index 0000000000..49a20c11b1 --- /dev/null +++ b/packages/labs/test/protobufjs/BUILD.bazel @@ -0,0 +1,31 @@ +load("@npm_bazel_jasmine//:index.from_src.bzl", "jasmine_node_test") +load("@npm_bazel_labs//:index.bzl", "ts_proto_library") +load("@npm_bazel_typescript//:index.from_src.bzl", "ts_library") +load("@rules_proto//proto:defs.bzl", "proto_library") + +ts_library( + name = "test", + srcs = ["test.ts"], + deps = [ + ":test_ts_proto", + "@npm//@types/jasmine", + ], +) + +proto_library( + name = "test_proto", + srcs = [":test.proto"], +) + +ts_proto_library( + name = "test_ts_proto", + deps = [":test_proto"], +) + +jasmine_node_test( + name = "protobuf_test", + srcs = [":test"], + deps = [ + "@npm//protobufjs", + ], +) diff --git a/packages/labs/test/protobufjs/test.proto b/packages/labs/test/protobufjs/test.proto new file mode 100644 index 0000000000..b2a2e63a09 --- /dev/null +++ b/packages/labs/test/protobufjs/test.proto @@ -0,0 +1,5 @@ +syntax = "proto3"; + +message TestMessage { + string test_field = 1; +} diff --git a/packages/labs/test/protobufjs/test.ts b/packages/labs/test/protobufjs/test.ts new file mode 100644 index 0000000000..d9d80f5360 --- /dev/null +++ b/packages/labs/test/protobufjs/test.ts @@ -0,0 +1,9 @@ +import {TestMessage} from 'build_bazel_rules_nodejs/packages/labs/test/protobufjs/test_ts_proto'; + +describe('protobufjs', () => { + it('should work in node', () => { + expect(TestMessage.verify({ + testField: 'Hello', + })).toBeFalsy(); // verify returns an error if failed + }); +});