Skip to content

Commit

Permalink
deno2: compile in TS, build protobuf
Browse files Browse the repository at this point in the history
  • Loading branch information
ry committed Jun 12, 2018
1 parent 356fd18 commit b300353
Show file tree
Hide file tree
Showing 7 changed files with 529 additions and 3,013 deletions.
98 changes: 80 additions & 18 deletions deno2/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -68,29 +68,91 @@ proto_library("msg_proto") {
]
}

action("run_parcel") {
template("run_node") {
action(target_name) {
forward_variables_from(invoker, "*")
script = "js/run_node.py"
}
}

run_node("bundle") {
main_source = "$target_gen_dir/tsc_dist/main.js"
main_output = "$target_gen_dir/bundle/main.js"
sources = [
"js/main.ts",
main_source,
]
outputs = [
"$target_gen_dir/main.js",
"$target_gen_dir/main.map",
main_output,
]
deps = [
":run_tsc",
]
args = [
"./node_modules/.bin/browserify",
rebase_path(main_source, root_build_dir),
"-o",
rebase_path(main_output, root_build_dir),
]
}

# Our script imports this Python file so we want to rebuild if it changes.
# inputs = [ "helper_library.py" ]
run_node("run_tsc") {
main_source = "js/main.ts"
sources = [
"$target_gen_dir/node_modules/deno_pb/msg.pb.d.ts",
"$target_gen_dir/node_modules/deno_pb/msg.pb.js",
"js/tsconfig.json",
main_source,
]
out_dir = "$target_gen_dir/tsc_dist"
outputs = [
out_dir + "/main.js",
out_dir + "/main.map",
]
deps = [
":run_pbjs",
":run_pbts",
]
args = [
"./node_modules/.bin/tsc",
"--baseUrl",
rebase_path(target_gen_dir + "/node_modules", root_build_dir),
"--outDir",
rebase_path(out_dir, root_build_dir),
rebase_path(main_source, root_build_dir),
]
}

# Note that we have to manually pass the sources to our script if the
# script needs them as inputs.
script = "js/run_node.py"
root = root_build_dir + "/../../js"
run_node("run_pbjs") {
sources = [
"msg.proto",
]
outputs = [
"$target_gen_dir/node_modules/deno_pb/msg.pb.js",
]
args = [
"./node_modules/.bin/pbjs",
"--target=static-module",
"--wraper=commonjs",
"--out=" + rebase_path(outputs[0], root_build_dir),
rebase_path(sources[0], root_build_dir),
]
}

run_node("run_pbts") {
sources = [
"$target_gen_dir/node_modules/deno_pb/msg.pb.js",
]
outputs = [
"$target_gen_dir/node_modules/deno_pb/msg.pb.d.ts",
]
args = [
"./node_modules/.bin/parcel",
"build",
"--log-level=1",
"--no-minify",
"--out-dir=" + rebase_path(target_gen_dir, root),
] + rebase_path(sources, root)
"./node_modules/.bin/pbts",
"--out=" + rebase_path(outputs[0], root_build_dir),
rebase_path(sources[0], root_build_dir),
]
deps = [
":run_pbjs",
]
}

# Template to generate different V8 snapshots based on different runtime flags.
Expand Down Expand Up @@ -145,9 +207,9 @@ template("create_snapshot") {

# Generates $target_gen_dir/snapshot_deno.cc
create_snapshot("deno") {
js = "$target_gen_dir/main.js"
js = "$target_gen_dir/bundle/main.js"
deps = [
":run_parcel",
":bundle",
]
}

Expand Down
6 changes: 6 additions & 0 deletions deno2/js/deno.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Copyright 2018 Ryan Dahl <[email protected]>
// All rights reserved. MIT License.
type MessageCallback = (msg: ArrayBuffer) => void;
declare function denoSub(channel: string, cb: MessageCallback): void;
declare function denoPub(channel: string, msg: ArrayBuffer): null | ArrayBuffer;
declare function denoPrint(x: string): void;
7 changes: 7 additions & 0 deletions deno2/js/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
/// <reference path="deno.d.ts" />
//import { main as pb } from "deno_pb/msg.pb"
import * as ts from "typescript";

const globalEval = eval;
const window = globalEval("this");
window["denoMain"] = () => {
//const msg = pb.Msg.fromObject({});
//denoPrint(`msg.command: ${msg.command}`);
denoPrint(`ts.version: ${ts.version}`);
denoPrint("Hello world from foo");
return "foo";
};
3 changes: 2 additions & 1 deletion deno2/js/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"devDependencies": {
"parcel-bundler": "^1.8.1",
"browserify": "^16.2.2",
"protobufjs": "^6.8.6",
"typescript": "^2.9.1"
}
}
10 changes: 7 additions & 3 deletions deno2/js/run_node.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
#!/usr/bin/env python
"""
gn can only run python scripts.
Also Node programs except to be run with cwd = $root_dir/js so it can resolve
node_modules.
"""
import subprocess
import sys
import os


js_path = os.path.dirname(os.path.realpath(__file__))
os.chdir(js_path)
node_modules_path = os.path.join(js_path, "node_modules")

# root_out_dir
if not os.path.exists("node_modules"):
os.symlink(node_modules_path, "node_modules")

args = ["node"] + sys.argv[1:]
sys.exit(subprocess.call(args))
19 changes: 19 additions & 0 deletions deno2/js/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"compilerOptions": {
"allowJs": true,
"module": "commonjs",
"noImplicitAny": true,
"sourceMap": true,
"removeComments": true,
"preserveConstEnums": true,
"declaration": true,
"target": "es2017",
"noImplicitReturns": true,
"pretty": true,
"noFallthroughCasesInSwitch": true,
"allowUnreachableCode": false,
"experimentalDecorators": true
},
"include": ["*.ts", "*.js"],
"exclude": ["tests.ts"]
}
Loading

0 comments on commit b300353

Please sign in to comment.