Commit eaddc4f8 authored by Giovanni Ortuño Urquidi's avatar Giovanni Ortuño Urquidi Committed by Commit Bot

mojo-ts: Generate tsconfig.json files for targets

tsconfig.json files are similar to BUILD files. We can use them to
specify the list of files to compile as well as compiler options.

We'll need them later when we add support for depending on other
mojo targets.

Sample generated tsconfig.json

{
  "compilerOptions": {
    "lib": [
      "es6",
      "esnext.bigint"
    ],
    "module": "es6",
    "strict": true,
    "target": "es6"
  },
  "files": [
    "constants.test-mojom-lite.m.ts",
    "enums.test-mojom-lite.m.ts",
    "export1.test-mojom-lite.m.ts",
    "export2.test-mojom-lite.m.ts",
    "export3.test-mojom-lite.m.ts",
    "export4.test-mojom-lite.m.ts",
    "import.test-mojom-lite.m.ts",
    "module.test-mojom-lite.m.ts",
    "structs.test-mojom-lite.m.ts",
    "other_dir/other_dir.test-mojom-lite.m.ts"
  ]
}

Bug: 1002798
Change-Id: I1155d038c93718f706724c99604e2e46917c9b92
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2245855Reviewed-by: default avatarKen Rockot <rockot@google.com>
Commit-Queue: Giovanni Ortuño Urquidi <ortuno@chromium.org>
Cr-Commit-Position: refs/heads/master@{#781689}
parent 84804c7a
......@@ -17,6 +17,7 @@ mojom("test_interfaces") {
"export4.test-mojom",
"import.test-mojom",
"module.test-mojom",
"other_dir/other_dir.test-mojom",
"structs.test-mojom",
]
use_typescript_sources = true
......
......@@ -7,13 +7,16 @@ module mojo.tstest.imported;
import "mojo/public/js/ts/bindings/tests/export1.test-mojom";
import "mojo/public/js/ts/bindings/tests/export2.test-mojom";
import "mojo/public/js/ts/bindings/tests/export4.test-mojom";
import "mojo/public/js/ts/bindings/tests/other_dir/other_dir.test-mojom";
const uint8 kImportedOne = mojo.tstest.exported.kOne;
const uint8 kImportedTwo = mojo.tstest.exported.kTwo;
const uint8 kImportedThree = mojo.tstest.exported.kAlsoThree;
const uint8 kImportedOtherDir = mojo.tstest.exported.kOtherDir;
struct StructWithImportedStructs {
mojo.tstest.exported.ExportedStructOne val1;
mojo.tstest.exported.ExportedStructTwo val2;
mojo.tstest.exported.ExportedStructFour val4;
mojo.tstest.exported.ExportedStructOtherDir val_other_dir;
};
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
module mojo.tstest.exported;
// Test that imports from .mojom files in the same target but different
// directory work.
const uint8 kOtherDir = 1;
struct ExportedStructOtherDir {
bool value;
};
......@@ -15,26 +15,11 @@ import node_modules
def main(argv):
parser = argparse.ArgumentParser()
parser.add_argument('--filelist', required=True)
parser.add_argument('--tsconfig_path', required=True)
args = parser.parse_args(argv)
files = []
with open(args.filelist) as filelist_file:
for line in filelist_file:
for f in line.split():
files.append(os.path.join(os.getcwd(), f))
file_paths = ' '.join(files)
result = node.RunNode(
[node_modules.PathToTypescript()] +
[
"--target 'es6'",
"--module 'es6'",
"--lib 'es6, esnext.bigint'",
"--strict",
file_paths
])
result = node.RunNode([node_modules.PathToTypescript()] +
['--project %s' % args.tsconfig_path])
if len(result) != 0:
raise RuntimeError('Failed to compile Typescript: \n%s' % result)
......
......@@ -1788,22 +1788,19 @@ template("mojom") {
js_extension = ".m.js"
},
]
foreach(dependency_type, dependency_types) {
ts_filelist = []
ts_outputs = []
js_outputs = []
foreach(base_path, output_file_base_paths) {
ts_outputs +=
[ "$root_gen_dir/$base_path-lite${dependency_type.ts_extension}" ]
ts_filelist += [ rebase_path(
"$root_gen_dir/$base_path-lite${dependency_type.ts_extension}",
root_build_dir) ]
js_outputs +=
[ "$root_gen_dir/$base_path-lite${dependency_type.js_extension}" ]
}
# Generate Typescript bindings.
generator_ts_target_name =
"${target_name}_${dependency_type.name}__ts__generator"
action(generator_ts_target_name) {
......@@ -1832,6 +1829,30 @@ template("mojom") {
# TODO(crbug.com/1007591): Support generate_fuzzing.
}
# Create tsconfig.json for the generated Typescript.
tsconfig_filename =
"$target_gen_dir/$target_name-${dependency_type.name}-tsconfig.json"
tsconfig = {
}
tsconfig.compilerOptions = {
target = "es6"
module = "es6"
lib = [
"es6",
"esnext.bigint",
]
strict = true
}
tsconfig.files = []
foreach(base_path, output_file_base_paths) {
tsconfig.files += [ rebase_path(
"$root_gen_dir/$base_path-lite${dependency_type.ts_extension}",
target_gen_dir,
root_gen_dir) ]
}
write_file(tsconfig_filename, tsconfig, "json")
# Compile previously generated Typescript to Javascript.
generator_js_target_name =
"${target_name}_${dependency_type.name}__js__generator"
generator_js_target_names += [ generator_js_target_name ]
......@@ -1841,8 +1862,9 @@ template("mojom") {
sources = ts_outputs
outputs = js_outputs
public_deps = [ ":$generator_ts_target_name" ]
response_file_contents = ts_filelist
args = [ "--filelist={{response_file_name}}" ]
absolute_tsconfig_path =
rebase_path(tsconfig_filename, "", target_gen_dir)
args = [ "--tsconfig_path=$absolute_tsconfig_path" ]
}
}
......
......@@ -6,6 +6,7 @@
<script src="/gen/mojo/public/js/ts/bindings/tests/export2.test-mojom-lite.js"></script>
<script src="/gen/mojo/public/js/ts/bindings/tests/export3.test-mojom-lite.js"></script>
<script src="/gen/mojo/public/js/ts/bindings/tests/export4.test-mojom-lite.js"></script>
<script src="/gen/mojo/public/js/ts/bindings/tests/other_dir/other_dir.test-mojom-lite.js"></script>
<script src="/gen/mojo/public/js/ts/bindings/tests/import.test-mojom-lite.js"></script>
<script>
'use strict';
......@@ -13,5 +14,6 @@ test(() => {
assert_equals(mojo.tstest.imported.IMPORTED_ONE, 1);
assert_equals(mojo.tstest.imported.IMPORTED_TWO, 2);
assert_equals(mojo.tstest.imported.IMPORTED_THREE, 3);
assert_equals(mojo.tstest.imported.IMPORTED_OTHER_DIR, 1);
}, 'Checks that values of imported constants is correct');
</script>
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment