-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmod.ts
More file actions
92 lines (78 loc) · 2.42 KB
/
mod.ts
File metadata and controls
92 lines (78 loc) · 2.42 KB
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
import { isAbsolute, join, postcss, tailwind } from './deps.ts';
import type { Config, Plugin } from './deps.ts';
import preflight from './preflight.ts';
interface PluginConfig {
mode?: 'development' | 'production';
input?: string;
verbose?: boolean;
tailwindConfig?: Config;
}
let generatedCss = '';
let buildTime = 0;
/**
* Determines whether the script is running in production mode.
*/
const isInProductionMode = (config: PluginConfig) => {
return config.mode === 'production' || Deno.env.get('TAILWINDCSS_MODE') === 'production';
};
/**
* Calculates and logs the time taken to build tailwindcss.
*/
const logBuildTime = (verbose: boolean) => {
if (!verbose) return;
if (!buildTime) {
console.log('[tailwindcss] Building...');
buildTime = Date.now();
return;
}
buildTime = Date.now() - buildTime;
console.log(`[tailwindcss] Rebuilt in ${buildTime}ms`);
buildTime = 0;
};
/**
* Checks if the input file exists and returns its absolute path.
*/
const getInputFilePath = (input: string) => {
const inputFilePath = isAbsolute(input) ? input : join(Deno.cwd(), input);
try {
Deno.statSync(inputFilePath);
} catch (_e) {
throw new Error(`[tailwindcss] No tailwindcss input file found at ${inputFilePath}`);
}
return inputFilePath;
};
/**
* Builds tailwindcss with the provided tailwindConfig and input file content.
*/
const buildTailwindcss = (inputFilePath: string, tailwindConfig: Config) => {
const tailwindBaseConfig = {
...tailwindConfig,
content: (tailwindConfig.content as string[]).map((content) => join(Deno.cwd(), content)),
corePlugins: {
...tailwindConfig?.corePlugins ?? {},
preflight: false,
},
};
const inputText = Deno.readTextFileSync(inputFilePath);
return postcss([tailwind(tailwindBaseConfig)]).process(inputText, { from: inputFilePath }).css;
};
export default (config: PluginConfig): Plugin => ({
name: 'tailwindcss',
render(ctx) {
ctx.render();
const { input = 'styles.css', tailwindConfig, verbose = false } = config;
if (isInProductionMode(config) && generatedCss) {
return {
scripts: [],
styles: [{ cssText: generatedCss }],
};
}
logBuildTime(verbose);
generatedCss = preflight + '\n' + buildTailwindcss(getInputFilePath(input), tailwindConfig || { content: [] });
logBuildTime(verbose);
return {
scripts: [],
styles: [{ cssText: generatedCss }],
};
},
});