💅 Using Lightning CSS with Deno #
In this video, we see a watch script for running Lightning CSS with Deno. This Deno script:
- waits, watching for changes to your input CSS files;
- bundles input CSS into a single output file when inputs change; and
- transpiles and minifies the generated CSS.
We use esbuild and Lightning CSS together, to generate ready-to-ship CSS which will run in older browsers, even when you work with modern CSS features. Lightning CSS is written in Rust, and makes use of the servo browser code (also used in Firefox Browser) for parsing CSS.
You see some details on setting up the watch script, which will help you customize it to suit your own workflow. Although we work completely in Deno here (using Deno Fresh as the framework), the script will be just as handy working in Rust Leptos, and other non-JS web frameworks.
I hope it will be useful for you. You can drop a comment below or reach out for a chat on Element , as well as X @mention if you have suggestions for improvements or questions.
📹 Video #
Please enable JavaScript to watch the video 📼
🗳 Poll #
🖥 Using Lightning CSS with Deno: Code #
Watch Script #
lightningcss.ts
— click to expand code.
1 import { debounce } from "@std/async";2 import { relative, resolve } from "@std/path";3 import browserslist from "browserslist";4 import { build } from "esbuild/mod.js";5 import init, { browserslistToTargets, transform } from "lightningcss";67 const __dirname = resolve();8 const ignoreFiles = ["styles/main_bundled.css"];910 await init();11 console.log("Watching for updates...");1213 const targets = browserslistToTargets(14 browserslist(15 "> 0.5%, last 2 versions, Firefox >= 102, Firefox ESR, not dead",16 ),17 );1819 async function buildStyles(path: string) {20 try {21 const css = await Deno.readTextFile(path);22 const { code: outputCss } = transform({23 filename: path,24 code: new TextEncoder().encode(css),25 minify: true,26 targets,27 });2829 const outputDir = "./static";30 const outputPath = `${outputDir}/styles.css`;31 const decoder = new TextDecoder();32 try {33 await Deno.writeTextFile(outputPath, decoder.decode(outputCss));34 } catch (error: unknown) {35 if (error instanceof Deno.errors.NotFound) {36 await Deno.mkdir(outputDir, { recursive: true });37 await Deno.writeTextFile(outputPath, decoder.decode(outputCss));38 }39 throw error;40 }41 } catch (error: unknown) {42 console.error(`Error building styles for path ${path}: ${error as string}`);43 }44 }4546 async function bundleStyles() {47 try {48 await build({49 entryPoints: ["styles/main.css"],50 bundle: true,51 outfile: "styles/main_bundled.css",52 external: ["*.woff2"],53 });54 } catch (error: unknown) {55 console.error(`Error bundling styles: ${error as string}`);56 }57 }5859 const debouncedUpdateStyles = debounce(async (path: string) => {60 const relativePath = relative(__dirname, path);6162 if (!ignoreFiles.includes(relativePath)) {63 await bundleStyles();64 await buildStyles("styles/main_bundled.css");65 console.log("Updated static/styles.css");66 }67 }, 200);6869 const watcher = Deno.watchFs(["./styles"]);70 for await (const event of watcher) {71 const { paths } = event;72 paths.forEach((path) => {73 debouncedUpdateStyles(path);74 });75 }
Running Watch Script #
In a new Terminal tab, run:
deno task watch:css
🔗 Links #
- GitHub repo with full project code
- Trying out Leptos post
- esbuild docs
- Lightning CSS docs
- Why switch to OK LCH colours
- Element chat: #Rodney matrix chat
- X handle: @askRodney
🙏🏽 Feedback #
Have you found the post useful? Would you prefer to see posts on another topic instead? Get in touch with ideas for new posts. Also, if you like my writing style, get in touch if I can write some posts for your company site on a consultancy basis. Read on to find ways to get in touch, further below. If you want to support posts similar to this one and can spare a few dollars, euros or pounds, please consider supporting me through Buy me a Coffee.
Just dropped a new video on ⚡️ Lightning CSS using:
— Rodney (@askRodney) October 13, 2023
— Deno watchFS, to watch for input CSS changes;
— esbuild to bundle
@import source into a single file; and
— Lightning CSS to minify and add legacy browser support.
Hope you find it useful!#askRodneyhttps://t.co/4M4CHazdmb
Finally, feel free to share the post on your social media accounts for all your followers who will find it useful. As well as leaving a comment below, you can get in touch via @askRodney on Twitter, @rodney@toot.community on Mastodon and also the #rodney Element Matrix room. Also, see further ways to get in touch with Rodney Lab. I post regularly on Astro as well as Deno. Also, subscribe to the newsletter to keep up-to-date with our latest projects.