Skip to content

Commit

Permalink
fix(labs): handle const/let syntax in generated protoc js
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Muller authored and alexeagle committed May 26, 2020
1 parent 15ca569 commit 96a0690
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 8 deletions.
25 changes: 24 additions & 1 deletion packages/labs/src/grpc_web/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -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"])

Expand All @@ -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(
Expand Down
22 changes: 15 additions & 7 deletions packages/labs/src/grpc_web/change_import_style.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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, '_');
Expand All @@ -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;');
};

Expand All @@ -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);
Expand Down
1 change: 1 addition & 0 deletions packages/labs/test/grpc_web/proto/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
],
)

Expand Down
7 changes: 7 additions & 0 deletions packages/labs/test/grpc_web/proto/pizza_service.proto
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
}

0 comments on commit 96a0690

Please sign in to comment.