Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TS/JS] Entry point per namespace and reworked 1.x compatible single file build #7510

Merged
merged 51 commits into from Jan 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
e93f88b
[TS/JS] Entry point per namespace
bjornharrtell Oct 4, 2022
72cae79
Fix handling of outputpath and array_test
bjornharrtell Nov 12, 2022
b4a17f5
Attempt to fix generate_code
bjornharrtell Nov 12, 2022
0d236af
Merge branch 'master' into ns-entry-points2
bjornharrtell Nov 12, 2022
fa26bdf
Fix cwd for ts in generate_code
bjornharrtell Nov 14, 2022
95a7519
Merge branch 'master' into ns-entry-points
bjornharrtell Nov 14, 2022
5ade15a
Attempt to fixup bazel and some docs
bjornharrtell Nov 14, 2022
4a4e1db
Add --ts-flat-files to bazel build to get bundle
bjornharrtell Nov 14, 2022
a1e8d8f
Move to DEFAULT_FLATC_TS_ARGS
bjornharrtell Nov 14, 2022
c58e85c
Attempt to add esbuild
bjornharrtell Nov 14, 2022
70e7d21
Attempt to use npm instead
bjornharrtell Nov 14, 2022
784ffdd
Remove futile attempt to add esbuild
bjornharrtell Nov 14, 2022
03b58f1
Attempt to as bazel esbuild
bjornharrtell Nov 14, 2022
f31b93c
Shuffle
bjornharrtell Nov 14, 2022
a2c1457
Upgrade bazel deps
bjornharrtell Nov 14, 2022
dabc775
Revert failed attempts to get bazel working
bjornharrtell Nov 14, 2022
029cf87
Ignore flatc tests for now
bjornharrtell Nov 14, 2022
25a36a5
Merge branch 'master' into ns-entry-points2
bjornharrtell Nov 23, 2022
224c9c2
Merge branch 'master' into ns-entry-points
dbaileychess Nov 23, 2022
4dabc0e
Merge branch 'master' into ns-entry-points
dbaileychess Nov 29, 2022
1a99c1c
Merge branch 'master' into ns-entry-points
dbaileychess Dec 1, 2022
92ac29b
Add esbuild dependency
dbaileychess Dec 1, 2022
307dff8
`package.json` Include esbuild
dbaileychess Dec 1, 2022
2d52888
`WORKSPACE` Add fetching esbuild binary
dbaileychess Dec 1, 2022
6e35f16
Update WORKSPACE
dbaileychess Dec 1, 2022
3a61085
Unfreeze Lockfile
dbaileychess Dec 1, 2022
a336904
Update WORKSPACE
dbaileychess Dec 1, 2022
2ed44a3
Update BUILD.bazel
dbaileychess Dec 1, 2022
0905de7
Merge branch 'master' into ns-entry-points
bjornharrtell Dec 10, 2022
2ef1502
Rework to suggest instead of running external bundler
bjornharrtell Dec 10, 2022
57a8cfb
Add esbuild generation to test script
bjornharrtell Dec 10, 2022
de00f0b
Prelim bundle test
bjornharrtell Dec 10, 2022
8704e69
Run test JavaScriptTest from flatbuffers 1.x
bjornharrtell Dec 10, 2022
6441b02
Deps upgrade
bjornharrtell Dec 10, 2022
595bf4f
Clang format fix
bjornharrtell Dec 10, 2022
11271d4
Revert bazel changes
bjornharrtell Dec 10, 2022
63b28c6
Fix newline
bjornharrtell Dec 10, 2022
d558a58
Merge branch 'master' into ns-entry-points
dbaileychess Dec 13, 2022
3de95e2
Merge branch 'master' into ns-entry-points
bjornharrtell Dec 23, 2022
4aca077
Generate with type declarations
bjornharrtell Dec 23, 2022
74e136e
Handle "empty" root namespace
bjornharrtell Jan 11, 2023
7ac6be5
Adjust tests for typescript_keywords.ts
bjornharrtell Jan 11, 2023
1b3e01f
Merge branch 'master' into ns-entry-points
bjornharrtell Jan 12, 2023
6fafede
Separate test procedure for old node resolution module output
bjornharrtell Jan 12, 2023
6c07d62
Fix rel path for root level re-exports
bjornharrtell Jan 12, 2023
5f28a53
Bazel support for esbuild-based flatc
jkuszmaul Dec 18, 2022
96ddf2c
Try to fix bazel build for when node isn't present on host
jkuszmaul Jan 15, 2023
027bc8b
Auto formatting fixes
bjornharrtell Jan 16, 2023
9ffa2ac
Fix missing generated code
bjornharrtell Jan 16, 2023
2ae545b
Merge branch 'master' into ns-entry-points
bjornharrtell Jan 21, 2023
5ee0c9f
Merge branch 'master' into ns-entry-points
dbaileychess Jan 21, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 11 additions & 7 deletions .github/workflows/build.yml
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
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
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
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
@@ -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
@@ -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
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
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
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
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
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;
dbaileychess marked this conversation as resolved.
Show resolved Hide resolved
} 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