Skip to content

Commit

Permalink
Merge branch 'tailwind-extractor' into speedup-css-compile
Browse files Browse the repository at this point in the history
  • Loading branch information
zampino committed Oct 18, 2023
2 parents 383c2b7 + 9bdb4a3 commit cc6cddb
Show file tree
Hide file tree
Showing 4 changed files with 668 additions and 0 deletions.
7 changes: 7 additions & 0 deletions tw_extractor/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"dependencies": {
"nbb": "^1.2.179",
"glob": "^8.0.3",
"tailwindcss": "^3.3.3"
}
}
44 changes: 44 additions & 0 deletions tw_extractor/tailwind-extractor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import fs from 'fs'
// import * as ox from '@tailwindcss/oxide'
import { defaultExtractor } from "tailwindcss/lib/lib/defaultExtractor.js"
import { generateRules, resolveMatches } from "tailwindcss/lib/lib/generateRules.js"
import { default as expandTailwindAtRules } from "tailwindcss/lib/lib/expandTailwindAtRules.js"
// import { default as processTailwindFeatures } from "tailwindcss/lib/processTailwindFeatures.js"
import tailwind from "tailwindcss/lib/processTailwindFeatures.js"
import { readdir, readFile } from 'fs'

// https://github.com/tailwindlabs/tailwindcss/blob/master/src/lib/defaultExtractor.js#L3
// https://github.com/tailwindlabs/tailwindcss/blob/master/src/lib/expandTailwindAtRules.js#L8

// root: resources/stylesheets/viewer.css

// https://medium.com/@bomber.marek/whats-tailwind-oxide-engine-the-next-evolution-of-tailwind-css-32e7ef8e19a1

let context = { tailwindConfig: {separator: ':', prefix: ''},
notClassCache: new Set(),
classCache: new Map(),
candidateRuleCache: new Map(),
candidateRuleMap: new Map()},
extract = defaultExtractor(context),
candidates = new Set([...extract("foo bar\n[:div.text-amber-200]\n<div class='mt-2'>ahoi</div>\n<div class='hover:test'>\n<div class='hover:font-bold'></div>"), "*"]),
sortedCandidates = [...candidates].sort((a, z) => {
if (a === z) return 0
if (a < z) return -1
return 1
}),
root = {name: "root",
walkAtRules: (walkFn) => { console.log('walk@', walkFn.toString()) },
walkRules: (walkFn) => { console.log('walk', walkFn.toString()) }}

// readFile("package.json", (err, contents) => { console.log(contents) })

// run with `bun tailwind-extractor.ts`
console.log("Extracted",
// ox,
// await expandTailwindAtRules(context)(root),
// await tailwind((input) => { console.log('input', input); return context; })(root, {}),
// fs.promises.readDir('src', ),
candidates,
Array.from(resolveMatches("foo", context)),
Array.from(resolveMatches("mt-2", context)),
generateRules(sortedCandidates, context));
35 changes: 35 additions & 0 deletions tw_extractor/tailwind_extractor.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
(ns tailwind-extractor
(:require [clojure.string :as str]
[nbb.core :refer [await slurp]]
["fs" :as fs]
["fs$promises" :as fsp]
["glob$default" :as glob]
["process" :refer [chdir cwd]]
["tailwindcss/lib/lib/defaultExtractor.js" :refer [defaultExtractor]]))

(def context #js {:tailwindConfig #js {:separator ":" :prefix ""}})

(defn ->set [a] (new js/Set a))

(def extract (comp to-array ->set (defaultExtractor context)))

(defn tokenize [{:as state :keys [dest-file candidates files]}]
(if-some [f (first files)]
(.. (slurp f)
(then (fn [content]
(println "processing" f)
(tokenize (-> state
(update :files next)
(update :candidates (fnil into #{}) (extract content)))))))
(do
(await (fsp/appendFile dest-file (str/join "\n" candidates)))
(println (str "Extracted " (count candidates) " candidates.")))))

;; run with `yarn nbb -m tailwind-extractor tw-candidates.txt`
(defn -main [& args]
(let [dest-file (or (first args) "test.txt")]
(chdir "..")
(when (fs/existsSync dest-file)
(await (fsp/rm dest-file)))
(tokenize {:dest-file dest-file
:files (glob/sync "**/**.{clj,cljs,cljc}")})))
Loading

0 comments on commit cc6cddb

Please sign in to comment.