Skip to content

Commit

Permalink
Add support for workers in svelte compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
thelgevold committed Nov 25, 2020
1 parent bcb9c5b commit e3876e6
Show file tree
Hide file tree
Showing 111 changed files with 978 additions and 23 deletions.
3 changes: 2 additions & 1 deletion internal/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,11 @@ nodejs_binary(
)

nodejs_binary(
name = "svelte",
name = "svelte_bin",
data = [
":svelte.js",
"@build_bazel_rules_svelte_deps//svelte",
"@build_bazel_rules_svelte_deps//@bazel/worker",
],
entry_point = ":svelte.js",
visibility = ["//visibility:public"],
Expand Down
3 changes: 2 additions & 1 deletion internal/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"terser": "5.3.8",
"google-closure-compiler": "20201102.0.1",
"fs-extra": "9.0.1",
"find-up": "5.0.0"
"find-up": "5.0.0",
"@bazel/worker": "2.3.0"
}
}
18 changes: 12 additions & 6 deletions internal/svelte.bzl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"Implementation of the svelte rule"

load("@build_bazel_rules_nodejs//:providers.bzl", "run_node")
SvelteFilesInfo = provider("transitive_sources")

def get_transitive_srcs(srcs, deps):
Expand All @@ -10,14 +10,20 @@ def get_transitive_srcs(srcs, deps):

def _svelte(ctx):
args = ctx.actions.args()

args.use_param_file("@%s", use_always = True)
args.set_param_file_format("multiline")

args.add(ctx.file.entry_point.path)
args.add(ctx.outputs.build.path)

ctx.actions.run(
mnemonic = "Svelte",
executable = ctx.executable._svelte,
run_node(
ctx,
execution_requirements = {"supports-workers": "1"},
executable = "_svelte_bin",
outputs = [ctx.outputs.build],
inputs = [ctx.file.entry_point],
mnemonic = "SvelteCompile",
arguments = [args],
)

Expand All @@ -34,8 +40,8 @@ svelte = rule(
"entry_point": attr.label(allow_single_file = True),
"deps": attr.label_list(),
"srcs": attr.label_list(allow_files = True),
"_svelte": attr.label(
default = Label("//internal:svelte"),
"_svelte_bin": attr.label(
default = Label("//internal:svelte_bin"),
executable = True,
cfg = "host",
),
Expand Down
66 changes: 56 additions & 10 deletions internal/svelte.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,61 @@
const fs = require("fs");
const worker = require('@bazel/worker');
const svelte = require("svelte/compiler");

const outputJs = process.argv[3];
const input = process.argv[2];
async function compileSvelte(args) {
const input = args[0]
const outputJs = args[1]

const svelte = require("svelte/compiler");
const source = await readFile(input);
const result = svelte.compile(source, {
format: "esm",
generate: "dom",
filename: outputJs
});

await writeFile(outputJs, result.js.code);

return true;
}

function writeFile(file, data) {
return new Promise((resolve, reject) => {
fs.writeFile(file, data, (err) => {
if (err) {
reject(err);
return
}
resolve();
});
});
}

function readFile(file) {
return new Promise((resolve, reject) => {
fs.readFile(file, 'utf8' , (err, data) => {
if (err) {
reject(err);
return
}
resolve(data);
});
});
}

const source = fs.readFileSync(input, "utf8");
const result = svelte.compile(source, {
format: "esm",
generate: "dom",
filename: outputJs
});
function main(args) {
if (worker.runAsWorker(process.argv)) {
worker.log('Svelte running as a Bazel worker');
worker.runWorkerLoop(compileSvelte);
}
else {
const paramFile = process.argv[2].replace(/^@/, '');
const commandLineArgs = require('fs').readFileSync(paramFile, 'utf-8').trim().split('\n');
console.log('Svelte running as a standalone process');
compileSvelte(commandLineArgs);
}
}

fs.writeFileSync(outputJs, result.js.code);
if (require.main === module) {
main(process.argv.slice(2));
process.exitCode = 0;
}
94 changes: 94 additions & 0 deletions internal/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,66 @@
# yarn lockfile v1


"@bazel/[email protected]":
version "2.3.0"
resolved "https://registry.yarnpkg.com/@bazel/worker/-/worker-2.3.0.tgz#c2eca9505332377381c79715ec8d4fea70cad64f"
integrity sha512-kz84SxTVokbkzB/WsLjH1qN6P045wG3oNbpRGFn4u1+rmAbklxVoQljxjmdh1o1KfBGYJaZx1SaxVLBPwVjMng==
dependencies:
protobufjs "6.8.8"

"@protobufjs/aspromise@^1.1.1", "@protobufjs/aspromise@^1.1.2":
version "1.1.2"
resolved "https://registry.yarnpkg.com/@protobufjs/aspromise/-/aspromise-1.1.2.tgz#9b8b0cc663d669a7d8f6f5d0893a14d348f30fbf"
integrity sha1-m4sMxmPWaafY9vXQiToU00jzD78=

"@protobufjs/base64@^1.1.2":
version "1.1.2"
resolved "https://registry.yarnpkg.com/@protobufjs/base64/-/base64-1.1.2.tgz#4c85730e59b9a1f1f349047dbf24296034bb2735"
integrity sha512-AZkcAA5vnN/v4PDqKyMR5lx7hZttPDgClv83E//FMNhR2TMcLUhfRUBHCmSl0oi9zMgDDqRUJkSxO3wm85+XLg==

"@protobufjs/codegen@^2.0.4":
version "2.0.4"
resolved "https://registry.yarnpkg.com/@protobufjs/codegen/-/codegen-2.0.4.tgz#7ef37f0d010fb028ad1ad59722e506d9262815cb"
integrity sha512-YyFaikqM5sH0ziFZCN3xDC7zeGaB/d0IUb9CATugHWbd1FRFwWwt4ld4OYMPWu5a3Xe01mGAULCdqhMlPl29Jg==

"@protobufjs/eventemitter@^1.1.0":
version "1.1.0"
resolved "https://registry.yarnpkg.com/@protobufjs/eventemitter/-/eventemitter-1.1.0.tgz#355cbc98bafad5978f9ed095f397621f1d066b70"
integrity sha1-NVy8mLr61ZePntCV85diHx0Ga3A=

"@protobufjs/fetch@^1.1.0":
version "1.1.0"
resolved "https://registry.yarnpkg.com/@protobufjs/fetch/-/fetch-1.1.0.tgz#ba99fb598614af65700c1619ff06d454b0d84c45"
integrity sha1-upn7WYYUr2VwDBYZ/wbUVLDYTEU=
dependencies:
"@protobufjs/aspromise" "^1.1.1"
"@protobufjs/inquire" "^1.1.0"

"@protobufjs/float@^1.0.2":
version "1.0.2"
resolved "https://registry.yarnpkg.com/@protobufjs/float/-/float-1.0.2.tgz#5e9e1abdcb73fc0a7cb8b291df78c8cbd97b87d1"
integrity sha1-Xp4avctz/Ap8uLKR33jIy9l7h9E=

"@protobufjs/inquire@^1.1.0":
version "1.1.0"
resolved "https://registry.yarnpkg.com/@protobufjs/inquire/-/inquire-1.1.0.tgz#ff200e3e7cf2429e2dcafc1140828e8cc638f089"
integrity sha1-/yAOPnzyQp4tyvwRQIKOjMY48Ik=

"@protobufjs/path@^1.1.2":
version "1.1.2"
resolved "https://registry.yarnpkg.com/@protobufjs/path/-/path-1.1.2.tgz#6cc2b20c5c9ad6ad0dccfd21ca7673d8d7fbf68d"
integrity sha1-bMKyDFya1q0NzP0hynZz2Nf79o0=

"@protobufjs/pool@^1.1.0":
version "1.1.0"
resolved "https://registry.yarnpkg.com/@protobufjs/pool/-/pool-1.1.0.tgz#09fd15f2d6d3abfa9b65bc366506d6ad7846ff54"
integrity sha1-Cf0V8tbTq/qbZbw2ZQbWrXhG/1Q=

"@protobufjs/utf8@^1.1.0":
version "1.1.0"
resolved "https://registry.yarnpkg.com/@protobufjs/utf8/-/utf8-1.1.0.tgz#a777360b5b39a1a2e5106f8e858f2fd2d060c570"
integrity sha1-p3c2C1s5oaLlEG+OhY8v0tBgxXA=

"@rollup/[email protected]":
version "15.0.0"
resolved "https://registry.yarnpkg.com/@rollup/plugin-commonjs/-/plugin-commonjs-15.0.0.tgz#690d15a9d54ba829db93555bff9b98ff34e08574"
Expand Down Expand Up @@ -46,11 +106,21 @@
resolved "https://registry.yarnpkg.com/@types/estree/-/estree-0.0.39.tgz#e177e699ee1b8c22d23174caaa7422644389509f"
integrity sha512-EYNwp3bU+98cpU4lAWYYL7Zz+2gryWH1qbdDTidVd6hkiR6weksdbMadyXKXNPEkQFhXM+hVO9ZygomHXp+AIw==

"@types/long@^4.0.0":
version "4.0.1"
resolved "https://registry.yarnpkg.com/@types/long/-/long-4.0.1.tgz#459c65fa1867dafe6a8f322c4c51695663cc55e9"
integrity sha512-5tXH6Bx/kNGd3MgffdmP4dy2Z+G4eaXw0SE81Tq3BNadtnMR5/ySMzX4SLEzHJzSmPNn4HIdpQsBvXMUykr58w==

"@types/node@*":
version "14.6.0"
resolved "https://registry.yarnpkg.com/@types/node/-/node-14.6.0.tgz#7d4411bf5157339337d7cff864d9ff45f177b499"
integrity sha512-mikldZQitV94akrc4sCcSjtJfsTKt4p+e/s0AGscVA6XArQ9kFclP+ZiYUMnq987rc6QlYxXv/EivqlfSLxpKA==

"@types/node@^10.1.0":
version "10.17.46"
resolved "https://registry.yarnpkg.com/@types/node/-/node-10.17.46.tgz#1cd867ebfe9957ab45951f2f715f8de5f3dab7a3"
integrity sha512-Tice8a+sJtlP9C1EUo0DYyjq52T37b3LexVu3p871+kfIBIN+OQ7PKPei1oF3MgF39olEpUfxaLtD+QFc1k69Q==

"@types/[email protected]":
version "1.17.1"
resolved "https://registry.yarnpkg.com/@types/resolve/-/resolve-1.17.1.tgz#3afd6ad8967c77e4376c598a82ddd58f46ec45d6"
Expand Down Expand Up @@ -309,6 +379,11 @@ locate-path@^6.0.0:
dependencies:
p-locate "^5.0.0"

long@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/long/-/long-4.0.0.tgz#9a7b71cfb7d361a194ea555241c92f7468d5bf28"
integrity sha512-XsP+KhQif4bjX1kbuSiySJFNAehNxgLb6hPRGJ9QsUr8ajHkuXGdrHmFUTUUXhDwVX2R5bY4JNZEwbUiMhV+MA==

magic-string@^0.25.7:
version "0.25.7"
resolved "https://registry.yarnpkg.com/magic-string/-/magic-string-0.25.7.tgz#3f497d6fd34c669c6798dcb821f2ef31f5445051"
Expand Down Expand Up @@ -379,6 +454,25 @@ process-nextick-args@^2.0.0, process-nextick-args@~2.0.0:
resolved "https://registry.yarnpkg.com/process-nextick-args/-/process-nextick-args-2.0.1.tgz#7820d9b16120cc55ca9ae7792680ae7dba6d7fe2"
integrity sha512-3ouUOpQhtgrbOa17J7+uxOTpITYWaGP7/AhoR3+A+/1e9skrzelGi/dXzEYyvbxubEF6Wn2ypscTKiKJFFn1ag==

[email protected]:
version "6.8.8"
resolved "https://registry.yarnpkg.com/protobufjs/-/protobufjs-6.8.8.tgz#c8b4f1282fd7a90e6f5b109ed11c84af82908e7c"
integrity sha512-AAmHtD5pXgZfi7GMpllpO3q1Xw1OYldr+dMUlAnffGTAhqkg72WdmSY71uKBF/JuyiKs8psYbtKrhi0ASCD8qw==
dependencies:
"@protobufjs/aspromise" "^1.1.2"
"@protobufjs/base64" "^1.1.2"
"@protobufjs/codegen" "^2.0.4"
"@protobufjs/eventemitter" "^1.1.0"
"@protobufjs/fetch" "^1.1.0"
"@protobufjs/float" "^1.0.2"
"@protobufjs/inquire" "^1.1.0"
"@protobufjs/path" "^1.1.2"
"@protobufjs/pool" "^1.1.0"
"@protobufjs/utf8" "^1.1.0"
"@types/long" "^4.0.0"
"@types/node" "^10.1.0"
long "^4.0.0"

readable-stream@^2.3.5:
version "2.3.7"
resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-2.3.7.tgz#1eca1cf711aef814c04f62252a36a62f6cb23b57"
Expand Down
4 changes: 2 additions & 2 deletions package.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ load("@build_bazel_rules_nodejs//:index.bzl", "yarn_install")
def rules_svelte_dependencies():
http_archive(
name = "build_bazel_rules_nodejs",
sha256 = "10fffa29f687aa4d8eb6dfe8731ab5beb63811ab00981fc84a93899641fd4af1",
urls = ["https://github.com/bazelbuild/rules_nodejs/releases/download/2.0.3/rules_nodejs-2.0.3.tar.gz"],
sha256 = "452bef42c4b2fbe0f509a2699ffeb3ae2c914087736b16314dbd356f3641d7e5",
urls = ["https://github.com/bazelbuild/rules_nodejs/releases/download/2.3.0/rules_nodejs-2.3.0.tar.gz"],
)

yarn_install(
Expand Down
3 changes: 2 additions & 1 deletion test-closure-compiler/.bazelrc
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
build --symlink_prefix=dist/
build --strategy=SvelteCompile=worker
build --symlink_prefix=dist/
4 changes: 2 additions & 2 deletions test-closure-compiler/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ git_repository(

http_archive(
name = "build_bazel_rules_nodejs",
sha256 = "10fffa29f687aa4d8eb6dfe8731ab5beb63811ab00981fc84a93899641fd4af1",
urls = ["https://github.com/bazelbuild/rules_nodejs/releases/download/2.0.3/rules_nodejs-2.0.3.tar.gz"],
sha256 = "452bef42c4b2fbe0f509a2699ffeb3ae2c914087736b16314dbd356f3641d7e5",
urls = ["https://github.com/bazelbuild/rules_nodejs/releases/download/2.3.0/rules_nodejs-2.3.0.tar.gz"],
)

local_repository(
Expand Down
Loading

0 comments on commit e3876e6

Please sign in to comment.