Skip to content

Commit def7b45

Browse files
committed
New: Removed even more clutter from generated static code
1 parent dbd19fd commit def7b45

20 files changed

+677
-875
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ pbjs.main([ "--target", "json-module", "path/to/myproto.proto" ], function(err,
345345

346346
### Descriptors vs. static modules
347347

348-
While .proto and JSON files require the full library (about 17.5kb gzipped), pretty much all code but the relatively short descriptors is shared and all features including reflection and the parser are available.
348+
While .proto and JSON files require the full library (about 18.5kb gzipped), pretty much all code but the relatively short descriptors is shared and all features including reflection and the parser are available.
349349

350350
Static code, on the other hand, requires just the minimal runtime (about 5.5kb gzipped), but generates additional, albeit editable, source code without any reflection features.
351351

cli/targets/static.js

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,14 @@ function buildFunction(type, functionName, gen, scope) {
176176
delete scope[key];
177177
});
178178

179+
var hasScope = Object.keys(scope).length;
180+
179181
// enclose all but the first and last line in an iife returning our properly scoped function
180182
var lines = code.split(/\n/g);
181-
push(name(type.name) + "." + functionName + " = (function(" + Object.keys(scope).join(", ") + ") { return " + lines[0]);
183+
if (hasScope)
184+
push(name(type.name) + "." + functionName + " = (function(" + Object.keys(scope).join(", ") + ") { return " + lines[0]);
185+
else
186+
push(name(type.name) + "." + functionName + " = " + lines[0]);
182187
lines.slice(1, lines.length - 1).forEach(function(line) {
183188
var prev = indent;
184189
var i = 0;
@@ -187,7 +192,10 @@ function buildFunction(type, functionName, gen, scope) {
187192
push(line.trim());
188193
indent = prev;
189194
});
190-
push("};})(" + Object.keys(scope).map(function(key) { return scope[key]; }).join(", ") + ");");
195+
if (hasScope)
196+
push("};})(" + Object.keys(scope).map(function(key) { return scope[key]; }).join(", ") + ");");
197+
else
198+
push("};");
191199
}
192200

193201
function toJsType(field) {
@@ -246,14 +254,8 @@ function buildType(ref, type) {
246254
--indent;
247255
push("}");
248256

249-
if (type.fieldsArray.length || type.oneofsArray.length || config.convert) {
250-
push("");
251-
if (config.comments)
252-
push("/** @alias " + fullName + ".prototype */");
253-
push("var $prototype = " + name(type.name) + ".prototype;");
254-
}
255-
256257
// default values
258+
var firstField = true;
257259
type.fieldsArray.forEach(function(field) {
258260
field.resolve();
259261
var jsType = toJsType(field);
@@ -269,21 +271,24 @@ function buildType(ref, type) {
269271
prop.charAt(0) !== "." ? "@name " + fullName + "#" + field.name : null,
270272
"@type {" + jsType + "}"
271273
]);
274+
} else if (firstField) {
275+
push("");
276+
firstField = false;
272277
}
273278
if (field.repeated)
274-
push("$prototype" + prop + " = $protobuf.util.emptyArray;");
279+
push(name(type.name) + ".prototype" + prop + " = $protobuf.util.emptyArray;");
275280
else if (field.map)
276-
push("$prototype" + prop + " = $protobuf.util.emptyObject;");
281+
push(name(type.name) + ".prototype" + prop + " = $protobuf.util.emptyObject;");
277282
else if (field.long)
278-
push("$prototype" + prop + " = $protobuf.util.Long ? $protobuf.util.Long.fromBits("
283+
push(name(type.name) + ".prototype" + prop + " = $protobuf.util.Long ? $protobuf.util.Long.fromBits("
279284
+ JSON.stringify(field.typeDefault.low) + ","
280285
+ JSON.stringify(field.typeDefault.high) + ","
281286
+ JSON.stringify(field.typeDefault.unsigned)
282287
+ ") : " + field.typeDefault.toNumber(field.type.charAt(0) === "u") + ";");
283288
else if (field.bytes) {
284-
push("$prototype" + prop + " = $protobuf.util.newBuffer(" + JSON.stringify(Array.prototype.slice.call(field.typeDefault)) + ");");
289+
push(name(type.name) + ".prototype" + prop + " = $protobuf.util.newBuffer(" + JSON.stringify(Array.prototype.slice.call(field.typeDefault)) + ");");
285290
} else
286-
push("$prototype" + prop + " = " + JSON.stringify(field.typeDefault) + ";");
291+
push(name(type.name) + ".prototype" + prop + " = " + JSON.stringify(field.typeDefault) + ";");
287292
});
288293

289294
// virtual oneof fields
@@ -303,7 +308,7 @@ function buildType(ref, type) {
303308
"@name " + fullName + "#" + name(oneof.name),
304309
"@type {string|undefined}"
305310
]);
306-
push("Object.defineProperty($prototype, " + JSON.stringify(oneof.name) +", {");
311+
push("Object.defineProperty(" + name(type.name) + ".prototype, " + JSON.stringify(oneof.name) +", {");
307312
++indent;
308313
push("get: $protobuf.util.oneOfGetter($oneOfFields = [" + oneof.oneof.map(JSON.stringify).join(", ") + "]),");
309314
push("set: $protobuf.util.oneOfSetter($oneOfFields)");
@@ -323,7 +328,7 @@ function buildType(ref, type) {
323328
if (hasTypes && (config.encode || config.decode || config.verify || config.convert)) {
324329
push("");
325330
if (config.comments)
326-
push("// Referenced types");
331+
push("// Lazily resolved referenced types");
327332
push("var $types = {" + types.join(",") + "}; $lazyTypes.push($types);");
328333
}
329334

@@ -461,7 +466,7 @@ function buildType(ref, type) {
461466
"@param {$protobuf.ConversionOptions} [options] Conversion options",
462467
"@returns {Object.<string,*>} Plain object"
463468
]);
464-
push("$prototype.toObject = function toObject(options) {");
469+
push(name(type.name) + ".prototype.toObject = function toObject(options) {");
465470
++indent;
466471
push("return this.constructor.toObject(this, options);");
467472
--indent;
@@ -472,7 +477,7 @@ function buildType(ref, type) {
472477
"Converts this " + type.name + " to JSON.",
473478
"@returns {Object.<string,*>} JSON object"
474479
]);
475-
push("$prototype.toJSON = function toJSON() {");
480+
push(name(type.name) + ".prototype.toJSON = function toJSON() {");
476481
++indent;
477482
push("return this.constructor.toObject(this, $protobuf.util.toJSONOptions);");
478483
--indent;

dist/noparse/protobuf.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/noparse/protobuf.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/noparse/protobuf.min.js.gz

2 Bytes
Binary file not shown.

dist/protobuf.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/protobuf.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/protobuf.min.js.gz

0 Bytes
Binary file not shown.

dist/runtime/protobuf.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/runtime/protobuf.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)