protocolbuffers / protobuf

Protocol Buffers - Google's data interchange format
http://protobuf.dev
Other
65.75k stars 15.51k forks source link

gRPC-Web unable to `npx webpack client.js` using Closure (works with CommonJS) #6165

Closed DazWilkin closed 5 years ago

DazWilkin commented 5 years ago

Heads-up: Apologies for the noob question. I've a limited understanding of Closure Library (long-term user of the Compiler service). I assume the general pattern is to use this for Node.JS runtimes but the documentation states that "browsers can be supported using ... webpack ...". The grpc.io and grpc-web documentation default to CommonJS.

What version of protobuf and what language are you using?

protoc --version
libprotoc 3.7.1

server: Golang clients: Golang, JavaScript

What operating system (Linux, Windows, ...) and version?

Debian

What runtime / compiler are you using (e.g., python version or gcc version)

What did you do?

I'm exploring gRPC-Web.

CommonJS (Working)

The CommonJS flow works flawlessly for the HelloWorld sample.

STYLE="commonjs"
protoc \
--proto_path=${HELLOWORLD} \
--proto_path=${GOOGLEAPIS} \
--js_out=import_style=${STYLE}:${HELLOWORLD} \
--grpc-web_out=import_style=${STYLE},mode=grpcwebtext:${HELLOWORLD} \
${HELLOWORLD}/helloworld/helloworld.proto

NB I added google/api/annotations to the proto and must remove these using CommonJS.

and:

const { GreeterClient } = require('./helloworld_grpc_web_pb.js');
const { HelloRequest, HelloReply } = require('./helloworld_pb.js');

with:

{
    "name": "grpc-web",
    "dependencies": {
        "google-protobuf": "^3.6.1",
        "grpc-web": "^0.4.0"
    },
    "devDependencies": {
        "browserify": "^16.2.2",
        "google-closure-library": "^20190513.0.0",
        "webpack": "^4.16.5",
        "webpack-cli": "^3.1.0"
    }
}

Followed by:

npm install
npx webpack client.js
python3 -m http.server 8081
Closure (Not Working)

I tried using Closure instead of CommonJS but am unable to it to work:

STYLE="closure"
protoc \
--proto_path=${HELLOWORLD} \
--proto_path=${GOOGLEAPIS} \
--js_out=import_style=${STYLE}:${HELLOWORLD} \
--grpc-web_out=import_style=${STYLE},mode=grpcwebtext:${HELLOWORLD} \
${HELLOWORLD}/helloworld/helloworld.proto

and:

require("google-closure-library");
const { GreeterClient } = goog.require('proto.helloworld.GreeterClient');
const { HelloRequest } = goog.require('proto.helloworld.HelloRequest');
const { HelloReply } = goog.require('proto.helloworld.HelloReply');

What did you expect to see

Expect to be able to build-run the Closure sample similarly to CommonJS

What did you see instead?

npx webpack client.js
Hash: 3ecf416ccfa5f9e1385d
Version: webpack 4.32.0
Time: 6433ms
Built at: 05/22/2019 11:51:01 AM
 1 asset
Entrypoint main = main.js
  [2] (webpack)/buildin/global.js 472 bytes {0} [built]
[939] ./client.js 2.36 KiB {0} [built]
[943] ./node_modules/google-closure-library/closure/goog sync ^\.\/.*$ 53.1 KiB {0} [built]
    + 1049 hidden modules

[[DELETED: WARNING in ./node_modules/google-closure-library/closure/goog/... ]]

ERROR in ./node_modules/google-closure-library/closure/goog/bootstrap/nodejs.js
Module not found: Error: Can't resolve 'fs' in '[[PATH]]/go/src/google.golang.org/grpc/examples/helloworld/greeter_client_js/node_modules/google-closure-library/closure/goog/bootstrap'
 @ ./node_modules/google-closure-library/closure/goog/bootstrap/nodejs.js 43:9-22
 @ ./client.js

ERROR in ./node_modules/google-closure-library/closure/goog/promise/testsuiteadapter.js
Module not found: Error: Can't resolve 'promises_aplus_tests' in '[[PATH]]/go/src/google.golang.org/grpc/examples/helloworld/greeter_client_js/node_modules/google-closure-library/closure/goog/promise'
 @ ./node_modules/google-closure-library/closure/goog/promise/testsuiteadapter.js 33:4-35
 @ ./node_modules/google-closure-library/closure/goog sync ^\.\/.*$
 @ ./node_modules/google-closure-library/closure/goog/bootstrap/nodejs.js
 @ ./client.js

ERROR in main.js from Terser
module redeclared [main.js:92560,6]

I tried adding "promises-aplus-tests": "^2.1.2" to package.json but this makes no different and reports a critical vuln in growl and a high vulnerability in minimatch.

Anything else we should know about your project / environment

BSBandme commented 5 years ago

Sorry but please file this issue in grpc/web repo. Close for now.