Skip to content

Commit 357f9c1

Browse files
committed
new option 'source' for tailwind
1 parent 3b5f066 commit 357f9c1

File tree

2 files changed

+52
-8
lines changed

2 files changed

+52
-8
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ and this project try to adheres to [Semantic Versioning](https://semver.org/).
66
Go to the `v1` branch to see the changelog of Lume 1.
77

88
## [Unreleased]
9+
### Added
10+
- `tailwindcss` plugin: New option `source` to configure whether load the content from the `src` folder or from the built pages (By default is `pages`).
11+
912
### Fixed
1013
- Updated deps: `std`, `liquidjs`, `preact`, `tailwindcss`, `xml`, `postcss`, `autoprefixer`, `unocss`.
1114
- Remove empty directories in `dest` folder.

plugins/tailwindcss.ts

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import tailwind from "../deps/tailwindcss.ts";
22
import { getExtension } from "../core/utils/path.ts";
33
import { merge } from "../core/utils/object.ts";
4+
import textLoader from "../core/loaders/text.ts";
45

56
import type { Config } from "../deps/tailwindcss.ts";
67
import type Site from "../core/site.ts";
@@ -9,6 +10,11 @@ export interface Options {
910
/** Extensions processed by this plugin to extract the utility classes */
1011
extensions?: string[];
1112

13+
/**
14+
* Source of the content to be processed by TailwindCSS.
15+
*/
16+
source?: "pages" | "src";
17+
1218
/**
1319
* Options passed to TailwindCSS.
1420
* @see https://tailwindcss.com/docs/configuration
@@ -18,6 +24,7 @@ export interface Options {
1824

1925
export const defaults: Options = {
2026
extensions: [".html"],
27+
source: "pages",
2128
};
2229

2330
export default function (userOptions?: Options) {
@@ -33,14 +40,43 @@ export default function (userOptions?: Options) {
3340
);
3441
}
3542

36-
site.process(options.extensions, (pages) => {
37-
// Get the content of all HTML pages (sorted by path)
38-
const content = pages.sort((a, b) => a.src.path.localeCompare(b.src.path))
39-
.map((page) => ({
40-
raw: page.content as string,
41-
extension: getExtension(page.outputPath).substring(1),
42-
}));
43+
if (options.source === "pages") {
44+
site.process(options.extensions, (pages) => {
45+
// Get the content of all HTML pages (sorted by path)
46+
const content = pages.sort((a, b) =>
47+
a.src.path.localeCompare(b.src.path)
48+
)
49+
.map((page) => ({
50+
raw: page.content as string,
51+
extension: getExtension(page.outputPath).substring(1),
52+
}));
53+
processContent(content);
54+
});
55+
} else {
56+
site.process("*", async () => {
57+
const content: Content[] = [];
58+
59+
for (const [, entry] of site.fs.entries) {
60+
if (entry.type !== "file") {
61+
continue;
62+
}
63+
64+
if (!options.extensions?.includes(getExtension(entry.path))) {
65+
continue;
66+
}
67+
68+
const contentString = await entry.getContent(textLoader);
69+
content.push({
70+
raw: contentString.content as string,
71+
extension: getExtension(entry.path).substring(1),
72+
});
73+
}
4374

75+
processContent(content);
76+
});
77+
}
78+
79+
function processContent(content: Content[]) {
4480
// Create Tailwind plugin
4581
// @ts-ignore: This expression is not callable.
4682
const plugin = tailwind({
@@ -64,6 +100,11 @@ export default function (userOptions?: Options) {
64100
tailwindPlugins = runner.normalize([plugin]);
65101
runner.plugins = runner.plugins.concat(tailwindPlugins);
66102
});
67-
});
103+
}
68104
};
69105
}
106+
107+
interface Content {
108+
raw: string;
109+
extension: string;
110+
}

0 commit comments

Comments
 (0)