Skip to content

Commit

Permalink
feat(typescript): use run_node helper to execute tsc
Browse files Browse the repository at this point in the history
This lets it resolve plugins like ts-lit-plugin.
Demonstrate this by adding a lit_plugin test under packages/typescript
  • Loading branch information
alexeagle committed Feb 27, 2020
1 parent 35952e5 commit 066a52c
Show file tree
Hide file tree
Showing 12 changed files with 395 additions and 29 deletions.
3 changes: 2 additions & 1 deletion WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,9 @@ install_bazel_dependencies()
# With http_archive it only sees releases/download/*.tar.gz urls
git_repository(
name = "build_bazel_rules_typescript",
commit = "5d79e42953eb8614d961ccf0e3440884e974eeb3",
commit = "10a5a86885f95ab788fd841ade47b6a16e0c13d6",
remote = "http://github.com/bazelbuild/rules_typescript.git",
shallow_since = "1582757372 -0800",
)

# We have a source dependency on build_bazel_rules_typescript
Expand Down
5 changes: 3 additions & 2 deletions internal/linker/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
/* THIS FILE GENERATED FROM .ts; see BUILD.bazel */ /* clang-format off */var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Expand Down Expand Up @@ -493,7 +494,7 @@ Include as much of the build output as you can without disclosing anything confi
exports.main = main;
exports.runfiles = new Runfiles(process.env);
if (require.main === module) {
(() => __awaiter(this, void 0, void 0, function* () {
(() => __awaiter(void 0, void 0, void 0, function* () {
try {
process.exitCode = yield main(process.argv.slice(2), exports.runfiles);
}
Expand Down
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"@gregmagolan/test-a": "0.0.5",
"@types/hammerjs": "2.0.35",
"@types/jasmine": "~3.3.13",
"@types/node": "10.12.20",
"@types/node": "^12.0.0",
"@types/semver": "6.2.0",
"bazel_workspaces": "file:./tools/npm_packages/bazel_workspaces",
"clang-format": "1.2.2",
Expand All @@ -42,6 +42,7 @@
"karma-jasmine": "2.0.1",
"karma-requirejs": "1.1.0",
"karma-sourcemap-loader": "0.3.7",
"lit-element": "^2.2.1",
"minimist": "^1.2.0",
"node_resolve_index": "file:./tools/npm_packages/node_resolve_index",
"node_resolve_index_2": "file:./tools/npm_packages/node_resolve_index_2",
Expand Down Expand Up @@ -70,10 +71,11 @@
"terser": "^4.4.0",
"testy": "file:./tools/npm_packages/testy",
"tmp": "0.1.0",
"tsickle": "0.33.1",
"ts-lit-plugin": "^1.1.9",
"tsickle": "0.38.0",
"tsutils": "2.27.2",
"typeorm": "0.2.18",
"typescript": "3.1.6",
"typescript": "^3.7.5",
"unidiff": "1.0.1",
"v8-coverage": "1.0.9",
"yarn": "1.22.0",
Expand Down
2 changes: 0 additions & 2 deletions packages/typescript/src/internal/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ load("@bazel_skylib//:bzl_library.bzl", "bzl_library")

package(default_visibility = ["//visibility:public"])

exports_files(["tsetse/tsconfig.json"])

bzl_library(
name = "bzl",
srcs = glob(
Expand Down
7 changes: 4 additions & 3 deletions packages/typescript/src/internal/build_defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

"TypeScript compilation"

load("@build_bazel_rules_nodejs//:providers.bzl", "NpmPackageInfo", "js_ecma_script_module_info", "js_named_module_info", "node_modules_aspect")
load("@build_bazel_rules_nodejs//:providers.bzl", "NpmPackageInfo", "js_ecma_script_module_info", "js_named_module_info", "node_modules_aspect", "run_node")

# pylint: disable=unused-argument
# pylint: disable=missing-docstring
Expand Down Expand Up @@ -142,7 +142,8 @@ def _compile_action(ctx, inputs, outputs, tsconfig_file, node_opts, description
arguments.append(tsconfig_file.path)
mnemonic = "tsc"

ctx.actions.run(
run_node(
ctx,
progress_message = "Compiling TypeScript (%s) %s" % (description, ctx.label),
mnemonic = mnemonic,
inputs = action_inputs,
Expand All @@ -152,7 +153,7 @@ def _compile_action(ctx, inputs, outputs, tsconfig_file, node_opts, description
# See https://github.com/NixOS/nixpkgs/issues/43955#issuecomment-407546331
use_default_shell_env = True,
arguments = arguments,
executable = ctx.executable.compiler,
executable = "compiler",
execution_requirements = {
"supports-workers": str(int(ctx.attr.supports_workers)),
},
Expand Down
35 changes: 35 additions & 0 deletions packages/typescript/test/lit_plugin/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Copyright 2017 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

load("@npm_bazel_typescript//:index.from_src.bzl", "ts_library")

ts_library(
name = "lit_plugin",
srcs = glob(["*.ts"]),
expected_diagnostics = [
"""TS2322: \\[lit\\] Unknown tag "unknown-element". Did you mean 'lit-element'?""",
"TS2322: \\[lit\\] Type '222' is not assignable to 'string",
"TS2322: \\[lit\\] Type '333' is not assignable to 'string",
"""TS2322: \\[lit\\] Type '"aaa"' is not assignable to 'number'""",
"""TS2322: \\[lit\\] Type '"ccc"' is not assignable to 'number'""",
"TS2322: \\[lit\\] Type '444' is not assignable to 'string",
"TS2322: \\[lit\\] Type '{ field: number; }' is not assignable to '{ field: string; }'",
],
tsconfig = ":tsconfig.json",
deps = [
"@npm//lit-element",
# TODO(#850): plugins["@npm//ts-lit-plugin"] instead?
"@npm//ts-lit-plugin",
],
)
22 changes: 22 additions & 0 deletions packages/typescript/test/lit_plugin/bad.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import './elements';
import {html} from 'lit-element';


const objValue = {
field: 111,
};
console.log(html`
<unknown-element></unknown-element>
<div .id=${222}></div>
<lit-element .strField=${333} .numField=${'aaa'}></lit-element>
<lit-element numField="bbb"></lit-element>
<vanilla-element .vanillaStr=${444}></vanilla-element>
<declared-element
.declaredNumberProp=${'ccc'}
.declaredObjProp=${objValue}></declared-element>
`);
14 changes: 14 additions & 0 deletions packages/typescript/test/lit_plugin/declared_element.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

export {};

/** An element that's only declared in a definition. */
declare class DeclaredElement extends HTMLElement {
declaredNumberProp: number;
declaredObjProp: {field: string};
}

declare global {
interface HTMLElementTagNameMap {
'declared-element': DeclaredElement;
}
}
25 changes: 25 additions & 0 deletions packages/typescript/test/lit_plugin/elements.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* @fileoverview Elements used in multiple build tests.
*/

import {customElement, LitElement} from 'lit-element';


@customElement('lit-element')
class LitElementElement extends LitElement {
strField!: string;
numField!: number;
}

class VanillaElement extends HTMLElement {
vanillaStr = 'hi';
str!: string;
}
customElements.define('vanilla-element', VanillaElement);

declare global {
interface HTMLElementTagNameMap {
'lit-element': LitElementElement;
'vanilla-element': VanillaElement;
}
}
33 changes: 33 additions & 0 deletions packages/typescript/test/lit_plugin/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"compilerOptions": {
"plugins": [
{
"name": "ts-lit-plugin",
"strict": true,
"rules": {
"no-unknown-tag-name": "error",
"no-unclosed-tag": "error",
"no-unknown-property": "error",
"no-unintended-mixed-binding": "error",
"no-invalid-boolean-binding": "error",
"no-expressionless-property-binding": "error",
"no-noncallable-event-binding": "error",
"no-boolean-in-attribute-binding": "error",
"no-complex-attribute-binding": "error",
"no-nullable-attribute-binding": "error",
"no-incompatible-type-binding": "error",
"no-invalid-directive-binding": "error",
"no-incompatible-property-type": "error",
"no-unknown-property-converter": "error",
"no-invalid-attribute-name": "error",
"no-invalid-tag-name": "error",
"no-unknown-attribute": "off",
"no-missing-import": "off",
}
}
]
},
"exclude": [
"node_modules"
]
}
2 changes: 1 addition & 1 deletion packages/worker/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ pkg_npm(
# Fix the require() statement that loads the worker_protocol.proto file
# we are re-rooting the sources into the @bazel/worker package so it's no longer
# relative to the build_bazel_rules_typescript workspace.
"build_bazel_rules_typescript": "@bazel/worker",
"../../third_party": "@bazel/worker/third_party",
},
deps = [
":copy_worker_dts",
Expand Down
Loading

0 comments on commit 066a52c

Please sign in to comment.