Something went wrong. Try again.
Markdown editor
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374import { readFile as read_file, mkdir, writeFile as write_file } from "node:fs/promises";import path from "node:path";import { fileURLToPath as file_url_to_path } from "node:url";import { transformAsync as transform_async } from "@babel/core";import stylex_plugin from "@stylexjs/babel-plugin";import { build } from "esbuild";import appearance from "../shared/appearance.json" with { type: "json" };
const root = file_url_to_path(new URL("../", import.meta.url));const renderer = path.join(root, "renderer") + path.sep;const rules = [];
const result = await build({ absWorkingDir: root, entryPoints: ["renderer/app.ts"], bundle: true, format: "esm", target: "es2022", outfile: "dist/renderer/app.js", jsx: "automatic", write: false, plugins: [ { name: "stylex", setup(builder) { builder.onLoad({ filter: /\.[jt]sx?$/ }, async ({ path: filename }) => { if (!filename.startsWith(renderer)) return; const source = await read_file(filename, "utf8"); const transformed = await transform_async(source, { filename, babelrc: false, configFile: false, parserOpts: { plugins: ["typescript", "jsx"] }, plugins: [ [ stylex_plugin, { dev: false, runtimeInjection: false, treeshakeCompensation: true, unstable_moduleResolution: { type: "commonJS", rootDir: root }, }, ], ], }); rules.push(...(transformed?.metadata.stylex ?? [])); return { contents: transformed?.code ?? source, loader: filename.endsWith("x") ? "tsx" : "ts", resolveDir: path.dirname(filename), }; }); }, }, ],});
// Extract static styles into the existing local stylesheet; no runtime style// injection or relaxed Electron CSP is needed.const css = stylex_plugin.processStylexRules(rules);const theme_variables = (palette) => Object.entries(palette) .map(([name, value]) => `--paper-${name.replaceAll("_", "-")}:${value};`) .join("");
const theme_css = `:root{${theme_variables(appearance.light)}}\n@media(prefers-color-scheme:dark){:root{${theme_variables(appearance.dark)}}}`;for (const output of result.outputFiles) { await mkdir(path.dirname(output.path), { recursive: true }); await write_file( output.path, output.path.endsWith(".css") ? `${theme_css}\n${output.text}\n${css}\n` : output.contents, );}