Skip to content

Commit

Permalink
fix: browser-tailwind
Browse files Browse the repository at this point in the history
  • Loading branch information
KermanX committed Nov 11, 2023
1 parent 36c158b commit 1a41d52
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 8 deletions.
13 changes: 8 additions & 5 deletions packages/browser-tailwind/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import postcss from "postcss";
import tailwindcss, { Config } from "tailwindcss";
import {
VIRTUAL_HTML_FILENAME,
VIRTUAL_CONTENT_EXTNAME,
VIRTUAL_CONTENT_FILENAME,
VIRTUAL_SOURCE_PATH,
} from "./polyfill/constants";

Expand All @@ -12,14 +13,16 @@ self.process = {

export async function compileTailwindCSS(
cssInput: string,
htmlInput: string,
contentInput: string,
extname: string,
config: Config | {} = {},
plugins = [],
) {
self[VIRTUAL_CONTENT_EXTNAME] = extname;
// Tailwind scans the config.content for files to parse classNames -> set a virtual file here
if ("content" in config) {
self[VIRTUAL_HTML_FILENAME] = htmlInput;
config.content = [VIRTUAL_HTML_FILENAME];
if (!("content" in config)) {
self[VIRTUAL_CONTENT_FILENAME] = contentInput;
(config as Config).content = [VIRTUAL_CONTENT_FILENAME];
}
return await postcss([
tailwindcss(config as Config),
Expand Down
5 changes: 3 additions & 2 deletions packages/browser-tailwind/src/polyfill/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
/**
* Dummy constant for Node like environment in Browser for lib depending on fs module, like PostCSS
*/
export const VIRTUAL_SOURCE_PATH = "/sourcePath";
export const VIRTUAL_HTML_FILENAME = "/htmlInput";
export const VIRTUAL_SOURCE_PATH = "BROWSER_TAILWIND_VIRTUAL_SOURCE_PATH";
export const VIRTUAL_CONTENT_FILENAME = "BROWSER_TAILWIND_VIRTUAL_CONTENT_FILENAME";
export const VIRTUAL_CONTENT_EXTNAME = "BROWSER_TAILWIND_VIRTUAL_CONTENT_EXTNAME";
7 changes: 6 additions & 1 deletion packages/browser-tailwind/src/polyfill/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,10 @@ export default {
}
return { mtimeMs: ++i };
},
readFileSync: (id) => self[id] || "",
readFileSync: (id) => self[id] ?? "",
promises: {
readFile(id) {
return self[id] ?? "";
},
},
};
4 changes: 4 additions & 0 deletions packages/browser-tailwind/src/polyfill/path.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
//@ts-nocheck
import { VIRTUAL_CONTENT_EXTNAME } from "./constants";

self.__dirname = "";
export default {
join: () => {
return "";
},
resolve: (id) => id,
extname: (id) => self[VIRTUAL_CONTENT_EXTNAME],
};

export const join = () => {
Expand Down

0 comments on commit 1a41d52

Please sign in to comment.