Skip to content

Commit b8bce03

Browse files
committed
Added simple CommonJS and AMD wrappers, see #540; Refactored json/static-module targets to use common wrappers
1 parent 5f07302 commit b8bce03

File tree

8 files changed

+36
-22
lines changed

8 files changed

+36
-22
lines changed

README.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,13 @@ Consolidates imports and converts between file formats.
342342
343343
-o, --out Saves to a file instead of writing to stdout.
344344
345-
-w, --wrap Specifies an alternative wrapper for any *-module target.
345+
-w, --wrap Specifies the wrapper to use for *-module targets. Also accepts a path.
346+
347+
default Default wrapper supporting both CommonJS and AMD
348+
commonjs CommonJS only wrapper
349+
amd AMD only wrapper
350+
351+
-r, --root Specifies an alternative protobuf.roots name for *-module targets.
346352
347353
usage: pbjs [options] file1.proto file2.json ...
348354
```

cli/pbjs.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ exports.main = function(args) {
3131
paths = typeof argv.path === 'string' ? [ argv.path ] : argv.path || [];
3232

3333
if (!files.length) {
34-
var descriptions = Object.keys(targets).filter(function(key) { return !targets[key].private; }).map(function(key) {
34+
var descs = Object.keys(targets).filter(function(key) { return !targets[key].private; }).map(function(key) {
3535
return " " + util.pad(key, 14, true) + targets[key].description;
3636
});
3737
console.log([
@@ -41,13 +41,17 @@ exports.main = function(args) {
4141
"",
4242
" -t, --target Specifies the target format. Also accepts a path to require a custom target.",
4343
"",
44-
descriptions.join('\n'),
44+
descs.join('\n'),
4545
"",
4646
" -p, --path Adds a directory to the include path.",
4747
"",
4848
" -o, --out Saves to a file instead of writing to stdout.",
4949
"",
50-
" -w, --wrap Specifies an alternative wrapper for *-module targets.",
50+
" -w, --wrap Specifies the wrapper to use for *-module targets. Also accepts a path.",
51+
"",
52+
" default Default wrapper supporting both CommonJS and AMD",
53+
" commonjs CommonJS only wrapper",
54+
" amd AMD only wrapper",
5155
"",
5256
" -r, --root Specifies an alternative protobuf.roots name for *-module targets.",
5357
"",

cli/targets/json-module.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ var protobuf = require("../..");
1010
json_module.description = "JSON representation as a module (AMD, CommonJS, global)"
1111

1212
function json_module(root, options, callback) {
13+
1314
try {
14-
var output = JSON.stringify(root, null, 2).replace(/^(?!$)/mg, " ").trim();
15-
output = util.wrap(options.wrap || "json-module", output, options.root);
15+
var output = "var $root = protobuf.Root.fromJSON(" + JSON.stringify(root, null, 2).replace(/^(?!$)/mg, " ").trim() + ");";
16+
output = util.wrap(options.wrap || "default", output, options.root);
1617
process.nextTick(function() {
1718
callback(null, output);
1819
});

cli/targets/static-module.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ function static_module_target(root, options, callback) {
1919
if (err)
2020
return callback(err);
2121
try {
22-
output = util.wrap(options.wrap || "static-module", output, options.root);
22+
output = util.wrap(options.wrap || "default", output, options.root);
2323
} catch (e) {
2424
callback(e);
2525
return;

cli/wrappers/amd.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
define(["protobuf"], function($protobuf) {
2+
"use strict"; // eslint-disable-line strict
3+
4+
%OUTPUT%
5+
6+
$protobuf.roots[%ROOT%] = $root;
7+
8+
return $root;
9+
});

cli/wrappers/commonjs.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
"use strict"; // eslint-disable-line strict
2+
3+
var $protobuf = require("protobufjs/runtime");
4+
5+
%OUTPUT%
6+
7+
$protobuf.roots[%ROOT%] = $root;
8+
9+
module.exports = $root;
File renamed without changes.

cli/wrappers/json-module.js

-15
This file was deleted.

0 commit comments

Comments
 (0)