Skip to content

Commit

Permalink
feat: Integrates TailwindCSS with Decofile (#230)
Browse files Browse the repository at this point in the history
  • Loading branch information
tlgimenes authored Jan 12, 2024
1 parent af9c87f commit cf7307a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
15 changes: 13 additions & 2 deletions plugins/tailwind/bundler.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import autoprefixer from "npm:[email protected]";
import cssnano from "npm:[email protected]";
import postcss from "npm:[email protected]";
import tailwindcss from "npm:tailwindcss@3.3.3";
import tailwindcss from "npm:tailwindcss@3.4.1";
import { cyan } from "std/fmt/colors.ts";
import { ensureFile } from "std/fs/mod.ts";
import { join, toFileUrl } from "std/path/mod.ts";
Expand All @@ -17,7 +17,9 @@ const DEFAULT_TAILWIND_CSS = `
@tailwind utilities;
`;

export const bundle = async ({ to, from }: { to: string; from: string }) => {
export const bundle = async (
{ to, from, release }: { to: string; from: string; release: string },
) => {
const start = performance.now();

// Try to recover config from default file, a.k.a tailwind.config.ts
Expand All @@ -27,6 +29,15 @@ export const bundle = async ({ to, from }: { to: string; from: string }) => {
.then((mod) => mod.default)
.catch(() => DEFAULT_OPTIONS);

if (Array.isArray(config.content)) {
config.content.push({
raw: release,
extension: "json",
});
} else {
console.warn("TailwindCSS generation from decofile disabled");
}

const processor = postcss([
// deno-lint-ignore no-explicit-any
(tailwindcss as any)(config),
Expand Down
22 changes: 18 additions & 4 deletions plugins/tailwind/mod.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
import type { Handlers, Plugin } from "$fresh/server.ts";
import { context } from "deco/live.ts";
import { Context, context } from "deco/deco.ts";
import { createWorker } from "../../utils/worker.ts";

export const TO = "./static/tailwind.css";
export const FROM = "./tailwind.css";

let current: string | undefined = "";

const generate = async () => {
const active = Context.active();
const revision = await active.release?.revision();

if (revision === current) {
return;
}

/**
* Here be capybaras! 🐁🐁🐁
*
Expand All @@ -22,16 +31,21 @@ const generate = async () => {
type: "module",
});

await worker.bundle({ to: TO, from: FROM });
await worker.bundle({
to: TO,
from: FROM,
release: JSON.stringify(await active.release?.state()),
});

worker.dispose();
current = revision;
};

const bundle = context.isDeploy ? Promise.resolve() : generate();
const bundle = context.isDeploy ? () => Promise.resolve() : generate;

export const handler: Handlers = {
GET: async () => {
await bundle;
await bundle();

try {
const [stats, file] = await Promise.all([Deno.lstat(TO), Deno.open(TO)]);
Expand Down

0 comments on commit cf7307a

Please sign in to comment.