bundle all Svelte css into separate file

- svelte compilation outputs a separate .css file for each component
- compilation also adds an "import foo.css" to the top of each generated
.mjs file
- when the .mjs files are bundled into app.js, esbuild creates an app.css
as well
- graphs.scss was renamed to graphs_shared.scss and imported in the
top level GraphsPage. Henrik's style refactoring would be a better path
forward, but I needed to make this change for now, as the filenames were
conflicting.
This commit is contained in:
Damien Elmes 2021-03-21 15:45:15 +10:00
parent 1e14384d88
commit dbe5d43ba0
6 changed files with 16 additions and 11 deletions

View file

@ -1,4 +1,4 @@
load("@npm//@bazel/esbuild:index.bzl", _esbuild = "esbuild") load("//ts/esbuild:upstream.bzl", _esbuild = "esbuild_macro")
def esbuild(name, **kwargs): def esbuild(name, **kwargs):
_esbuild( _esbuild(

View file

@ -6,8 +6,8 @@ load("//ts:esbuild.bzl", "esbuild")
load("@io_bazel_rules_sass//:defs.bzl", "sass_binary") load("@io_bazel_rules_sass//:defs.bzl", "sass_binary")
sass_binary( sass_binary(
name = "graphs_css", name = "graphs_shared",
src = "graphs.scss", src = "graphs_shared.scss",
visibility = ["//visibility:public"], visibility = ["//visibility:public"],
deps = [ deps = [
"//ts/sass:core_lib", "//ts/sass:core_lib",
@ -66,12 +66,14 @@ esbuild(
external = [ external = [
"protobufjs/light", "protobufjs/light",
], ],
output_css = True,
visibility = ["//visibility:public"], visibility = ["//visibility:public"],
deps = [ deps = [
"//ts/lib", "//ts/lib",
"//ts/lib:backend_proto", "//ts/lib:backend_proto",
"//ts/lib:fluent_proto", "//ts/lib:fluent_proto",
"bootstrap", "bootstrap",
"graphs_shared",
] + svelte_names, ] + svelte_names,
) )

View file

@ -1,7 +1,5 @@
<script context="module">
</script>
<script lang="typescript"> <script lang="typescript">
import "./graphs_shared.css";
import type { SvelteComponent } from "svelte/internal"; import type { SvelteComponent } from "svelte/internal";
import type { I18n } from "anki/i18n"; import type { I18n } from "anki/i18n";
import type { PreferenceStore } from "./preferences"; import type { PreferenceStore } from "./preferences";

View file

@ -8,6 +8,7 @@ _deps = [
"@npm//svelte-preprocess", "@npm//svelte-preprocess",
"@npm//svelte2tsx", "@npm//svelte2tsx",
"@npm//typescript", "@npm//typescript",
"@npm//@types/node",
] ]
ts_library( ts_library(

View file

@ -2,6 +2,7 @@ const fs = require("fs");
const worker = require("@bazel/worker"); const worker = require("@bazel/worker");
const svelte2tsx = require("svelte2tsx"); const svelte2tsx = require("svelte2tsx");
const preprocess = require("svelte-preprocess"); const preprocess = require("svelte-preprocess");
import { basename } from "path";
import * as ts from "typescript"; import * as ts from "typescript";
import * as svelte from "svelte/compiler.js"; import * as svelte from "svelte/compiler.js";
@ -150,9 +151,7 @@ async function writeJs(
const processed = await svelte.preprocess(source, preprocessOptions); const processed = await svelte.preprocess(source, preprocessOptions);
const result = svelte.compile(processed.toString!(), { const result = svelte.compile(processed.toString!(), {
format: "esm", format: "esm",
// FIXME: once we're bundling .css separately, set this to false so we don't css: false,
// also include it in the resulting .js
css: true,
generate: "dom", generate: "dom",
filename: outputJsPath, filename: outputJsPath,
}); });
@ -161,10 +160,15 @@ async function writeJs(
console.log(`warnings during compile: ${result.warnings}`); console.log(`warnings during compile: ${result.warnings}`);
return; return;
} }
const outputSource = result.js.code; // write out the css file
await writeFile(outputJsPath, outputSource);
const outputCss = result.css.code ?? ""; const outputCss = result.css.code ?? "";
await writeFile(outputCssPath, outputCss); await writeFile(outputCssPath, outputCss);
// if it was non-empty, prepend a reference to it in the js file, so that
// it's included in the bundled .css when bundling
const outputSource =
(outputCss ? `import "./${basename(outputCssPath)}";` : "") +
result.js.code;
await writeFile(outputJsPath, outputSource);
} catch (err) { } catch (err) {
console.log(`compile failed: ${err}`); console.log(`compile failed: ${err}`);
return; return;