1+ "use strict" ;
12module . exports = static_target ;
23
3- static_target . private = true ;
4+ // - Static code does not have any reflection or JSON features.
5+ // - The default wrapper supports AMD, CommonJS and the global scope (as window.root), in this order.
6+ // - You can specify a custom wrapper with the --wrap argument.
7+ // - CommonJS modules depend on the minimal static runtime for reduced package size with browserify.
8+ // - AMD and global scope depend on the full library for now.
9+ // - Services aren't supported, yet.
410
5- // Currently, this target builds single file CommonJS modules.
6- // - There is no reflection and no message inheritance from Prototype.
7- // - Generated code is tailored for browerify build processes (minimal runtime).
8-
9- // TBD:
10- // - Generate a single file or scaffold an entire project directory? Both?
11- // - Targets: ES5, ES6, TypeScript? CommonJS? AMD?
12- // - What about generating typescript definitions for non-ts targets?
11+ var path = require ( "path" ) ,
12+ fs = require ( "fs" ) ;
1313
1414var protobuf = require ( "../.." ) ;
1515
@@ -26,14 +26,21 @@ var out = [];
2626var indent = 0 ;
2727
2828function static_target ( root , options , callback ) {
29- tree = { } ;
29+ if ( options . wrap )
30+ options . wrap = path . resolve ( process . cwd ( ) , options . wrap ) ;
31+ else
32+ options . wrap = path . join ( __dirname , "static.tpl" ) ;
3033 try {
31- buildNamespace ( "module.exports" , root ) ;
32- callback ( null , out . join ( '\n' ) ) ;
34+ var wrap = fs . readFileSync ( options . wrap ) . toString ( "utf8" ) ;
35+ ++ indent ;
36+ buildNamespace ( null , root ) ;
37+ -- indent ;
38+ callback ( null , wrap . replace ( / \r ? \n % O U T P U T % / , out . join ( '\n' ) ) ) ;
3339 } catch ( err ) {
3440 callback ( err ) ;
3541 } finally {
3642 out = [ ] ;
43+ indent = 0 ;
3744 }
3845}
3946
@@ -63,17 +70,7 @@ function name(name) {
6370function buildNamespace ( ref , ns ) {
6471 if ( ! ns )
6572 return ;
66- if ( ns . name === "" ) { // root
67- push ( name ( ref ) + " = (function() {" ) ;
68- ++ indent ;
69- push ( '"use strict";' ) ;
70- push ( "" ) ;
71- push ( "// Minimal static codegen runtime" ) ;
72- push ( "var $runtime = require(\"protobufjs/runtime\");" )
73- push ( "" ) ;
74- push ( "// Lazily resolved type references" ) ;
75- push ( "var $lazyTypes = [];" ) ;
76- } else {
73+ if ( ns . name !== "" ) {
7774 push ( "" ) ;
7875 push ( "/** @alias " + ns . fullName . substring ( 1 ) + " */" ) ;
7976 push ( name ( ref ) + "." + name ( ns . name ) + " = (function() {" ) ;
@@ -84,7 +81,7 @@ function buildNamespace(ref, ns) {
8481 buildType ( undefined , ns ) ;
8582 } else if ( ns instanceof Service )
8683 buildService ( undefined , ns ) ;
87- else {
84+ else if ( ns . name !== "" ) {
8885 push ( "" ) ;
8986 push ( "/** @alias " + ( ns . name && ns . fullName . substring ( 1 ) || "exports" ) + " */" ) ;
9087 push ( "var " + name ( ns . name ) + " = {};" ) ;
@@ -96,13 +93,12 @@ function buildNamespace(ref, ns) {
9693 else if ( nested instanceof Namespace )
9794 buildNamespace ( ns . name , nested ) ;
9895 } ) ;
99- push ( "" ) ;
100- if ( ns . name === "" ) // root
101- push ( "return $runtime.resolve($root, $lazyTypes);" ) ;
102- else
96+ if ( ns . name !== "" ) {
97+ push ( "" ) ;
10398 push ( "return " + name ( ns . name ) + ";" ) ;
104- -- indent ;
105- push ( "})();" ) ;
99+ -- indent ;
100+ push ( "})();" ) ;
101+ }
106102}
107103
108104function buildFunction ( type , functionName , gen , scope ) {
@@ -235,17 +231,24 @@ function buildType(ref, type) {
235231
236232function buildService ( ref , service ) {
237233 push ( "" ) ;
238- push ( name ( ref ) + "." + name ( service . name ) + " = {};" ) ; // currently just an empty object
234+ push ( name ( ref ) + "." + name ( service . name ) + " = {};" ) ;
235+ // TODO: Services are just empty objects currently
239236}
240237
241238function buildEnum ( ref , enm ) {
242239 push ( "" ) ;
243- push ( ref + "." + enm . name + " = {" ) ;
240+ pushComment ( [
241+ enm . name + " values." ,
242+ "@exports " + enm . fullName . substring ( 1 ) ,
243+ "@type {Object.<string,number>}"
244+ ] ) ;
245+ push ( name ( ref ) + "." + name ( enm . name ) + " = {" ) ;
244246 ++ indent ;
245247 push ( "" ) ;
246- Object . keys ( enm . values ) . forEach ( function ( key ) {
247- push ( name ( key ) + ": " + enm . values [ key ] . toString ( 10 ) + "," ) ;
248- } ) ;
248+ var keys = Object . keys ( enm . values ) ;
249+ for ( var i = 0 ; i < keys . length ; ++ i ) {
250+ push ( name ( keys [ i ] ) + ": " + enm . values [ keys [ i ] ] . toString ( 10 ) + ( i < keys . length - 1 ? "," : "" ) ) ;
251+ }
249252 -- indent ;
250253 push ( "};" ) ;
251254}
0 commit comments