diff --git a/README.md b/README.md index 2ee1e9e2b..4f9d2f128 100644 --- a/README.md +++ b/README.md @@ -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 ... ``` diff --git a/cli/pbjs.js b/cli/pbjs.js index e4e4dc09e..3382954f8 100644 --- a/cli/pbjs.js +++ b/cli/pbjs.js @@ -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([ @@ -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.", "", diff --git a/cli/targets/json-module.js b/cli/targets/json-module.js index f5ef38ca5..122be8614 100644 --- a/cli/targets/json-module.js +++ b/cli/targets/json-module.js @@ -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); }); diff --git a/cli/targets/static-module.js b/cli/targets/static-module.js index 934daf370..d2f5f164b 100644 --- a/cli/targets/static-module.js +++ b/cli/targets/static-module.js @@ -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; diff --git a/cli/wrappers/amd.js b/cli/wrappers/amd.js new file mode 100644 index 000000000..2df098f04 --- /dev/null +++ b/cli/wrappers/amd.js @@ -0,0 +1,9 @@ +define(["protobuf"], function($protobuf) { + "use strict"; // eslint-disable-line strict + + %OUTPUT% + + $protobuf.roots[%ROOT%] = $root; + + return $root; +}); diff --git a/cli/wrappers/commonjs.js b/cli/wrappers/commonjs.js new file mode 100644 index 000000000..38835b81c --- /dev/null +++ b/cli/wrappers/commonjs.js @@ -0,0 +1,9 @@ +"use strict"; // eslint-disable-line strict + +var $protobuf = require("protobufjs/runtime"); + +%OUTPUT% + +$protobuf.roots[%ROOT%] = $root; + +module.exports = $root; diff --git a/cli/wrappers/static-module.js b/cli/wrappers/default.js similarity index 100% rename from cli/wrappers/static-module.js rename to cli/wrappers/default.js diff --git a/cli/wrappers/json-module.js b/cli/wrappers/json-module.js deleted file mode 100644 index f59c6bf1a..000000000 --- a/cli/wrappers/json-module.js +++ /dev/null @@ -1,15 +0,0 @@ -(function(global, factory) { - /* eslint-disable no-undef */ - - /* AMD */ if (typeof define === 'function' && define.amd) - define(["protobuf"], factory); - - /* CommonJS */ else if (typeof require === 'function' && typeof module === 'object' && module && module.exports) - module.exports = factory(require("protobufjs")); - - /* eslint-enable no-undef */ -})(this, function(protobuf) { - - var $root = protobuf.roots[%ROOT%] = protobuf.Root.fromJSON(%OUTPUT%); - return $root; -});