mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-08 22:20:56 +00:00
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.
This commit is contained in:
committed by
Wouter van Oortmerssen
parent
0848f58cdd
commit
b4ca4d3cde
@@ -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";
|
||||
|
||||
Reference in New Issue
Block a user