Skip to content

Add cli option to disable bundling #1461

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

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions cli/pbjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ exports.main = function main(args, callback) {
"force-message": "strict-message"
},
string: [ "target", "out", "path", "wrap", "dependency", "root", "lint" ],
boolean: [ "create", "encode", "decode", "verify", "convert", "delimited", "beautify", "comments", "es6", "sparse", "keep-case", "force-long", "force-number", "force-enum-string", "force-message" ],
boolean: [ "create", "encode", "decode", "verify", "convert", "delimited", "beautify", "comments", "es6", "sparse", "keep-case", "force-long", "force-number", "force-enum-string", "force-message", "bundle" ],
default: {
target: "json",
create: true,
Expand All @@ -55,6 +55,7 @@ exports.main = function main(args, callback) {
delimited: true,
beautify: true,
comments: true,
bundle: true,
es6: null,
lint: lintDefault,
"keep-case": false,
Expand Down Expand Up @@ -99,7 +100,7 @@ exports.main = function main(args, callback) {
"",
" -o, --out Saves to a file instead of writing to stdout.",
"",
" --sparse Exports only those types referenced from a main file (experimental).",
" --sparse Exports only those types referenced from a main file (experimental). Ignored with --no-bundle.",
"",
chalk.bold.gray(" Module targets only:"),
"",
Expand Down Expand Up @@ -135,12 +136,13 @@ exports.main = function main(args, callback) {
" --no-delimited Does not generate delimited encode/decode functions.",
" --no-beautify Does not beautify generated code.",
" --no-comments Does not output any JSDoc comments.",
" --no-bundle Does not bundle imported protos. Only accepts one input file with this option.",
"",
" --force-long Enfores the use of 'Long' for s-/u-/int64 and s-/fixed64 fields.",
" --force-number Enfores the use of 'number' for s-/u-/int64 and s-/fixed64 fields.",
" --force-message Enfores the use of message instances instead of plain objects.",
" --force-long Enforces the use of 'Long' for s-/u-/int64 and s-/fixed64 fields.",
" --force-number Enforces the use of 'number' for s-/u-/int64 and s-/fixed64 fields.",
" --force-message Enforces the use of message instances instead of plain objects.",
"",
"usage: " + chalk.bold.green("pbjs") + " [options] file1.proto file2.json ..." + chalk.gray(" (or pipe) ") + "other | " + chalk.bold.green("pbjs") + " [options] -",
"usage: " + chalk.bold.green("pbjs") + " [options] file1.proto file2.json ..." + chalk.gray(" (or pipe) ") + "other | " + chalk.bold.green("pbjs") + " [options] - | " + chalk.bold.green("pbjs") + " --no-bundle [options] file.proto",
""
].join("\n"));
return 1;
Expand Down Expand Up @@ -231,9 +233,12 @@ exports.main = function main(args, callback) {

// Load from disk
} else {
if (!argv.bundle && files.length !== 1) {
throw Error("Only one file may be specified with --no-bundle.");
}
try {
root.loadSync(files, parseOptions).resolveAll(); // sync is deterministic while async is not
if (argv.sparse)
if (argv.sparse && argv.bundle)
sparsify(root);
callTarget();
} catch (err) {
Expand Down
39 changes: 28 additions & 11 deletions cli/pbts.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ exports.main = function(args, callback) {
import: "i"
},
string: [ "name", "out", "global", "import" ],
boolean: [ "comments", "main" ],
boolean: [ "comments", "main", "copy-imports" ],
default: {
comments: true,
main: false
main: false,
"copy-imports": false,
}
});

Expand All @@ -46,19 +47,21 @@ exports.main = function(args, callback) {
"",
chalk.bold.white("Generates TypeScript definitions from annotated JavaScript files."),
"",
" -o, --out Saves to a file instead of writing to stdout.",
" -o, --out Saves to a file instead of writing to stdout.",
"",
" -g, --global Name of the global object in browser environments, if any.",
" -g, --global Name of the global object in browser environments, if any.",
"",
" -i, --import Comma delimited list of imports. Local names will equal camelCase of the basename.",
" -i, --import Comma delimited list of imports. Local names will equal camelCase of the basename.",
"",
" --no-comments Does not output any JSDoc comments.",
" --no-copy-imports Copy imports from source. For use with --no-bundle pbjs output. Only accepts one input file with this option.",
"",
" --no-comments Does not output any JSDoc comments.",
"",
chalk.bold.gray(" Internal flags:"),
"",
" -n, --name Wraps everything in a module of the specified name.",
" -n, --name Wraps everything in a module of the specified name.",
"",
" -m, --main Whether building the main library without any imports.",
" -m, --main Whether building the main library without any imports.",
"",
"usage: " + chalk.bold.green("pbts") + " [options] file1.js file2.js ..." + chalk.bold.gray(" (or) ") + "other | " + chalk.bold.green("pbts") + " [options] -",
""
Expand Down Expand Up @@ -93,10 +96,20 @@ exports.main = function(args, callback) {

// Load from disk
} else {
if (!argv.bundle && files.length !== 1) {
throw Error("Only one file may be specified with --copy-imports.");
}
callJsdoc();
}

function callJsdoc() {
var copiedImports = [];

if (argv["copy-imports"]) {
copiedImports = fs.readFileSync(files[0], "utf-8").split("\n").filter(function(line) {
return line.startsWith("import *");
});
}

// There is no proper API for jsdoc, so this executes the CLI and pipes the output
var basedir = path.join(__dirname, ".");
Expand Down Expand Up @@ -162,9 +175,8 @@ exports.main = function(args, callback) {
var importArray = typeof argv.import === "string" ? argv.import.split(",") : argv.import || [];

// Build an object of imports and paths
var imports = {
$protobuf: "protobufjs"
};
var imports = {};

importArray.forEach(function(importItem) {
imports[getImportName(importItem)] = importItem;
});
Expand All @@ -173,6 +185,11 @@ exports.main = function(args, callback) {
Object.keys(imports).forEach(function(key) {
output.push("import * as " + key + " from \"" + imports[key] + "\";");
});

if (copiedImports) {
output = output.concat(copiedImports);
}
output.push("");
}

output = output.join("\n") + "\n" + out.join("");
Expand Down
Loading