-
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
committed
Aug 24, 2020
1 parent
660b456
commit b116847
Showing
8 changed files
with
184 additions
and
3 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
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,27 @@ | ||
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"], | ||
) |
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,40 @@ | ||
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') | ||
|
||
return new Promise((res) => { | ||
function awaitBuild(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); | ||
} | ||
}; | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
load("//packages/typescript:index.bzl", "ts_project") | ||
|
||
ts_project( | ||
declaration = True, | ||
supports_workers = True, | ||
) |
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 = 45; |
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,6 @@ | ||
{ | ||
"compilerOptions": { | ||
"types": [], | ||
"declaration": true | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
packages/typescript/test/ts_project/worker/worker_adapter.js
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,40 @@ | ||
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') | ||
|
||
return new Promise((res) => { | ||
function awaitBuild(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); | ||
} | ||
}; | ||
child.stdout.on('data', awaitBuild); | ||
}); | ||
} |