feat(labs): introduce a new ts_proto_library with grpc support This version of ts_proto_library is generated using the more standard grpc/grpc-web package.
diff --git a/examples/protocol_buffers/BUILD.bazel b/examples/protocol_buffers/BUILD.bazel index ddaf959..d5bd499 100644 --- a/examples/protocol_buffers/BUILD.bazel +++ b/examples/protocol_buffers/BUILD.bazel
@@ -11,6 +11,14 @@ srcs = ["tire.proto"], ) +ts_proto_library( + # The result will be "tire.d.ts" named after this target. + # We could use the output_name attribute if we want the output named + # differently than the target. + name = "tire", + proto = ":tire_proto", +) + proto_library( name = "car_proto", srcs = ["car.proto"], @@ -22,7 +30,7 @@ # We could use the output_name attribute if we want the output named # differently than the target. name = "car", - deps = [":car_proto"], + proto = ":car_proto", ) ts_config( @@ -38,16 +46,17 @@ tsconfig = "//:tsconfig-test", deps = [ ":car", + ":tire", "@npm//@types/jasmine", - "@npm//@types/long", "@npm//@types/node", - "@npm//long", ], ) karma_web_test_suite( name = "test", - bootstrap = ["@npm_bazel_labs//protobufjs:bootstrap_scripts"], + srcs = [ + "@npm_bazel_labs//grpc_web:bootstrap_scripts", + ], browsers = [ "@io_bazel_rules_webtesting//browsers:chromium-local", "@io_bazel_rules_webtesting//browsers:firefox-local", @@ -64,8 +73,8 @@ ts_devserver( name = "devserver", - bootstrap = ["@npm_bazel_labs//protobufjs:bootstrap_scripts"], entry_module = "examples_protocol_buffers/app", + scripts = ["@npm_bazel_labs//grpc_web:bootstrap_scripts"], deps = [":app"], ) @@ -75,7 +84,12 @@ config_file = "rollup.config.js", entry_point = ":app.ts", format = "iife", - deps = [":app"], + deps = [ + ":app", + "@npm//:node_modules", + "@npm//rollup-plugin-commonjs", + "@npm//rollup-plugin-node-resolve", + ], ) terser_minified( @@ -83,27 +97,11 @@ src = ":bundle", ) -# Needed because the prodserver only loads static files that appear under this -# package. -genrule( - name = "protobufjs", - srcs = [ - "@build_bazel_rules_typescript_protobufs_compiletime_deps//:node_modules/protobufjs/dist/minimal/protobuf.min.js", - "@build_bazel_rules_typescript_protobufs_compiletime_deps//:node_modules/long/dist/long.js", - ], - outs = [ - "protobuf.min.js", - "long.js", - ], - cmd = "outs=($(OUTS)); d=$$(dirname $${outs[0]}); for s in $(SRCS); do cp $$s $$d; done", -) - http_server( name = "prodserver", data = [ "index.html", ":bundle.min", - ":protobufjs", ], )
diff --git a/examples/protocol_buffers/WORKSPACE b/examples/protocol_buffers/WORKSPACE index f6cf1d1..0744852 100644 --- a/examples/protocol_buffers/WORKSPACE +++ b/examples/protocol_buffers/WORKSPACE
@@ -32,6 +32,15 @@ urls = ["https://github.com/protocolbuffers/protobuf/archive/v3.11.4.tar.gz"], ) +http_archive( + name = "rules_proto", + sha256 = "4d421d51f9ecfe9bf96ab23b55c6f2b809cbaf0eea24952683e397decfbd0dd0", + strip_prefix = "rules_proto-f6b8d89b90a7956f6782a4a3609b2f0eee3ce965", + urls = [ + "https://github.com/bazelbuild/rules_proto/archive/f6b8d89b90a7956f6782a4a3609b2f0eee3ce965.tar.gz", + ], +) + load("@build_bazel_rules_nodejs//:index.bzl", "yarn_install") yarn_install( @@ -56,6 +65,12 @@ npm_bazel_labs_dependencies() +load("@rules_proto//proto:repositories.bzl", "rules_proto_dependencies", "rules_proto_toolchains") + +rules_proto_dependencies() + +rules_proto_toolchains() + load("@io_bazel_rules_webtesting//web:repositories.bzl", "web_test_repositories") web_test_repositories()
diff --git a/examples/protocol_buffers/app.ts b/examples/protocol_buffers/app.ts index 2308f5e..044b202 100644 --- a/examples/protocol_buffers/app.ts +++ b/examples/protocol_buffers/app.ts
@@ -1,8 +1,9 @@ -import {Proto} from './car'; +import {Car} from 'examples_protocol_buffers/car_pb'; -const serverResponse = `{"make": "Porsche"}`; -const car = Proto.Car.create(JSON.parse(serverResponse)); +const car = new Car(); +car.setMake('Porsche'); + const el: HTMLDivElement = document.createElement('div'); -el.innerText = `Car from server: ${car.make}`; +el.innerText = `Car from server: ${car.getMake()}`; el.className = 'ts1'; document.body.appendChild(el);
diff --git a/examples/protocol_buffers/car.spec.ts b/examples/protocol_buffers/car.spec.ts index 65f90cd..6618fc1 100644 --- a/examples/protocol_buffers/car.spec.ts +++ b/examples/protocol_buffers/car.spec.ts
@@ -1,34 +1,18 @@ -import {Proto} from './car'; -import Long = require('long'); +import {Car} from 'examples_protocol_buffers/car_pb'; +import {Tire} from 'examples_protocol_buffers/tire_pb'; describe('protocol buffers', () => { it('allows creation of an object described by proto', () => { - const pontiac = Proto.Car.create({ - make: 'pontiac', - frontTires: { - width: 225, - aspectRatio: 65, - construction: 'R', - diameter: 17, - }, - }); - expect(pontiac.make).toEqual('pontiac'); - if (!pontiac.frontTires) { - fail('Should have frontTires set'); - } else { - expect(pontiac.frontTires.width).toEqual(225); - } - }); + const tires = new Tire(); + tires.setAspectRatio(65); + tires.setWidth(225); + tires.setConstruction('R'); + tires.setDiameter(17); - // Asserts that longs are handled correctly. - // This value comes from https://github.com/dcodeIO/long.js#background - it('handles long values correctly', () => { - const pontiac = Proto.Car.create({ - make: 'pontiac', - // Long.MAX_VALUE - mileage: new Long(0xFFFFFFFF, 0x7FFFFFFF), - }); - const object = Proto.Car.toObject(pontiac, {longs: String}); - expect(object['mileage']).toEqual('9223372036854775807'); + const pontiac = new Car(); + pontiac.setMake('pontiac'); + pontiac.setFrontTires(tires) + + expect(pontiac.getMake()).toEqual('pontiac'); }); });
diff --git a/examples/protocol_buffers/index.html b/examples/protocol_buffers/index.html index 6f8690e..e3c9871 100644 --- a/examples/protocol_buffers/index.html +++ b/examples/protocol_buffers/index.html
@@ -3,8 +3,6 @@ <title>protocol_buffers example</title> </head> <body> -<script src="/protobuf.min.js"></script> -<script src="/long.js"></script> <script src="/bundle.min.js"></script> </body> </html> \ No newline at end of file
diff --git a/examples/protocol_buffers/package.json b/examples/protocol_buffers/package.json index a2d4500..4bac109 100644 --- a/examples/protocol_buffers/package.json +++ b/examples/protocol_buffers/package.json
@@ -9,6 +9,8 @@ "@types/jasmine": "2.8.2", "@types/long": "^4.0.0", "@types/node": "11.11.1", + "google-protobuf": "3.11.4", + "grpc-web": "1.0.7", "http-server": "^0.11.1", "karma": "~4.1.0", "karma-chrome-launcher": "2.2.0", @@ -21,6 +23,8 @@ "protractor": "^5.4.2", "requirejs": "2.3.6", "rollup": "1.20.3", + "rollup-plugin-commonjs": "10.1.0", + "rollup-plugin-node-resolve": "5.2.0", "terser": "4.3.1", "typescript": "^3.3.1" },
diff --git a/examples/protocol_buffers/rollup.config.js b/examples/protocol_buffers/rollup.config.js index c7bad73..acd7272 100644 --- a/examples/protocol_buffers/rollup.config.js +++ b/examples/protocol_buffers/rollup.config.js
@@ -1,8 +1,9 @@ +const commonjs = require('rollup-plugin-commonjs'); +const nodeRequire = require('rollup-plugin-node-resolve'); + module.exports = { - // indicate which modules should be treated as external - external: [ - 'long', - 'protobufjs/minimal', + plugins: [ + nodeRequire(), + commonjs(), ], - output: {globals: {long: 'Long', 'protobufjs/minimal': 'protobuf'}} -}; +}; \ No newline at end of file
diff --git a/examples/protocol_buffers/yarn.lock b/examples/protocol_buffers/yarn.lock index 8902e36..fdc585a 100644 --- a/examples/protocol_buffers/yarn.lock +++ b/examples/protocol_buffers/yarn.lock
@@ -107,6 +107,11 @@ resolved "https://registry.yarnpkg.com/@types/long/-/long-4.0.0.tgz#719551d2352d301ac8b81db732acb6bdc28dbdef" integrity sha512-1w52Nyx4Gq47uuu0EVcsHBxZFJgurQ+rTKS3qMHxR1GY2T8c2AJYd6vZoZ9q1rupaDjU0yT+Jc2XTyXkjeMA+Q== +"@types/node@*": + version "13.9.0" + resolved "https://registry.yarnpkg.com/@types/node/-/node-13.9.0.tgz#5b6ee7a77faacddd7de719017d0bc12f52f81589" + integrity sha512-0ARSQootUG1RljH2HncpsY2TJBfGQIKOOi7kxzUY6z54ePu/ZD+wJA8zI2Q6v8rol2qpG/rvqsReco8zNMPvhQ== + "@types/node@11.11.1": version "11.11.1" resolved "https://registry.yarnpkg.com/@types/node/-/node-11.11.1.tgz#9ee55ffce20f72e141863b0036a6e51c6fc09a1f" @@ -127,6 +132,13 @@ resolved "https://registry.yarnpkg.com/@types/q/-/q-0.0.32.tgz#bd284e57c84f1325da702babfc82a5328190c0c5" integrity sha1-vShOV8hPEyXacCur/IKlMoGQwMU= +"@types/resolve@0.0.8": + version "0.0.8" + resolved "https://registry.yarnpkg.com/@types/resolve/-/resolve-0.0.8.tgz#f26074d238e02659e323ce1a13d041eee280e194" + integrity sha512-auApPaJf3NPfe18hSoJkp8EbZzer2ISk7o8mCC3M9he/a04+gbMF97NkpD2S8riMGvm4BMRI59/SZQSaLTKpsQ== + dependencies: + "@types/node" "*" + "@types/selenium-webdriver@^3.0.0": version "3.0.16" resolved "https://registry.yarnpkg.com/@types/selenium-webdriver/-/selenium-webdriver-3.0.16.tgz#50a4755f8e33edacd9c406729e9b930d2451902a" @@ -461,6 +473,11 @@ resolved "https://registry.yarnpkg.com/buffer-from/-/buffer-from-1.1.1.tgz#32713bc028f75c02fdb710d7c7bcec1f2c6070ef" integrity sha512-MQcXEUbCKtEo7bhqEs6560Hyd4XaovZlO/k9V3hjVUF/zwW7KBVdSK4gIt/bzwS9MbR5qob+F5jusZsb0YQK2A== +builtin-modules@^3.1.0: + version "3.1.0" + resolved "https://registry.yarnpkg.com/builtin-modules/-/builtin-modules-3.1.0.tgz#aad97c15131eb76b65b50ef208e7584cd76a7484" + integrity sha512-k0KL0aWZuBt2lrxrcASWDfwOLMnodeQjodT/1SxEQAXsHANgo6ZC/VEaSEHCXt7aSTZ4/4H5LKa+tBXmW7Vtvw== + bytebuffer@~5: version "5.0.1" resolved "https://registry.yarnpkg.com/bytebuffer/-/bytebuffer-5.0.1.tgz#582eea4b1a873b6d020a48d58df85f0bba6cfddd" @@ -924,6 +941,11 @@ resolved "https://registry.yarnpkg.com/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz#1b61c0562190a8dff6ae3bb2cf0200ca130b86d4" integrity sha1-G2HAViGQqN/2rjuyzwIAyhMLhtQ= +estree-walker@^0.6.1: + version "0.6.1" + resolved "https://registry.yarnpkg.com/estree-walker/-/estree-walker-0.6.1.tgz#53049143f40c6eb918b23671d1fe3219f3a1b362" + integrity sha512-SqmZANLWS0mnatqbSfRP5g8OXZC12Fgg1IwNtLsyHDzJizORW4khDfjPqJZsemPWBB2uqykUah5YpQ6epsqC/w== + eventemitter3@^4.0.0: version "4.0.0" resolved "https://registry.yarnpkg.com/eventemitter3/-/eventemitter3-4.0.0.tgz#d65176163887ee59f386d64c82610b696a4a74eb" @@ -1161,11 +1183,21 @@ pify "^2.0.0" pinkie-promise "^2.0.0" +google-protobuf@3.11.4: + version "3.11.4" + resolved "https://registry.yarnpkg.com/google-protobuf/-/google-protobuf-3.11.4.tgz#598ca405a3cfa917a2132994d008b5932ef42014" + integrity sha512-lL6b04rDirurUBOgsY2+LalI6Evq8eH5TcNzi7TYQ3BsIWelT0KSOQSBsXuavEkNf+odQU6c0lgz3UsZXeNX9Q== + graceful-fs@^4.1.11, graceful-fs@^4.1.2, graceful-fs@^4.1.6: version "4.2.2" resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-4.2.2.tgz#6f0952605d0140c1cfdb138ed005775b92d67b02" integrity sha512-IItsdsea19BoLC7ELy13q1iJFNmd7ofZH5+X/pJr90/nRoPEX0DJo1dHDbgtYWOhJhcCgMDTOw84RZ72q6lB+Q== +grpc-web@1.0.7: + version "1.0.7" + resolved "https://registry.yarnpkg.com/grpc-web/-/grpc-web-1.0.7.tgz#9e4fbcf63d3734515332ab59e42baa7d0d290015" + integrity sha512-Fkbz1nyvvt6GC6ODcxh9Fen6LLB3OTCgGHzHwM2Eni44SUhzqPz1UQgFp9sfBEfInOhx3yBdwo9ZLjZAmJ+TtA== + har-schema@^2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/har-schema/-/har-schema-2.0.0.tgz#a94c2224ebcac04782a0d9035521f24735b7ec92" @@ -1465,6 +1497,11 @@ dependencies: is-extglob "^2.1.1" +is-module@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/is-module/-/is-module-1.0.0.tgz#3258fb69f78c14d5b815d664336b4cffb6441591" + integrity sha1-Mlj7afeMFNW4FdZkM2tM/7ZEFZE= + is-number@^3.0.0: version "3.0.0" resolved "https://registry.yarnpkg.com/is-number/-/is-number-3.0.0.tgz#24fd6201a4782cf50561c810276afc7d12d71195" @@ -1498,6 +1535,13 @@ dependencies: isobject "^3.0.1" +is-reference@^1.1.2: + version "1.1.4" + resolved "https://registry.yarnpkg.com/is-reference/-/is-reference-1.1.4.tgz#3f95849886ddb70256a3e6d062b1a68c13c51427" + integrity sha512-uJA/CDPO3Tao3GTrxYn6AwkM4nUPJiGGYu5+cB8qbC7WGFlrKZbiRo7SFKxUAEpFUfiHofWCXBUNhvYJMh+6zw== + dependencies: + "@types/estree" "0.0.39" + is-regex@^1.0.4: version "1.0.4" resolved "https://registry.yarnpkg.com/is-regex/-/is-regex-1.0.4.tgz#5517489b547091b0930e095654ced25ee97e9491" @@ -1769,6 +1813,13 @@ pseudomap "^1.0.2" yallist "^2.1.2" +magic-string@^0.25.2: + version "0.25.7" + resolved "https://registry.yarnpkg.com/magic-string/-/magic-string-0.25.7.tgz#3f497d6fd34c669c6798dcb821f2ef31f5445051" + integrity sha512-4CrMT5DOHTDk4HYDlzmwu4FVCcIYI8gauveasrdCu2IKIFOJ3f0v/8MDGJCDL9oD2ppz/Av1b0Nj345H9M+XIA== + dependencies: + sourcemap-codec "^1.4.4" + map-cache@^0.2.2: version "0.2.2" resolved "https://registry.yarnpkg.com/map-cache/-/map-cache-0.2.2.tgz#c32abd0bd6525d9b051645bb4f26ac5dc98a0dbf" @@ -2151,6 +2202,11 @@ resolved "https://registry.yarnpkg.com/path-is-inside/-/path-is-inside-1.0.2.tgz#365417dede44430d1c11af61027facf074bdfc53" integrity sha1-NlQX3t5EQw0cEa9hAn+s8HS9/FM= +path-parse@^1.0.6: + version "1.0.6" + resolved "https://registry.yarnpkg.com/path-parse/-/path-parse-1.0.6.tgz#d62dbb5679405d72c4737ec58600e9ddcf06d24c" + integrity sha512-GSmOT2EbHrINBf9SR7CDELwlJ8AENk3Qn7OikK4nFYAu3Ote2+JYNVvkpAEQm3/TLNEJFD/xZJjzyxg3KBWOzw== + performance-now@^2.1.0: version "2.1.0" resolved "https://registry.yarnpkg.com/performance-now/-/performance-now-2.1.0.tgz#6309f4e0e5fa913ec1c69307ae364b4b377c9e7b" @@ -2403,6 +2459,13 @@ resolved "https://registry.yarnpkg.com/resolve-url/-/resolve-url-0.2.1.tgz#2c637fe77c893afd2a663fe21aa9080068e2052a" integrity sha1-LGN/53yJOv0qZj/iGqkIAGjiBSo= +resolve@^1.11.0, resolve@^1.11.1: + version "1.15.1" + resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.15.1.tgz#27bdcdeffeaf2d6244b95bb0f9f4b4653451f3e8" + integrity sha512-84oo6ZTtoTUpjgNEr5SJyzQhzL72gaRodsSfyxC/AXRvwu0Yse9H8eF9IpGo7b8YetZhlI6v7ZQ6bKBFV/6S7w== + dependencies: + path-parse "^1.0.6" + ret@~0.1.10: version "0.1.15" resolved "https://registry.yarnpkg.com/ret/-/ret-0.1.15.tgz#b8a4825d5bdb1fc3f6f53c2bc33f81388681c7bc" @@ -2420,6 +2483,35 @@ dependencies: glob "^7.1.3" +rollup-plugin-commonjs@10.1.0: + version "10.1.0" + resolved "https://registry.yarnpkg.com/rollup-plugin-commonjs/-/rollup-plugin-commonjs-10.1.0.tgz#417af3b54503878e084d127adf4d1caf8beb86fb" + integrity sha512-jlXbjZSQg8EIeAAvepNwhJj++qJWNJw1Cl0YnOqKtP5Djx+fFGkp3WRh+W0ASCaFG5w1jhmzDxgu3SJuVxPF4Q== + dependencies: + estree-walker "^0.6.1" + is-reference "^1.1.2" + magic-string "^0.25.2" + resolve "^1.11.0" + rollup-pluginutils "^2.8.1" + +rollup-plugin-node-resolve@5.2.0: + version "5.2.0" + resolved "https://registry.yarnpkg.com/rollup-plugin-node-resolve/-/rollup-plugin-node-resolve-5.2.0.tgz#730f93d10ed202473b1fb54a5997a7db8c6d8523" + integrity sha512-jUlyaDXts7TW2CqQ4GaO5VJ4PwwaV8VUGA7+km3n6k6xtOEacf61u0VXwN80phY/evMcaS+9eIeJ9MOyDxt5Zw== + dependencies: + "@types/resolve" "0.0.8" + builtin-modules "^3.1.0" + is-module "^1.0.0" + resolve "^1.11.1" + rollup-pluginutils "^2.8.1" + +rollup-pluginutils@^2.8.1: + version "2.8.2" + resolved "https://registry.yarnpkg.com/rollup-pluginutils/-/rollup-pluginutils-2.8.2.tgz#72f2af0748b592364dbd3389e600e5a9444a351e" + integrity sha512-EEp9NhnUkwY8aif6bxgovPHMoMoNr2FulJziTndpt5H9RdwC47GSGuII9XxpSdzVGM0GWrNPHV6ie1LTNJPaLQ== + dependencies: + estree-walker "^0.6.1" + rollup@1.20.3: version "1.20.3" resolved "https://registry.yarnpkg.com/rollup/-/rollup-1.20.3.tgz#6243f6c118ca05f56b2d9433112400cd834a1eb8" @@ -2638,6 +2730,11 @@ resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.6.1.tgz#74722af32e9614e9c287a8d0bbde48b5e2f1a263" integrity sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g== +sourcemap-codec@^1.4.4: + version "1.4.8" + resolved "https://registry.yarnpkg.com/sourcemap-codec/-/sourcemap-codec-1.4.8.tgz#ea804bd94857402e6992d05a38ef1ae35a9ab4c4" + integrity sha512-9NykojV5Uih4lgo5So5dtw+f0JgJX30KCNI8gwhz2J9A15wD0Ml6tjHKwf6fTSa6fAdVBdZeNOs9eJ71qCk8vA== + split-string@^3.0.1, split-string@^3.0.2: version "3.1.0" resolved "https://registry.yarnpkg.com/split-string/-/split-string-3.1.0.tgz#7cb09dda3a86585705c64b39a6466038682e8fe2"