summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNathan Mitchell <thecsapprentice.business@gmail.com>2018-05-31 08:29:58 -0700
committerWouter van Oortmerssen <aardappel@gmail.com>2018-05-31 08:29:58 -0700
commitb4ca4d3cdea7dbfb78342514151a495b451a7f6f (patch)
tree9db92264c42fd1f95049a92d9d6481caecd0fb09
parent0848f58cdd848d2353b1c0a070f1f6c932d2458f (diff)
downloadflatbuffers-b4ca4d3cdea7dbfb78342514151a495b451a7f6f.tar.gz
flatbuffers-b4ca4d3cdea7dbfb78342514151a495b451a7f6f.tar.bz2
flatbuffers-b4ca4d3cdea7dbfb78342514151a495b451a7f6f.zip
Javascript: Add suppport for ES6 style exports (#4754)
* Add suppport for ES6 style exports Adds support for ECMAScript 6 module exports during Javascript generation. This is useful as many development projects are switching to this new standard and away from custom module solutions. By integrating support into flatbuffers, users do not need to perform additional post-processing of generated files in order to use flatbuffers output directly in their codebases. Reference to ECMAScript 6 modules: https://www.ecma-international.org/ecma-262/6.0/#sec-exports Changes: * Added `--es6-js-export` option to cli parser tool * Added conditional code to generate a ES6 style export line, replacing the normal NodeJS/RequireJS line. * Fixed missing export statements Added exports for definition and struct names that were not inside namespaces * Updated Compiler.md with new generator option Added entry to Compiler.md in docs for the `--es6-js-export` flag, including a brief description of the effects and usefulness.
-rw-r--r--docs/source/Compiler.md4
-rw-r--r--include/flatbuffers/idl.h2
-rw-r--r--src/flatc.cpp5
-rw-r--r--src/idl_gen_js.cpp11
4 files changed, 21 insertions, 1 deletions
diff --git a/docs/source/Compiler.md b/docs/source/Compiler.md
index 6401c44d..a7302320 100644
--- a/docs/source/Compiler.md
+++ b/docs/source/Compiler.md
@@ -101,6 +101,10 @@ Additional options:
instead of Node.js style exporting. Needed for compatibility with the
Google closure compiler (useful for JS).
+- `--es6-js-export` : Generates ECMAScript v6 style export definitions
+ instead of Node.js style exporting. Useful when integrating flatbuffers
+ with modern Javascript projects.
+
- `--raw-binary` : Allow binaries without a file_indentifier to be read.
This may crash flatc given a mismatched schema.
diff --git a/include/flatbuffers/idl.h b/include/flatbuffers/idl.h
index a40c5c19..a47417de 100644
--- a/include/flatbuffers/idl.h
+++ b/include/flatbuffers/idl.h
@@ -358,6 +358,7 @@ struct IDLOptions {
bool strict_json;
bool skip_js_exports;
bool use_goog_js_export_format;
+ bool use_ES6_js_export_format;
bool output_default_scalars_in_json;
int indent_step;
bool output_enum_identifiers;
@@ -423,6 +424,7 @@ struct IDLOptions {
: strict_json(false),
skip_js_exports(false),
use_goog_js_export_format(false),
+ use_ES6_js_export_format(false),
output_default_scalars_in_json(false),
indent_step(2),
output_enum_identifiers(true),
diff --git a/src/flatc.cpp b/src/flatc.cpp
index c7e1b091..0487a451 100644
--- a/src/flatc.cpp
+++ b/src/flatc.cpp
@@ -97,6 +97,7 @@ std::string FlatCompiler::GetUsageString(const char *program_name) const {
" Default value is \"T\"\n"
" --no-js-exports Removes Node.js style export lines in JS.\n"
" --goog-js-export Uses goog.exports* for closure compiler exporting in JS.\n"
+ " --es6-js-export Uses ECMAScript 6 export style lines in JS.\n"
" --go-namespace Generate the overrided namespace in Golang.\n"
" --go-import Generate the overrided import for flatbuffers in Golang.\n"
" (default is \"github.com/google/flatbuffers/go\")\n"
@@ -191,6 +192,10 @@ int FlatCompiler::Compile(int argc, const char **argv) {
opts.skip_js_exports = true;
} else if (arg == "--goog-js-export") {
opts.use_goog_js_export_format = true;
+ opts.use_ES6_js_export_format = false;
+ } else if (arg == "--es6-js-export") {
+ opts.use_goog_js_export_format = false;
+ opts.use_ES6_js_export_format = true;
} else if (arg == "--go-namespace") {
if (++argi >= argc) Error("missing golang namespace" + arg, true);
opts.go_namespace = argv[argi];
diff --git a/src/idl_gen_js.cpp b/src/idl_gen_js.cpp
index 3e3730ac..184749a8 100644
--- a/src/idl_gen_js.cpp
+++ b/src/idl_gen_js.cpp
@@ -103,7 +103,10 @@ class JsGenerator : public BaseGenerator {
if (lang_.language == IDLOptions::kJs && !exports_code.empty() &&
!parser_.opts.skip_js_exports) {
- code += "// Exports for Node.js and RequireJS\n";
+ if( parser_.opts.use_ES6_js_export_format )
+ code += "// Exports for ECMAScript6 Modules\n";
+ else
+ code += "// Exports for Node.js and RequireJS\n";
code += exports_code;
}
@@ -223,6 +226,8 @@ class JsGenerator : public BaseGenerator {
code += "var ";
if (parser_.opts.use_goog_js_export_format) {
exports += "goog.exportSymbol('" + *it + "', " + *it + ");\n";
+ } else if( parser_.opts.use_ES6_js_export_format){
+ exports += "export {" + *it + "};\n";
} else {
exports += "this." + *it + " = " + *it + ";\n";
}
@@ -293,6 +298,8 @@ class JsGenerator : public BaseGenerator {
if (parser_.opts.use_goog_js_export_format) {
exports += "goog.exportSymbol('" + enum_def.name + "', " +
enum_def.name + ");\n";
+ } else if (parser_.opts.use_ES6_js_export_format) {
+ exports += "export {" + enum_def.name + "};\n";
} else {
exports += "this." + enum_def.name + " = " + enum_def.name + ";\n";
}
@@ -561,6 +568,8 @@ class JsGenerator : public BaseGenerator {
if (parser_.opts.use_goog_js_export_format) {
exports += "goog.exportSymbol('" + struct_def.name + "', " +
struct_def.name + ");\n";
+ } else if (parser_.opts.use_ES6_js_export_format) {
+ exports += "export {" + struct_def.name + "};\n";
} else {
exports +=
"this." + struct_def.name + " = " + struct_def.name + ";\n";