From 6cee43631a0d4c1a52d37f5a839956714cab6aae Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Thu, 15 Apr 2021 14:27:53 +1000 Subject: [PATCH] allow passing sass deps to svelte compile/check --- ts/svelte/svelte.bzl | 16 +++++++++++++--- ts/svelte/svelte.ts | 12 ++++++++---- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/ts/svelte/svelte.bzl b/ts/svelte/svelte.bzl index 9744be58e..57b700a97 100644 --- a/ts/svelte/svelte.bzl +++ b/ts/svelte/svelte.bzl @@ -1,5 +1,9 @@ load("@npm//svelte-check:index.bzl", _svelte_check = "svelte_check_test") load("@build_bazel_rules_nodejs//:providers.bzl", "declaration_info", "run_node") +load("@io_bazel_rules_sass//:defs.bzl", "SassInfo") + +def _get_sources(deps): + return depset([], transitive = [dep[SassInfo].transitive_sources for dep in deps]) def _svelte(ctx): args = ctx.actions.args() @@ -10,13 +14,17 @@ def _svelte(ctx): args.add(ctx.outputs.mjs.path) args.add(ctx.outputs.dts.path) args.add(ctx.outputs.css.path) + args.add(ctx.var["BINDIR"]) + args.add(ctx.var["GENDIR"]) args.add_all(ctx.files._shims) + deps = _get_sources(ctx.attr.deps).to_list() + ctx.actions.run( execution_requirements = {"supports-workers": "1"}, executable = ctx.executable._svelte_bin, outputs = [ctx.outputs.mjs, ctx.outputs.dts, ctx.outputs.css], - inputs = [ctx.file.entry_point] + ctx.files._shims, + inputs = [ctx.file.entry_point] + ctx.files._shims + deps, mnemonic = "Svelte", arguments = [args], ) @@ -29,6 +37,7 @@ svelte = rule( implementation = _svelte, attrs = { "entry_point": attr.label(allow_single_file = True), + "deps": attr.label_list(), "_svelte_bin": attr.label( default = Label("//ts/svelte:svelte_bin"), executable = True, @@ -46,11 +55,12 @@ svelte = rule( }, ) -def compile_svelte(name, srcs): +def compile_svelte(name, srcs, deps = []): for src in srcs: svelte( name = src.replace(".svelte", ""), entry_point = src, + deps = deps, ) native.filegroup( @@ -74,5 +84,5 @@ def svelte_check(name = "svelte_check", srcs = []): "//ts/lib:backend_proto", "@npm//sass", ] + srcs, - link_workspace_root = True, + env = {"SASS_PATH": "$(rootpath //ts:tsconfig.json)/../.."}, ) diff --git a/ts/svelte/svelte.ts b/ts/svelte/svelte.ts index d1fdf3bde..1a59ecb5e 100644 --- a/ts/svelte/svelte.ts +++ b/ts/svelte/svelte.ts @@ -146,9 +146,13 @@ async function writeJs( source: string, inputFilename: string, outputJsPath: string, - outputCssPath: string + outputCssPath: string, + binDir: string, + genDir: string ): Promise { - const preprocessOptions = preprocess({}); + const preprocessOptions = preprocess({ + scss: { includePaths: [binDir, genDir] }, + }); preprocessOptions.filename = inputFilename; try { @@ -179,13 +183,13 @@ async function writeJs( } async function compileSvelte(args) { - const [sveltePath, mjsPath, dtsPath, cssPath, ...tsLibs] = args; + const [sveltePath, mjsPath, dtsPath, cssPath, binDir, genDir, ...tsLibs] = args; const svelteSource = (await readFile(sveltePath)) as string; const mockTsPath = sveltePath + ".ts"; await writeTs(svelteSource, sveltePath, mockTsPath); await writeDts(mockTsPath, dtsPath, tsLibs); - await writeJs(svelteSource, sveltePath, mjsPath, cssPath); + await writeJs(svelteSource, sveltePath, mjsPath, cssPath, binDir, genDir); return true; }