Skip to content

Commit 6a38fce

Browse files
committed
Add ts_flat_file implemented using esbuild automation
1 parent 7cd67b5 commit 6a38fce

File tree

7 files changed

+1690
-7
lines changed

7 files changed

+1690
-7
lines changed

include/flatbuffers/idl.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,7 @@ struct IDLOptions {
632632
bool json_nested_flatbuffers;
633633
bool json_nested_flexbuffers;
634634
bool json_nested_legacy_flatbuffers;
635+
bool ts_flat_file;
635636
bool ts_entry_points;
636637
bool no_leak_private_annotations;
637638

@@ -732,6 +733,7 @@ struct IDLOptions {
732733
json_nested_flatbuffers(true),
733734
json_nested_flexbuffers(true),
734735
json_nested_legacy_flatbuffers(false),
736+
ts_flat_file(false),
735737
ts_entry_points(false),
736738
no_leak_private_annotations(false),
737739
mini_reflect(IDLOptions::kNone),

src/flatc.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,10 @@ const static FlatCOption options[] = {
217217
{ "", "json-nested-bytes", "",
218218
"Allow a nested_flatbuffer field to be parsed as a vector of bytes"
219219
"in JSON, which is unsafe unless checked by a verifier afterwards." },
220-
{ "", "ts-flat-files", "",
221-
"Only generated one typescript file per .fbs file." },
220+
{ "", "ts_flat_file", "",
221+
"Generate a single typescript file per .fbs file. Implies ts_entry_points." },
222+
{ "", "ts_entry_points", "",
223+
"Generate entry point typescript per namespace. Implies gen-all." },
222224
{ "", "annotate", "SCHEMA",
223225
"Annotate the provided BINARY_FILE with the specified SCHEMA file." },
224226
{ "", "no-leak-private-annotation", "",
@@ -597,6 +599,10 @@ int FlatCompiler::Compile(int argc, const char **argv) {
597599
opts.cs_global_alias = true;
598600
} else if (arg == "--json-nested-bytes") {
599601
opts.json_nested_legacy_flatbuffers = true;
602+
} else if (arg == "--ts_flat_file") {
603+
opts.ts_flat_file = true;
604+
opts.ts_entry_points = true;
605+
opts.generate_all = true;
600606
} else if (arg == "--ts_entry_points") {
601607
opts.ts_entry_points = true;
602608
opts.generate_all = true;

src/idl_gen_ts.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ class TsGenerator : public BaseGenerator {
109109
generateEnums();
110110
generateStructs();
111111
generateEntry();
112+
generateBundle();
112113
return true;
113114
}
114115

@@ -271,6 +272,33 @@ class TsGenerator : public BaseGenerator {
271272
}
272273
}
273274

275+
void generateBundle() {
276+
if (parser_.opts.ts_flat_file) {
277+
std::string inputpath;
278+
std::string symbolic_name = file_name_;
279+
if (parser_.current_namespace_->components.size() > 0) {
280+
std::string path = namer_.Directories(*parser_.current_namespace_,
281+
SkipDir::OutputPathAndTrailingPathSeparator);
282+
inputpath = path + ".ts";
283+
symbolic_name = parser_.current_namespace_->components.back();
284+
} else {
285+
inputpath = file_name_ + ".ts";
286+
}
287+
const std::string bundlepath =
288+
GeneratedFileName(path_, file_name_, parser_.opts);
289+
std::string cmd = "esbuild";
290+
cmd += " ";
291+
cmd += inputpath;
292+
cmd += " --minify";
293+
cmd += " --bundle --outfile=";
294+
cmd += bundlepath;
295+
cmd += " --global-name=";
296+
cmd += symbolic_name;
297+
cmd += " --external:flatbuffers";
298+
system(cmd.c_str());
299+
}
300+
}
301+
274302
// Generate a documentation comment, if available.
275303
static void GenDocComment(const std::vector<std::string> &dc,
276304
std::string *code_ptr,

tests/ts/TypeScriptTest.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def flatc(options, schema, prefix=None, include=None, data=None, cwd=tests_path)
6161

6262
print("Invoking flatc...")
6363
flatc(
64-
options=["--ts", "--reflect-names", "--gen-name-strings", "--gen-mutable", "--gen-object-api", '--ts_entry_points'],
64+
options=["--ts", "--reflect-names", "--gen-name-strings", "--gen-mutable", "--gen-object-api", '--ts_entry_points', '--ts_flat_file'],
6565
schema="../monster_test.fbs",
6666
include="../include_test",
6767
)
@@ -85,7 +85,7 @@ def flatc(options, schema, prefix=None, include=None, data=None, cwd=tests_path)
8585
)
8686

8787
flatc(
88-
options=["--ts", "--reflect-names", "--gen-name-strings", "--gen-mutable", "--gen-object-api", '--ts_entry_points'],
88+
options=["--ts", "--reflect-names", "--gen-name-strings", "--gen-mutable", "--gen-object-api"],
8989
schema=[
9090
"typescript_keywords.fbs",
9191
"test_dir/typescript_include.fbs",
@@ -102,7 +102,6 @@ def flatc(options, schema, prefix=None, include=None, data=None, cwd=tests_path)
102102
"--gen-name-strings",
103103
"--gen-mutable",
104104
"--gen-object-api",
105-
"--ts_entry_points"
106105
],
107106
schema=[
108107
"typescript_keywords.fbs",

tests/ts/monster_test_generated.ts

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

tests/ts/reflection.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ export { Schema } from './reflection/schema.js';
44
export { SchemaFile } from './reflection/schema-file.js';
55
export { Service } from './reflection/service.js';
66
export { RPCCall } from './reflection/rpccall.js';
7-
export { Field } from './reflection/field.js';
87
export { Object } from './reflection/object.js';
8+
export { Enum } from './reflection/enum.js';
99
export { EnumVal } from './reflection/enum-val.js';
1010
export { KeyValue } from './reflection/key-value.js';
11-
export { Enum } from './reflection/enum.js';
11+
export { Field } from './reflection/field.js';
1212
export { Type } from './reflection/type.js';
1313
export { AdvancedFeatures } from './reflection/advanced-features.js';
1414
export { BaseType } from './reflection/base-type.js';

0 commit comments

Comments
 (0)