Skip to content

Commit

Permalink
Added simple CommonJS and AMD wrappers, see #540; Refactored json/sta…
Browse files Browse the repository at this point in the history
…tic-module targets to use common wrappers
  • Loading branch information
dcodeIO committed Dec 11, 2016
1 parent 5f07302 commit b8bce03
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 22 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,13 @@ Consolidates imports and converts between file formats.
-o, --out Saves to a file instead of writing to stdout.
-w, --wrap Specifies an alternative wrapper for any *-module target.
-w, --wrap Specifies the wrapper to use for *-module targets. Also accepts a path.
default Default wrapper supporting both CommonJS and AMD
commonjs CommonJS only wrapper
amd AMD only wrapper
-r, --root Specifies an alternative protobuf.roots name for *-module targets.
usage: pbjs [options] file1.proto file2.json ...
```
Expand Down
10 changes: 7 additions & 3 deletions cli/pbjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ exports.main = function(args) {
paths = typeof argv.path === 'string' ? [ argv.path ] : argv.path || [];

if (!files.length) {
var descriptions = Object.keys(targets).filter(function(key) { return !targets[key].private; }).map(function(key) {
var descs = Object.keys(targets).filter(function(key) { return !targets[key].private; }).map(function(key) {
return " " + util.pad(key, 14, true) + targets[key].description;
});
console.log([
Expand All @@ -41,13 +41,17 @@ exports.main = function(args) {
"",
" -t, --target Specifies the target format. Also accepts a path to require a custom target.",
"",
descriptions.join('\n'),
descs.join('\n'),
"",
" -p, --path Adds a directory to the include path.",
"",
" -o, --out Saves to a file instead of writing to stdout.",
"",
" -w, --wrap Specifies an alternative wrapper for *-module targets.",
" -w, --wrap Specifies the wrapper to use for *-module targets. Also accepts a path.",
"",
" default Default wrapper supporting both CommonJS and AMD",
" commonjs CommonJS only wrapper",
" amd AMD only wrapper",
"",
" -r, --root Specifies an alternative protobuf.roots name for *-module targets.",
"",
Expand Down
5 changes: 3 additions & 2 deletions cli/targets/json-module.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ var protobuf = require("../..");
json_module.description = "JSON representation as a module (AMD, CommonJS, global)"

function json_module(root, options, callback) {

try {
var output = JSON.stringify(root, null, 2).replace(/^(?!$)/mg, " ").trim();
output = util.wrap(options.wrap || "json-module", output, options.root);
var output = "var $root = protobuf.Root.fromJSON(" + JSON.stringify(root, null, 2).replace(/^(?!$)/mg, " ").trim() + ");";
output = util.wrap(options.wrap || "default", output, options.root);
process.nextTick(function() {
callback(null, output);
});
Expand Down
2 changes: 1 addition & 1 deletion cli/targets/static-module.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function static_module_target(root, options, callback) {
if (err)
return callback(err);
try {
output = util.wrap(options.wrap || "static-module", output, options.root);
output = util.wrap(options.wrap || "default", output, options.root);
} catch (e) {
callback(e);
return;
Expand Down
9 changes: 9 additions & 0 deletions cli/wrappers/amd.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
define(["protobuf"], function($protobuf) {
"use strict"; // eslint-disable-line strict

%OUTPUT%

$protobuf.roots[%ROOT%] = $root;

return $root;
});
9 changes: 9 additions & 0 deletions cli/wrappers/commonjs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"use strict"; // eslint-disable-line strict

var $protobuf = require("protobufjs/runtime");

%OUTPUT%

$protobuf.roots[%ROOT%] = $root;

module.exports = $root;
File renamed without changes.
15 changes: 0 additions & 15 deletions cli/wrappers/json-module.js

This file was deleted.

0 comments on commit b8bce03

Please sign in to comment.