Skip to content

Commit

Permalink
Clean up the build (replace browserify with parcel)
Browse files Browse the repository at this point in the history
  • Loading branch information
ry committed Jun 13, 2018
1 parent 1676822 commit bb6222c
Show file tree
Hide file tree
Showing 5 changed files with 3,045 additions and 378 deletions.
47 changes: 30 additions & 17 deletions deno2/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -76,51 +76,64 @@ template("run_node") {
}

run_node("bundle") {
main_source = "$target_gen_dir/tsc_dist/main.js"
main_output = "$target_gen_dir/bundle/main.js"
out_dir = "$target_gen_dir/bundle/"
sources = [
main_source,
"$target_gen_dir/tsc_dist/main.js", # Not real input. See run_tsc comment.
"js/main.ts",
]
outputs = [
main_output,
out_dir + "main.js",
]
deps = [
":run_tsc",
]
args = [
"./node_modules/browserify/bin/cmd.js",
rebase_path(main_source, root_build_dir),
"-o",
rebase_path(main_output, root_build_dir),
"./node_modules/parcel-bundler/bin/cli.js",
"build",
"--no-minify",
"--out-dir",
rebase_path(out_dir, root_build_dir),
rebase_path("js/main.ts", root_build_dir),
]
}

# Due to bugs in Parcel we must run TSC independently in order to catch errors.
# https://github.com/parcel-bundler/parcel/issues/954
run_node("run_tsc") {
main_source = "js/main.ts"
main = "js/main.ts"
tsconfig = "js/tsconfig.json"
out_dir = "$target_gen_dir/tsc_dist/"
sources = [
#"js/msg.pb.d.ts",
"js/msg.pb.d.ts",
"js/msg.pb.js",
"js/tsconfig.json",
main_source,
main,
tsconfig,
]
out_dir = "$target_gen_dir/tsc_dist"
outputs = [
out_dir + "/main.js",
out_dir + "/main.map",
]
deps = [
":pbjs_hack",
":protobufjs",
]
args = [
"./node_modules/typescript/bin/tsc",
"-p",
rebase_path("js/tsconfig.json", root_build_dir),
"--project",
rebase_path(tsconfig, root_build_dir),
"--outDir",
rebase_path(out_dir, root_build_dir),
]
}

action("pbjs_hack") {
# Generates protobufjs code.
# TODO(ry) Ideally protobufjs output files should be written into
# target_gen_dir, but its difficult to get this working in a way that the
# bundler can resolve their location. (The bundler does not support NODE_PATH?)
# Therefore this hack: write the generated msg.pb.js and msg.pb.d.ts outputs
# into the js/ folder, and we check them into the repo. Hopefully this hack can
# be removed at some point. If msg.proto is changed, commit changes to the
# generated JS files. The stamp file is just to make gn work.
action("protobufjs") {
script = "js/pbjs_hack.py"
sources = [
"msg.proto",
Expand Down
5 changes: 1 addition & 4 deletions deno2/js/package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
{
"devDependencies": {
"@types/base64-js": "^1.2.5",
"@types/source-map-support": "^0.4.1",
"browserify": "^16.2.2",
"parcel-bundler": "^1.8.1",
"protobufjs": "^6.8.6",
"source-map-support": "^0.5.6",
"typescript": "^2.9.1"
}
}
14 changes: 9 additions & 5 deletions deno2/js/pbjs_hack.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
#!/usr/bin/env python
"""
gn can only run python scripts.
protobuf.js must generate some javascript files.
it's very difficult to get this into the gn build sanely.
therefore we write them into the source directory.
Generates protobufjs code.
"""
import subprocess
import sys
import os
# TODO(ry) Ideally protobufjs output files should be written into
# target_gen_dir, but its difficult to get this working in a way that parcel can
# resolve their location. (Parcel does not support NODE_PATH.) Therefore this
# hack: write the generated msg.pb.js and msg.pb.d.ts outputs into the js/
# folder, and we check them into the repo. Hopefully this hack can be removed at
# some point. If msg.proto is changed, commit changes to the generated JS
# files.

js_path = os.path.dirname(os.path.realpath(__file__))
#bin_path = os.path.join(js_path, "deno_protobufjs", "bin")
pbjs_path = os.path.join(js_path, "node_modules", "protobufjs", "bin")
pbjs_bin = os.path.join(pbjs_path, "pbjs")
pbts_bin = os.path.join(pbjs_path, "pbts")
Expand All @@ -31,7 +36,6 @@ def touch(fname):
subprocess.check_call([
"node",
pbjs_bin,
#"--dependency=./deno_protobufjs/minimal",
"--target=static-module",
"--wrapper=commonjs",
"--out=" + msg_pbjs_out,
Expand Down
5 changes: 3 additions & 2 deletions deno2/js/run_node.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#!/usr/bin/env python
"""
gn can only run python scripts.
gn can only run python scripts. This launches a subprocess Node process.
The working dir of this program is out/Debug/ (AKA root_build_dir)
Before running node, we symlink js/node_modules to out/Debug/node_modules.
"""
import subprocess
import sys
Expand All @@ -23,7 +25,6 @@ def symlink(target, name, target_is_dir=False):
js_path = os.path.dirname(os.path.realpath(__file__))
node_modules_path = os.path.join(js_path, "node_modules")

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

Expand Down
Loading

0 comments on commit bb6222c

Please sign in to comment.