Skip to content

Commit

Permalink
Add ts_flat_file implemented using esbuild automation
Browse files Browse the repository at this point in the history
  • Loading branch information
bjornharrtell committed Oct 4, 2022
1 parent 7cd67b5 commit 6a38fce
Show file tree
Hide file tree
Showing 7 changed files with 1,690 additions and 7 deletions.
2 changes: 2 additions & 0 deletions include/flatbuffers/idl.h
Expand Up @@ -632,6 +632,7 @@ struct IDLOptions {
bool json_nested_flatbuffers;
bool json_nested_flexbuffers;
bool json_nested_legacy_flatbuffers;
bool ts_flat_file;
bool ts_entry_points;
bool no_leak_private_annotations;

Expand Down Expand Up @@ -732,6 +733,7 @@ struct IDLOptions {
json_nested_flatbuffers(true),
json_nested_flexbuffers(true),
json_nested_legacy_flatbuffers(false),
ts_flat_file(false),
ts_entry_points(false),
no_leak_private_annotations(false),
mini_reflect(IDLOptions::kNone),
Expand Down
10 changes: 8 additions & 2 deletions src/flatc.cpp
Expand Up @@ -217,8 +217,10 @@ const static FlatCOption options[] = {
{ "", "json-nested-bytes", "",
"Allow a nested_flatbuffer field to be parsed as a vector of bytes"
"in JSON, which is unsafe unless checked by a verifier afterwards." },
{ "", "ts-flat-files", "",
"Only generated one typescript file per .fbs file." },
{ "", "ts_flat_file", "",
"Generate a single typescript file per .fbs file. Implies ts_entry_points." },
{ "", "ts_entry_points", "",
"Generate entry point typescript per namespace. Implies gen-all." },
{ "", "annotate", "SCHEMA",
"Annotate the provided BINARY_FILE with the specified SCHEMA file." },
{ "", "no-leak-private-annotation", "",
Expand Down Expand Up @@ -597,6 +599,10 @@ int FlatCompiler::Compile(int argc, const char **argv) {
opts.cs_global_alias = true;
} else if (arg == "--json-nested-bytes") {
opts.json_nested_legacy_flatbuffers = true;
} else if (arg == "--ts_flat_file") {
opts.ts_flat_file = true;
opts.ts_entry_points = true;
opts.generate_all = true;
} else if (arg == "--ts_entry_points") {
opts.ts_entry_points = true;
opts.generate_all = true;
Expand Down
28 changes: 28 additions & 0 deletions src/idl_gen_ts.cpp
Expand Up @@ -109,6 +109,7 @@ class TsGenerator : public BaseGenerator {
generateEnums();
generateStructs();
generateEntry();
generateBundle();
return true;
}

Expand Down Expand Up @@ -271,6 +272,33 @@ class TsGenerator : public BaseGenerator {
}
}

void generateBundle() {
if (parser_.opts.ts_flat_file) {
std::string inputpath;
std::string symbolic_name = file_name_;
if (parser_.current_namespace_->components.size() > 0) {
std::string path = namer_.Directories(*parser_.current_namespace_,
SkipDir::OutputPathAndTrailingPathSeparator);
inputpath = path + ".ts";
symbolic_name = parser_.current_namespace_->components.back();
} else {
inputpath = file_name_ + ".ts";
}
const std::string bundlepath =
GeneratedFileName(path_, file_name_, parser_.opts);
std::string cmd = "esbuild";
cmd += " ";
cmd += inputpath;
cmd += " --minify";
cmd += " --bundle --outfile=";
cmd += bundlepath;
cmd += " --global-name=";
cmd += symbolic_name;
cmd += " --external:flatbuffers";
system(cmd.c_str());
}
}

// Generate a documentation comment, if available.
static void GenDocComment(const std::vector<std::string> &dc,
std::string *code_ptr,
Expand Down
5 changes: 2 additions & 3 deletions tests/ts/TypeScriptTest.py
Expand Up @@ -61,7 +61,7 @@ def flatc(options, schema, prefix=None, include=None, data=None, cwd=tests_path)

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

flatc(
options=["--ts", "--reflect-names", "--gen-name-strings", "--gen-mutable", "--gen-object-api", '--ts_entry_points'],
options=["--ts", "--reflect-names", "--gen-name-strings", "--gen-mutable", "--gen-object-api"],
schema=[
"typescript_keywords.fbs",
"test_dir/typescript_include.fbs",
Expand All @@ -102,7 +102,6 @@ def flatc(options, schema, prefix=None, include=None, data=None, cwd=tests_path)
"--gen-name-strings",
"--gen-mutable",
"--gen-object-api",
"--ts_entry_points"
],
schema=[
"typescript_keywords.fbs",
Expand Down
1 change: 1 addition & 0 deletions tests/ts/monster_test_generated.ts

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions tests/ts/reflection.ts
Expand Up @@ -4,11 +4,11 @@ export { Schema } from './reflection/schema.js';
export { SchemaFile } from './reflection/schema-file.js';
export { Service } from './reflection/service.js';
export { RPCCall } from './reflection/rpccall.js';
export { Field } from './reflection/field.js';
export { Object } from './reflection/object.js';
export { Enum } from './reflection/enum.js';
export { EnumVal } from './reflection/enum-val.js';
export { KeyValue } from './reflection/key-value.js';
export { Enum } from './reflection/enum.js';
export { Field } from './reflection/field.js';
export { Type } from './reflection/type.js';
export { AdvancedFeatures } from './reflection/advanced-features.js';
export { BaseType } from './reflection/base-type.js';

0 comments on commit 6a38fce

Please sign in to comment.