Skip to content

Commit

Permalink
improve minification logic (#237)
Browse files Browse the repository at this point in the history
  • Loading branch information
tlgimenes authored May 27, 2024
1 parent e3e2e66 commit 60d7628
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 31 deletions.
2 changes: 1 addition & 1 deletion plugins/tailwind/bundler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export const bundle = async (
);

console.info(
` 🎨 Tailwind css ready in ${
` 🎨 TailwindCSS ready in ${
cyan(`${((performance.now() - start) / 1e3).toFixed(1)}s`)
}`,
);
Expand Down
30 changes: 2 additions & 28 deletions plugins/tailwind/deno.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@ const visit = (
}
};

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

if (!program) {
Expand Down Expand Up @@ -59,21 +56,6 @@ const importsFrom = async (

const arg0 = node.arguments?.[0]?.expression;
if (arg0.type !== "StringLiteral") {
if (path.endsWith(".tsx") && verbose) {
console.warn([
`Invalid import statement`,
`TailwindCSS will not load classes from dependencies of ${path}`,
"To fix this issue, make sure you are following the patterns:",
` Statically evaluated imports WORK`,
` import("path/to/file")`,
` lazy(() => import("path/to/file"))`,
` Dinamically evaluated imports FAIL`,
` import(\`path/to/file\`)`,
` lazy((variable) => import(\`path/to/file/\${variable}\`))`,
"",
].join("\n"));
}

return;
}

Expand Down Expand Up @@ -107,7 +89,6 @@ 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 @@ -117,7 +98,7 @@ const resolveRecursively = async (

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

if (!content) {
Expand All @@ -126,18 +107,13 @@ 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 @@ -163,7 +139,6 @@ const readImportMap = async () => {
export const resolveDeps = async (
entries: string[],
cache: Map<string, string>,
verbose = false,
) => {
const importMap = await readImportMap();
const loader = initLoader();
Expand All @@ -180,7 +155,6 @@ export const resolveDeps = async (
loader,
importMapResolver,
cache,
verbose,
);
}
};
11 changes: 9 additions & 2 deletions plugins/tailwind/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ export const plugin = (config?: Config & { verbose?: boolean }): Plugin => {
name: "deco-tailwind",
routes,
configResolved: async (fresh) => {
const mode = fresh.dev ? "dev" : "prod";
const isDev = fresh.dev || Deno.env.get("DECO_PREVIEW");
const mode = isDev ? "dev" : "prod";
const ctx = Context.active();

const withReleaseContent = async (config: Config) => {
Expand All @@ -117,7 +118,13 @@ export const plugin = (config?: Config & { verbose?: boolean }): Plugin => {
}
}

await resolveDeps([...roots.values()], allTsxFiles, config.verbose);
const start = performance.now();
await resolveDeps([...roots.values()], allTsxFiles);
const duration = (performance.now() - start).toFixed(0);

console.log(
` 🔍 TailwindCSS resolved ${allTsxFiles.size} dependencies in ${duration}ms`,
);

return {
...config,
Expand Down

0 comments on commit 60d7628

Please sign in to comment.