Reputation: 1065
I am using protobuf.js in an ionic2 project. I have a valid .proto file which I first convert to a static javascript file by:
pbjs -t static databaseapi.proto > databaseapi.js
Becasue ionic2 uses typescript, I add a d.ts file by doing:
pbts databaseapi.js > databaseapi.d.ts
my application transpiles and runs, but I end up with a runtime error:
Runtime Error $protobuf is not defined
I know it is because the compiled protocol buffer file references the variable $protobuf, but I don't quite know where this variable is defined. I also don't know how to include the missing file because it needs to be referenced by the js file rather than the ts file.
Upvotes: 3
Views: 2695
Reputation: 1065
Thanks dcode, I ended up doing it a little differently. To others reading this, the solution is Ionic specific, please see the answer by dcode for a better generic solution.
What I ended up doing, was adding a reference to the compiled js file to Cordova's index.html as follows:
<script src="lib/protobuf.js"></script>
<script>$protobuf = protobuf;</script>
<script src="js/databaseapi.js"></script>
<script> $database = $root.com.database.api.v1; </script>
databaseapi.js is the name of the compiled protobuf file.
Upvotes: 0
Reputation: 614
-t static
just creates the raw code but doesn't wrap it as a module (and thus does not define the $protobuf
dependency).
To also wrap it as a module, use -t static-module
and pick your desired format through -w default|commonjs|amd|es6
. default
uses an universal wrapper that works with AMD, CommonJS and a global variable.
For all command line options, see: https://github.com/dcodeIO/protobuf.js#command-line
Upvotes: 5