From 96a0690b747203cf045a07dfcb59ba1cc2cfed20 Mon Sep 17 00:00:00 2001 From: Daniel Muller Date: Tue, 19 May 2020 04:05:50 +0000 Subject: [PATCH] fix(labs): handle const/let syntax in generated protoc js --- packages/labs/src/grpc_web/BUILD.bazel | 25 ++++++++++++++++++- .../labs/src/grpc_web/change_import_style.js | 22 ++++++++++------ packages/labs/test/grpc_web/proto/BUILD.bazel | 1 + .../test/grpc_web/proto/pizza_service.proto | 7 ++++++ 4 files changed, 47 insertions(+), 8 deletions(-) diff --git a/packages/labs/src/grpc_web/BUILD.bazel b/packages/labs/src/grpc_web/BUILD.bazel index e6aca6d0ca..74cef43010 100644 --- a/packages/labs/src/grpc_web/BUILD.bazel +++ b/packages/labs/src/grpc_web/BUILD.bazel @@ -1,6 +1,7 @@ load("@bazel_skylib//:bzl_library.bzl", "bzl_library") load("@build_bazel_rules_nodejs//:index.bzl", "nodejs_binary") load("@build_bazel_rules_nodejs//internal/js_library:js_library.bzl", "js_library") +load("@build_bazel_rules_nodejs//internal/npm_install:npm_umd_bundle.bzl", "npm_umd_bundle") package(default_visibility = ["//visibility:public"]) @@ -17,12 +18,34 @@ nodejs_binary( visibility = ["//visibility:public"], ) +well_known_protos = [ + "any_pb", + "api_pb", + "descriptor_pb", + "duration_pb", + "empty_pb", + "field_mask_pb", + "source_context_pb", + "struct_pb", + "timestamp_pb", + "type_pb", + "wrappers_pb", +] + +[npm_umd_bundle( + name = "google-protobuf-%s__umd" % p, + package_name = "google-protobuf/google/protobuf/%s" % p, + entry_point = "@npm//:node_modules/google-protobuf/google-protobuf.js", + excluded = ["google-protobuf"], + package = "@npm//google-protobuf", +) for p in well_known_protos] + js_library( name = "bootstrap_scripts", srcs = [ "@npm//google-protobuf:google-protobuf__umd", "@npm//grpc-web:grpc-web__umd", - ], + ] + [":google-protobuf-%s__umd" % p for p in well_known_protos], ) filegroup( diff --git a/packages/labs/src/grpc_web/change_import_style.js b/packages/labs/src/grpc_web/change_import_style.js index f1a285d93e..d91ee9cf3c 100644 --- a/packages/labs/src/grpc_web/change_import_style.js +++ b/packages/labs/src/grpc_web/change_import_style.js @@ -67,8 +67,11 @@ function convertToUmd(args, initialContents) { `; }; - const transformations = - [wrapInAMDModule, replaceRecursiveFilePaths(args), removeJsExtensionsFromRequires]; + const transformations = [ + wrapInAMDModule, + replaceRecursiveFilePaths(args), + removeJsExtensionsFromRequires, + ]; return transformations.reduce((currentContents, transform) => { return transform(currentContents); }, initialContents); @@ -119,7 +122,8 @@ function convertToESM(args, initialContents) { const replaceRequiresWithImports = (contents) => { return contents .replace( - /var ([\w\d_]+) = require\((['"][\.\\]*[\w\d@/_-]+['"])\)/g, 'import * as $1 from $2') + /(?:var|const|let) ([\w\d_]+) = require\((['"][\.\\]*[\w\d@/_-]+['"])\)/g, + 'import * as $1 from $2') .replace( /([\.\w\d_]+) = require\((['"][\.\w\d@/_-]+['"])\)/g, (_, variable, importPath) => { const normalizedVariable = variable.replace(/\./g, '_'); @@ -130,7 +134,7 @@ function convertToESM(args, initialContents) { const replaceRequiresWithSubpackageImports = (contents) => { return contents.replace( - /var ([\w\d_]+) = require\((['"][\w\d@/_-]+['"])\)\.([\w\d_]+);/g, + /(?:var|const|let) ([\w\d_]+) = require\((['"][\w\d@/_-]+['"])\)\.([\w\d_]+);/g, 'import * as $1 from $2;'); }; @@ -139,9 +143,13 @@ function convertToESM(args, initialContents) { }; const transformations = [ - replaceRecursiveFilePaths(args), removeJsExtensionsFromRequires, replaceGoogExtendWithExports, - replaceRequiresWithImports, replaceRequiresWithSubpackageImports, - replaceCMDefaultExportWithExports, replaceCJSExportsWithECMAExports + replaceRecursiveFilePaths(args), + removeJsExtensionsFromRequires, + replaceGoogExtendWithExports, + replaceRequiresWithImports, + replaceRequiresWithSubpackageImports, + replaceCMDefaultExportWithExports, + replaceCJSExportsWithECMAExports, ]; return transformations.reduce((currentContents, transform) => { return transform(currentContents); diff --git a/packages/labs/test/grpc_web/proto/BUILD.bazel b/packages/labs/test/grpc_web/proto/BUILD.bazel index 2a6f7c6f23..13ae7812e7 100644 --- a/packages/labs/test/grpc_web/proto/BUILD.bazel +++ b/packages/labs/test/grpc_web/proto/BUILD.bazel @@ -23,6 +23,7 @@ proto_library( deps = [ "//packages/labs/test/grpc_web/proto/common:delivery_person_proto", "//packages/labs/test/grpc_web/proto/common:pizza_proto", + "@com_google_protobuf//:timestamp_proto", ], ) diff --git a/packages/labs/test/grpc_web/proto/pizza_service.proto b/packages/labs/test/grpc_web/proto/pizza_service.proto index 0f5d68411c..eb7649691c 100644 --- a/packages/labs/test/grpc_web/proto/pizza_service.proto +++ b/packages/labs/test/grpc_web/proto/pizza_service.proto @@ -4,6 +4,7 @@ package test.bazel.proto; import "packages/labs/test/grpc_web/proto/common/pizza.proto"; import "packages/labs/test/grpc_web/proto/common/delivery_person.proto"; +import "google/protobuf/timestamp.proto"; service PizzaService { rpc OrderPizza(OrderPizzaRequest) returns (OrderPizzaResponse) { @@ -18,4 +19,10 @@ message OrderPizzaRequest { message OrderPizzaResponse { // The person that will deliver the pizza. DeliveryPerson delivery_person = 1; + + Eta eta = 2; +} + +message Eta { + google.protobuf.Timestamp time_of_arrival = 1; }