Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Let's talk about static code generation #512

Closed
dcodeIO opened this issue Dec 1, 2016 · 11 comments
Closed

Let's talk about static code generation #512

dcodeIO opened this issue Dec 1, 2016 · 11 comments

Comments

@dcodeIO
Copy link
Member

dcodeIO commented Dec 1, 2016

There have been quite a lot of requests for static code generation in the past. Personally, I'd not absolutely require it with v6's built-in codegen, but there certainly are valid use cases where having actual code to reference makes sense (i.e. when using TypeScript).

I know that a few people already started working on something for v5, so I added a static stub to v6's pbjs that can be used as a reference: https://github.com/dcodeIO/protobuf.js/blob/master/cli/targets/static.js

I also made sure that built-in code generation can be reused to write encode/decode functions on top of static code. See Type#decode / Type#encode for an example on how to bind a scope and make it output a function.

If you have any suggestions or want to tackle it, let me know!

@dcodeIO
Copy link
Member Author

dcodeIO commented Dec 1, 2016

This is how generated code looks like currently: https://github.com/dcodeIO/protobuf.js/blob/master/tests/data/package.js

Of course it is missing reflection entirely and there is no asJSON or services, yet. On the other hand, it only requires a super minimal runtime.

@englercj
Copy link

englercj commented Dec 2, 2016

I think for me being able to write custom targets for the CLI would be nice, instead of having to pick from premade targets something like --target-file mytarget.js which just loads my file and treats it like one of the targets would be ideal.

Currently there is no way to "plug in" to the CLI and do custom things.

@dcodeIO
Copy link
Member Author

dcodeIO commented Dec 3, 2016

Specifying a path to an alternative --target is now possible.

I've also updated the static target to support AMD, CommonJS and the global scope, depending on what is available. Additionally, the --wrap option lets you specify an alternative wrapper.

@dcodeIO
Copy link
Member Author

dcodeIO commented Dec 3, 2016

Now also generates services (example).

@paralin
Copy link

paralin commented Dec 7, 2016

Can we generate a TypeScript definition with the static bundle?

@dcodeIO
Copy link
Member Author

dcodeIO commented Dec 7, 2016

Most likely, just as we do with the library itself, we could run tsd-jsdoc on the static output.

@chuanglovelily
Copy link

chuanglovelily commented Dec 12, 2016

hello, I come from china, My english not good,I have a question for you, Please help me, Thank you

// login.proto
package login;
syntax = "proto3";

message CheckVersionRequest
{
	int32 game_id = 1;
}
require.config({
    paths: {
        long:'src/protobuf3/node_modules/protobufjs/node_modules/long/dist/long',
        protobuf:'src/protobuf3/node_modules/protobufjs/dist/protobuf',
		messageManager3:'src/protobuf3/messageManager3'
    }
});

require(['protobuf'],
    function(protobuf){
		protobuf.load("/src/protobuf3/login.proto",
						function(err, root)
						{
							if (err) throw err;
							var CheckVersionRequest = root.lookup("login.CheckVersionRequest");	
							var message = CheckVersionRequest.create({game_id:4});
							var buffer = CheckVersionRequest.encode(message).finish();
							cc.log("--------------checkVersionReques=", CheckVersionRequest);
							cc.log("--------------message=", message);
							cc.log("--------------buffer=", buffer, "byteLength=", buffer.byteLength);

						}
					)
    }
);

why buffer is empty?

@dcodeIO
Copy link
Member Author

dcodeIO commented Dec 13, 2016

It's now possible to generate TypeScript definitions from static modules.

@dcodeIO
Copy link
Member Author

dcodeIO commented Dec 13, 2016

The static runtime is now also available for browser usage. It works with statically generated modules only - at 5.4kb gzipped.

@dcodeIO
Copy link
Member Author

dcodeIO commented Dec 15, 2016

@chuanglovelily Sorry, I looked over it briefly, but I cannot identify the issue. Consider creating a dedicated issue if it still isn't working with current master (this issue thread appears to be the wrong place to me).

@dcodeIO
Copy link
Member Author

dcodeIO commented Dec 16, 2016

Most of the things I wanted to do are in now, so I am closing this for now.

@dcodeIO dcodeIO closed this as completed Dec 16, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants