Skip to content

Commit

Permalink
Tailwind plugin verbose mode (#236)
Browse files Browse the repository at this point in the history
* verbose mode

* more verbose
  • Loading branch information
tlgimenes authored May 20, 2024
1 parent 19a0366 commit e3e2e66
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
17 changes: 14 additions & 3 deletions plugins/tailwind/deno.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ const visit = (
}
};

const importsFrom = async (path: string): Promise<string[]> => {
const importsFrom = async (
path: string,
verbose: boolean,
): Promise<string[]> => {
const program = await parsePath(path);

if (!program) {
Expand Down Expand Up @@ -56,7 +59,7 @@ const importsFrom = async (path: string): Promise<string[]> => {

const arg0 = node.arguments?.[0]?.expression;
if (arg0.type !== "StringLiteral") {
if (path.endsWith(".tsx")) {
if (path.endsWith(".tsx") && verbose) {
console.warn([
`Invalid import statement`,
`TailwindCSS will not load classes from dependencies of ${path}`,
Expand Down Expand Up @@ -104,6 +107,7 @@ const resolveRecursively = async (
loader: (specifier: string) => Promise<string | undefined>,
importMapResolver: ImportMapResolver,
cache: Map<string, string>,
verbose: boolean,
) => {
const resolvedPath = importMapResolver.resolve(path, context);

Expand All @@ -113,7 +117,7 @@ const resolveRecursively = async (

const [content, imports] = await Promise.all([
loader(resolvedPath),
importsFrom(resolvedPath),
importsFrom(resolvedPath, verbose),
]);

if (!content) {
Expand All @@ -122,13 +126,18 @@ const resolveRecursively = async (

cache.set(resolvedPath, content);

if (verbose) {
console.log(`TailwindCSS plugin is resolving: [${imports.join(", ")}]`);
}

await Promise.all(imports.map((imp) =>
resolveRecursively(
imp,
resolvedPath,
loader,
importMapResolver,
cache,
verbose,
)
));
};
Expand All @@ -154,6 +163,7 @@ const readImportMap = async () => {
export const resolveDeps = async (
entries: string[],
cache: Map<string, string>,
verbose = false,
) => {
const importMap = await readImportMap();
const loader = initLoader();
Expand All @@ -170,6 +180,7 @@ export const resolveDeps = async (
loader,
importMapResolver,
cache,
verbose,
);
}
};
6 changes: 3 additions & 3 deletions plugins/tailwind/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ rm static/tailwind.css
* Pass the config directly to use the new dynamic features. Pass undefined
* if you wish to have the old behavior
*/
export const plugin = (config?: Config): Plugin => {
export const plugin = (config?: Config & { verbose?: boolean }): Plugin => {
const routes: Plugin["routes"] = [];

if (!config) {
Expand Down Expand Up @@ -117,7 +117,7 @@ export const plugin = (config?: Config): Plugin => {
}
}

await resolveDeps([...roots.values()], allTsxFiles);
await resolveDeps([...roots.values()], allTsxFiles, config.verbose);

return {
...config,
Expand Down Expand Up @@ -159,7 +159,7 @@ export const plugin = (config?: Config): Plugin => {
config: await withReleaseContent(config),
});

lru.set(revision, css);
lru.set(revision, css!);
}

return new Response(css, {
Expand Down

0 comments on commit e3e2e66

Please sign in to comment.