-
Notifications
You must be signed in to change notification settings - Fork 522
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(typescript): worker mode for ts_project
- Loading branch information
Daniel Muller
authored and
Dan Muller
committed
Sep 24, 2020
1 parent
56b8f69
commit ac14465
Showing
11 changed files
with
211 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,14 @@ | ||
{ | ||
"compilerOptions": { | ||
"jsx": "react", | ||
"lib": ["ES2015", "DOM"] | ||
} | ||
"lib": [ | ||
"ES2015", | ||
"DOM" | ||
] | ||
}, | ||
// When using ts_project in worker mode, make sure to whitelist the files that should be part of this particular compilation. In worker mode, the CWD is different than by default so typescript will pickup extraneous files without the whitelist. | ||
"include": [ | ||
"*.tsx", | ||
"*.ts" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# BEGIN-INTERNAL | ||
|
||
load("//internal/common:copy_to_bin.bzl", "copy_to_bin") | ||
load("//third_party/github.com/bazelbuild/bazel-skylib:rules/copy_file.bzl", "copy_file") | ||
|
||
# Copy the proto file to a matching third_party/... nested directory | ||
# so the runtime require() statements still work | ||
_worker_proto_dir = "third_party/github.com/bazelbuild/bazel/src/main/protobuf" | ||
|
||
genrule( | ||
name = "copy_worker_js", | ||
srcs = ["//packages/worker:npm_package"], | ||
outs = ["worker.js"], | ||
cmd = "cp $(execpath //packages/worker:npm_package)/index.js $@", | ||
visibility = ["//visibility:public"], | ||
) | ||
|
||
copy_file( | ||
name = "copy_worker_proto", | ||
src = "@build_bazel_rules_typescript//%s:worker_protocol.proto" % _worker_proto_dir, | ||
out = "%s/worker_protocol.proto" % _worker_proto_dir, | ||
visibility = ["//visibility:public"], | ||
) | ||
|
||
copy_to_bin( | ||
name = "worker_adapter", | ||
srcs = [ | ||
"worker_adapter.js", | ||
], | ||
visibility = ["//visibility:public"], | ||
) | ||
|
||
filegroup( | ||
name = "package_contents", | ||
srcs = [ | ||
"BUILD.bazel", | ||
], | ||
visibility = ["//packages/typescript:__subpackages__"], | ||
) | ||
|
||
# END-INTERNAL | ||
|
||
exports_files([ | ||
"worker_adapter.js", | ||
]) | ||
|
||
filegroup( | ||
name = "worker", | ||
srcs = [ | ||
"third_party/github.com/bazelbuild/bazel/src/main/protobuf/worker_protocol.proto", | ||
"worker.js", | ||
"worker_adapter.js", | ||
], | ||
visibility = ["//visibility:public"], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
const child_process = require('child_process'); | ||
|
||
const worker = require('./worker'); | ||
|
||
const workerArg = process.argv.indexOf('--persistent_worker') | ||
if (workerArg > 0) { | ||
process.argv.splice(workerArg, 1) | ||
|
||
if (process.platform !== 'linux' && process.platform !== 'darwin') { | ||
throw new Error('Worker mode is only supported on unix type systems.'); | ||
} | ||
|
||
worker.runWorkerLoop(awaitOneBuild); | ||
} | ||
|
||
const [tscBin, ...tscArgs] = process.argv.slice(2); | ||
|
||
const child = child_process.spawn( | ||
tscBin, | ||
tscArgs, | ||
{stdio: 'pipe'}, | ||
); | ||
|
||
function awaitOneBuild() { | ||
child.kill('SIGCONT') | ||
|
||
let buffer = []; | ||
return new Promise((res) => { | ||
function awaitBuild(s) { | ||
buffer.push(s); | ||
|
||
if (s.includes('Watching for file changes.')) { | ||
child.kill('SIGSTOP') | ||
|
||
const success = s.includes('Found 0 errors.'); | ||
res(success); | ||
|
||
child.stdout.removeListener('data', awaitBuild); | ||
|
||
if (!success) { | ||
console.error( | ||
`\nError output from tsc worker:\n\n ${ | ||
buffer.slice(1).map(s => s.toString()).join('').replace(/\n/g, '\n ')}`, | ||
) | ||
} | ||
|
||
buffer = []; | ||
} | ||
}; | ||
child.stdout.on('data', awaitBuild); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
load("//packages/typescript:index.bzl", "ts_project") | ||
|
||
ts_project( | ||
supports_workers = True, | ||
tsconfig = { | ||
"compilerOptions": { | ||
"declaration": True, | ||
"types": [], | ||
}, | ||
}, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// TODO: make it big enough to slow down tsc | ||
|
||
export const a: number = 2; |