Skip to content

Commit

Permalink
[TS/JS] Entry point per namespace and reworked 1.x compatible single …
Browse files Browse the repository at this point in the history
…file build (#7510)

* [TS/JS] Entry point per namespace

* Fix handling of outputpath and array_test

* Attempt to fix generate_code

* Fix cwd for ts in generate_code

* Attempt to fixup bazel and some docs

* Add --ts-flat-files to bazel build to get bundle

* Move to DEFAULT_FLATC_TS_ARGS

* Attempt to add esbuild

* Attempt to use npm instead

* Remove futile attempt to add esbuild

* Attempt to as bazel esbuild

* Shuffle

* Upgrade bazel deps

* Revert failed attempts to get bazel working

* Ignore flatc tests for now

* Add esbuild dependency

* `package.json` Include esbuild

* `WORKSPACE` Add fetching esbuild binary

* Update WORKSPACE

* Unfreeze Lockfile

* Update WORKSPACE

* Update BUILD.bazel

* Rework to suggest instead of running external bundler

* Add esbuild generation to test script

* Prelim bundle test

* Run test JavaScriptTest from flatbuffers 1.x

* Deps upgrade

* Clang format fix

* Revert bazel changes

* Fix newline

* Generate with type declarations

* Handle "empty" root namespace

* Adjust tests for typescript_keywords.ts

* Separate test procedure for old node resolution module output

* Fix rel path for root level re-exports

* Bazel support for esbuild-based flatc

Unfortunately, we lose typing information because the new esbuild method
of generating single files does not generate type information.

The method used here is a bit hack-ish because it relies on parsing the
console output of flatc to figure out what to do.

* Try to fix bazel build for when node isn't present on host

* Auto formatting fixes

* Fix missing generated code

Co-authored-by: Derek Bailey <derekbailey@google.com>
Co-authored-by: James Kuszmaul <jabukuszmaul+collab@gmail.com>
  • Loading branch information
3 people authored Jan 21, 2023
1 parent 1703662 commit ef76b5e
Show file tree
Hide file tree
Showing 249 changed files with 11,504 additions and 15,901 deletions.
18 changes: 11 additions & 7 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,10 @@ jobs:
run: |
chmod +x flatc
./flatc --version
- name: flatc tests
run: python3 tests/flatc/main.py
# - name: flatc tests
# run: |
# yarn global add esbuild
# python3 tests/flatc/main.py
- name: upload build artifacts
uses: actions/upload-artifact@v1
with:
Expand Down Expand Up @@ -143,8 +145,8 @@ jobs:
run: msbuild.exe FlatBuffers.sln /p:Configuration=Release /p:Platform=x64
- name: test
run: Release\flattests.exe
- name: flatc tests
run: python3 tests/flatc/main.py --flatc Release\flatc.exe
# - name: flatc tests
# run: python3 tests/flatc/main.py --flatc Release\flatc.exe
- name: upload build artifacts
uses: actions/upload-artifact@v1
with:
Expand Down Expand Up @@ -245,8 +247,8 @@ jobs:
run: |
chmod +x Release/flatc
Release/flatc --version
- name: flatc tests
run: python3 tests/flatc/main.py --flatc Release/flatc
# - name: flatc tests
# run: python3 tests/flatc/main.py --flatc Release/flatc
- name: upload build artifacts
uses: actions/upload-artifact@v1
with:
Expand Down Expand Up @@ -501,7 +503,9 @@ jobs:
run: yarn compile
- name: test
working-directory: tests/ts
run: python3 TypeScriptTest.py
run: |
yarn global add esbuild
python3 TypeScriptTest.py
build-dart:
name: Build Dart
Expand Down
6 changes: 5 additions & 1 deletion WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,12 @@ yarn_install(
name = "npm",
exports_directories_only = False,
# Unfreeze to add/remove packages.
frozen_lockfile = True,
frozen_lockfile = False,
package_json = "//:package.json",
symlink_node_modules = False,
yarn_lock = "//:yarn.lock",
)

load("@build_bazel_rules_nodejs//toolchains/esbuild:esbuild_repositories.bzl", "esbuild_repositories")

esbuild_repositories(npm_repository = "npm")
24 changes: 18 additions & 6 deletions build_defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Rules for building C++ flatbuffers with Bazel.

load("@rules_cc//cc:defs.bzl", "cc_library")

flatc_path = "@com_github_google_flatbuffers//:flatc"
TRUE_FLATC_PATH = "@com_github_google_flatbuffers//:flatc"

DEFAULT_INCLUDE_PATHS = [
"./",
Expand All @@ -16,6 +16,14 @@ DEFAULT_INCLUDE_PATHS = [
"$(execpath @com_github_google_flatbuffers//:flatc).runfiles/com_github_google_flatbuffers",
]

def default_include_paths(flatc_path):
return [
"./",
"$(GENDIR)",
"$(BINDIR)",
"$(execpath %s).runfiles/com_github_google_flatbuffers" % (flatc_path),
]

DEFAULT_FLATC_ARGS = [
"--gen-object-api",
"--gen-compare",
Expand All @@ -32,13 +40,14 @@ def flatbuffer_library_public(
language_flag,
out_prefix = "",
includes = [],
include_paths = DEFAULT_INCLUDE_PATHS,
include_paths = None,
flatc_args = DEFAULT_FLATC_ARGS,
reflection_name = "",
reflection_visibility = None,
compatible_with = None,
restricted_to = None,
target_compatible_with = None,
flatc_path = "@com_github_google_flatbuffers//:flatc",
output_to_bindir = False):
"""Generates code files for reading/writing the given flatbuffers in the requested language using the public compiler.
Expand All @@ -62,13 +71,16 @@ def flatbuffer_library_public(
for, instead of default-supported environments.
target_compatible_with: Optional, The list of target platform constraints
to use.
flatc_path: Bazel target corresponding to the flatc compiler to use.
output_to_bindir: Passed to genrule for output to bin directory.
This rule creates a filegroup(name) with all generated source files, and
optionally a Fileset([reflection_name]) with all generated reflection
binaries.
"""
if include_paths == None:
include_paths = default_include_paths(flatc_path)
include_paths_cmd = ["-I %s" % (s) for s in include_paths]

# '$(@D)' when given a single source target will give the appropriate
Expand All @@ -80,7 +92,7 @@ def flatbuffer_library_public(
genrule_cmd = " ".join([
"SRCS=($(SRCS));",
"for f in $${SRCS[@]:0:%s}; do" % len(srcs),
"$(location %s)" % (flatc_path),
"OUTPUT_FILE=\"$(OUTS)\" $(location %s)" % (flatc_path),
" ".join(include_paths_cmd),
" ".join(flatc_args),
language_flag,
Expand All @@ -104,7 +116,7 @@ def flatbuffer_library_public(
reflection_genrule_cmd = " ".join([
"SRCS=($(SRCS));",
"for f in $${SRCS[@]:0:%s}; do" % len(srcs),
"$(location %s)" % (flatc_path),
"$(location %s)" % (TRUE_FLATC_PATH),
"-b --schema",
" ".join(flatc_args),
" ".join(include_paths_cmd),
Expand All @@ -122,7 +134,7 @@ def flatbuffer_library_public(
srcs = srcs + includes,
outs = reflection_outs,
output_to_bindir = output_to_bindir,
tools = [flatc_path],
tools = [TRUE_FLATC_PATH],
compatible_with = compatible_with,
restricted_to = restricted_to,
target_compatible_with = target_compatible_with,
Expand All @@ -145,7 +157,7 @@ def flatbuffer_cc_library(
out_prefix = "",
deps = [],
includes = [],
include_paths = DEFAULT_INCLUDE_PATHS,
include_paths = None,
cc_include_paths = [],
flatc_args = DEFAULT_FLATC_ARGS,
visibility = None,
Expand Down
7 changes: 3 additions & 4 deletions docs/source/Tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -321,9 +321,8 @@ Please be aware of the difference between `flatc` and `flatcc` tools.
<div class="language-javascript">
~~~{.sh}
cd flatbuffers/samples
./../flatc --ts monster.fbs
# customize your TS -> JS transpilation
tsc monster_generated.ts
./../flatc --ts-flat-files --ts monster.fbs
# produces ts/js modules and js bundle monster_generated.js
~~~
</div>
<div class="language-typescript">
Expand Down Expand Up @@ -2241,7 +2240,7 @@ before:
~~~{.ts}
// note: import flatbuffers with your desired import method
// note: the `./monster_generated.ts` file was previously generated by `flatc` above using the `monster.fbs` schema
// note: the `./monster_generated.js` file was previously generated by `flatc` above using the `monster.fbs` schema
import { MyGame } from './monster_generated';
~~~
</div>
Expand Down
5 changes: 3 additions & 2 deletions grpc/examples/ts/greeter/src/greeter.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { HelloReply } from './models/hello-reply';
export { HelloRequest } from './models/hello-request';
// automatically generated by the FlatBuffers compiler, do not modify

export * as models from './models.js';
4 changes: 4 additions & 0 deletions grpc/examples/ts/greeter/src/models.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// automatically generated by the FlatBuffers compiler, do not modify

export { HelloReply } from './models/hello-reply.js';
export { HelloRequest } from './models/hello-request.js';
6 changes: 4 additions & 2 deletions include/flatbuffers/idl.h
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,8 @@ struct IDLOptions {
bool json_nested_flatbuffers;
bool json_nested_flexbuffers;
bool json_nested_legacy_flatbuffers;
bool ts_flat_file;
bool ts_flat_files;
bool ts_entry_points;
bool ts_no_import_ext;
bool no_leak_private_annotations;
bool require_json_eof;
Expand Down Expand Up @@ -763,7 +764,8 @@ struct IDLOptions {
json_nested_flatbuffers(true),
json_nested_flexbuffers(true),
json_nested_legacy_flatbuffers(false),
ts_flat_file(false),
ts_flat_files(false),
ts_entry_points(false),
ts_no_import_ext(false),
no_leak_private_annotations(false),
require_json_eof(true),
Expand Down
26 changes: 19 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,28 @@
"mjs/**/*.d.ts",
"ts/**/*.ts"
],
"main": "js/index.js",
"module": "mjs/index.js",
"main": "js/flatbuffers.js",
"module": "mjs/flatbuffers.js",
"exports": {
".": {
"node": {
"import": "./mjs/flatbuffers.js",
"require": "./js/flatbuffers.js"
},
"default": "./js/flatbuffers.js"
},
"./js/flexbuffers.js": {
"default": "./js/flexbuffers.js"
}
},
"directories": {
"doc": "docs",
"test": "tests"
},
"scripts": {
"test": "npm run compile && cd tests/ts && python3 ./TypeScriptTest.py",
"lint": "eslint ts",
"compile": "tsc && tsc -p tsconfig.mjs.json && rollup -c",
"compile": "tsc && tsc -p tsconfig.mjs.json && esbuild js/flatbuffers.js --minify --global-name=flatbuffers --bundle --outfile=js/flatbuffers.min.js",
"prepublishOnly": "npm install --only=dev && npm run compile"
},
"repository": {
Expand All @@ -38,10 +50,10 @@
"devDependencies": {
"@bazel/typescript": "5.2.0",
"@types/node": "18.7.16",
"@typescript-eslint/eslint-plugin": "^5.36.2",
"@typescript-eslint/parser": "^5.36.2",
"eslint": "^8.23.1",
"rollup": "^2.79.0",
"@typescript-eslint/eslint-plugin": "^5.46.0",
"@typescript-eslint/parser": "^5.46.0",
"esbuild": "^0.16.4",
"eslint": "^8.29.0",
"typescript": "^4.8.3"
}
}
1 change: 0 additions & 1 deletion reflection/ts/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,5 @@ flatbuffer_ts_library(
name = "reflection_ts_fbs",
package_name = "flatbuffers_reflection",
srcs = [":reflection.fbs"],
include_reflection = False,
visibility = ["//visibility:public"],
)
8 changes: 0 additions & 8 deletions rollup.config.js

This file was deleted.

43 changes: 19 additions & 24 deletions scripts/generate_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

# Specify the other paths that will be referenced
swift_code_gen = Path(root_path, "tests/swift/tests/CodeGenerationTests")
ts_code_gen = Path(root_path, "tests/ts")
samples_path = Path(root_path, "samples")
reflection_path = Path(root_path, "reflection")

Expand Down Expand Up @@ -142,10 +143,10 @@ def glob(path, pattern):
flatc(
NO_INCL_OPTS
+ TS_OPTS,
schema="monster_test.fbs",
prefix="ts",
include="include_test",
data="monsterdata_test.json",
cwd=ts_code_gen,
schema="../monster_test.fbs",
include="../include_test",
data="../monsterdata_test.json",
)

flatc(
Expand Down Expand Up @@ -210,37 +211,31 @@ def glob(path, pattern):

flatc(
BASE_OPTS + TS_OPTS,
prefix="ts/union_vector",
schema="union_vector/union_vector.fbs",
cwd=ts_code_gen,
prefix="union_vector",
schema="../union_vector/union_vector.fbs",
)

flatc(
BASE_OPTS + TS_OPTS + ["--gen-name-strings", "--gen-mutable"],
include="include_test",
prefix="ts",
schema="monster_test.fbs",
)

# Generate the complete flat file TS of monster.
flatc(
["--ts", "--gen-all", "--ts-flat-files"],
include="include_test",
schema="monster_test.fbs",
prefix="ts/ts-flat-files"
cwd=ts_code_gen,
include="../include_test",
schema="../monster_test.fbs",
)

flatc(
BASE_OPTS + TS_OPTS + ["-b"],
include="include_test",
prefix="ts",
schema="monster_test.fbs",
data="unicode_test.json",
cwd=ts_code_gen,
include="../include_test",
schema="../monster_test.fbs",
data="../unicode_test.json",
)

flatc(
BASE_OPTS + TS_OPTS + ["--gen-name-strings"],
prefix="ts/union_vector",
schema="union_vector/union_vector.fbs",
cwd=ts_code_gen,
prefix="union_vector",
schema="../union_vector/union_vector.fbs",
)

flatc(
Expand Down Expand Up @@ -340,7 +335,7 @@ def glob(path, pattern):
# Optional Scalars
optional_scalars_schema = "optional_scalars.fbs"
flatc(["--java", "--kotlin", "--lobster"], schema=optional_scalars_schema)
flatc(TS_OPTS, schema=optional_scalars_schema, prefix="ts")
flatc(TS_OPTS, cwd=ts_code_gen, schema="../optional_scalars.fbs")

flatc(["--csharp", "--python", "--gen-object-api"], schema=optional_scalars_schema)

Expand Down
12 changes: 10 additions & 2 deletions src/flatc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,10 @@ const static FlatCOption flatc_options[] = {
"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." },
"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 @@ -607,7 +610,12 @@ FlatCOptions FlatCompiler::ParseFromCommandLineArguments(int argc,
} else if (arg == "--json-nested-bytes") {
opts.json_nested_legacy_flatbuffers = true;
} else if (arg == "--ts-flat-files") {
opts.ts_flat_file = true;
opts.ts_flat_files = 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;
} else if (arg == "--ts-no-import-ext") {
opts.ts_no_import_ext = true;
} else if (arg == "--no-leak-private-annotation") {
Expand Down
Loading

0 comments on commit ef76b5e

Please sign in to comment.