This repository was archived by the owner on Oct 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.ts
147 lines (114 loc) · 3.71 KB
/
plugin.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import type { Plugin } from "https://deno.land/x/aleph@v0.3.0-beta.19/types.d.ts";
import type { RequiredConfig } from "https://deno.land/x/aleph@v0.3.0-beta.19/server/config.ts";
import * as semver from "https://deno.land/x/semver@v1.4.0/mod.ts";
import * as colors from "https://deno.land/std@0.125.0/fmt/colors.ts";
import * as fs from "https://deno.land/std@0.125.0/fs/mod.ts";
import * as path from "https://deno.land/std@0.125.0/path/mod.ts";
// Constants
const DEFAULT_VERSION = "^3.0.18";
// Types
type Aleph = Parameters<Plugin["setup"]>[0] & { config: RequiredConfig };
// Helper Functions
const log = (...messages: string[]) => {
console.log(colors.blue("Tailwind CSS"), ...messages);
};
const watch = async (filePath: string, callback: (string: string) => void) => {
const watcher = Deno.watchFs(filePath);
for await (const event of watcher) {
if (event.kind === "modify") {
callback(
new TextDecoder().decode(await Deno.readFile(filePath)),
);
}
}
};
// State
let style = "";
// Main
interface PluginOptions {
verbose?: boolean;
version?: string;
}
const tailwindcss = ({ verbose = false, version }: PluginOptions): Plugin => {
return {
name: "tailwindcss",
async setup(aleph: Aleph) {
const npm = Deno.build.os === "windows" ? "npm.cmd" : "npm";
// Check if this plugin is compatible with the current version of npm.
const npmVersion = new TextDecoder().decode(
await (await Deno.run({ cmd: [npm, "--version"], stdout: "piped" }))
.output(),
).replace(/\r?\n/g, "");
if (!semver.gt(npmVersion, "7.0.0")) {
throw new Error("This plugin requires npm version 7.0.0 or higher.");
}
//
if (version && !semver.validRange(version)) {
throw new Error("Invalid version.");
}
//
const tailwindConfigJs = path.resolve(
aleph.workingDir,
"./tailwind.config.js",
);
if (!(await fs.exists(tailwindConfigJs))) {
log(
"The process was aborted because tailwind.config.js does not exist.",
);
return;
}
// Input
const inputFilePath = await Deno.makeTempFile();
const outputFilePath = await Deno.makeTempFile();
await Deno.writeFile(
inputFilePath,
new TextEncoder().encode(`
@tailwind base;
@tailwind components;
@tailwind utilities;
`),
);
// Events
aleph.onRender(({ html, path }) => {
html.head.push(`<style>${style}</style>`);
log("render", path);
});
// Helper Functions
const build = (options: { watch: boolean }) => {
return Deno.run({
cmd: [
npm,
"exec",
"--yes",
"--",
`tailwindcss@${version ?? DEFAULT_VERSION}`,
"build",
"--config",
tailwindConfigJs,
"--input",
inputFilePath,
"--output",
outputFilePath,
].concat(
aleph.mode === "production" ? ["--minify"] : [],
options.watch ? ["--watch"] : [],
),
cwd: aleph.workingDir,
stdout: verbose ? undefined : "null",
stderr: verbose ? undefined : "null",
});
};
// Main
await build({ watch: false }).status();
style = new TextDecoder().decode(await Deno.readFile(outputFilePath));
if (aleph.mode === "development") {
watch(outputFilePath, (string) => style = string);
build({ watch: true });
log("Start watching code changes...");
}
},
};
};
const defaultConfiguration = tailwindcss({ version: DEFAULT_VERSION });
tailwindcss.setup = defaultConfiguration.setup;
export default tailwindcss;