npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

minipbjs

v0.4.4

Published

minimizes javascript created by pbjs for code size reduction

Downloads

26

Readme

minipbjs npm

English | 中文

minipbjs is a command line tool based on pbjs from protobuf.js project, which targets on minimizing the generated javascript code while keeping the same functionality. Compatible to Node.js, browsers, wechat miniprogram and other instant-games. Making it perfectly suitable for the scenarios like wechat miniprogram which requires code size restrictions.

minipbjs currently supports:

  • [x] message
    • constructor
    • create
    • encode / encodeDelimited
    • decode / decodeDelimited
    • verify
    • fromObject
    • toObject
    • toJSON
  • [x] service
    • constructor
    • create
  • [x] enum
  • [ ] oneof (PRs are appreciated! 🎉)

Installation & Example

installing

# install `minipbjs` globally with option -g
$> npm install minipbjs -g

Generating Javascript code

# With same usage with pbjs
$> minipbjs --keep-case # Keeps field casing instead of converting to camel case.
            --root PB   # Specifies an alternative protobuf.roots name.
            # Adds a directory to the include path. Following with target protofiles
            --path /path/to/protofiles a.proto b.proto c.proto ...
            # Notice: --out option in pbjs refers to a file, but it refers to a dir in minipbjs
            --out /path/to/protobuf-bundles/
            # Additional options in minipbjs:
            #   --name Specifies the output Javascript filename,'protobuf-bundles' by default.
            --name protobuf-bundles-mini

# Automatically generates Javascript files specified with --name options.
# Along with the uglified min.js.
$> ls -al /path/to/protobuf-bundles/
 > -rw-r--r--   1 mustime staff  xxxx  protobuf-bundles-mini.js
 > -rw-r--r--   1 mustime staff  xxxx  protobuf-bundles-mini.min.js
 

Using in program

// require the necessary protobuf-library first
require('protobuf-library-minimal.js');
require('protobuf-bundles-mini.js');

// a simple test(PB variable is specified by `--root PB` option)
var payload = { 'a': 1, 'b': 'test' };
var foo = PB.foo.Foo.create(payload);
var bytes = PB.foo.Foo.encode(foo).finish();
var foo2 = PB.foo.Foo.decode(bytes);
var foo3 = PB.foo.Foo.fromObject(PB.foo.Foo.toObject(foo2));

// they should print the same
console.log(foo.a, foo2.a, foo3.a);
console.log(foo.b, foo2.b, foo3.b);

// see futher tests under 'minipbjs/tests'

Comparison with pbjs

Under the hood

The Javascript static code generated by pbjs --target static contains function stubs like constructor, create, encode, decode, fromObject and toObject, etc. These function stubs are generated individually for each message. Which means that these function stubs are distinguish to each other and unlikely be able to share with other messages. Normally we have to strip some less frequently used functionality like create/fromObject/toObject by --no-create/--no-convert, etc., but the resulting min.js is usually remarkable huge.

However, minipbjs pulls basic info from each message(id, name, default value, options, etc.) into a single map. And providing all major functionalities as shared implements for all messages. No need to care about the negligible code size anymore.

Comparison

Take my recent project as example, which is a wechat minigame contains more than 2500 messages. On my 2019 RMBP-15, it costs more than 40s to run pbjs --target static, come up with a 5.2+m min.js file. Meanwhile, running with minipbjs(remove --target static option as well, or minipbjs will perform the same as pbjs) costs only 1.6s, come up with a 160+kB min.js.

Liscense

This project is liscensed under The MIT Liscense.

Enjoy. Let me know if you have any suggestion or encountering with any problem : ).