-
Notifications
You must be signed in to change notification settings - Fork 959
Expand file tree
/
Copy pathvite.config.ts
More file actions
68 lines (56 loc) · 1.94 KB
/
vite.config.ts
File metadata and controls
68 lines (56 loc) · 1.94 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
import { existsSync } from 'fs';
import { mkdir, writeFile } from 'fs/promises';
import { extractCss } from 'goober';
import { resolve } from 'path';
import { defineConfig, type PluginOption } from 'vite';
import solid from 'vite-plugin-solid';
import { css, type UseCss } from './src';
export default ({ command }: any) => defineConfig({
plugins: [
solid(),
writeCss({
'flipclock.css': [css, 'flip-clock']
})
],
build: {
target: 'esnext',
sourcemap: command === 'build',
minify: true,
lib: {
entry: resolve(__dirname, 'src/index.ts'),
name: 'FlipClock',
fileName: (format) => `FlipClock.${format}.js`,
}
}
});
type WriteCssFiles = Record<string, [UseCss<any>, string]>;
function writeCss(files: WriteCssFiles): PluginOption {
let hasExtracted = false;
let lastExtractedLength = 0;
return {
name: 'extract-css',
writeBundle(options) {
if (hasExtracted || !options.dir) {
return Promise.resolve();
}
const promises: Promise<void>[] = [];
for (const [filename, [css, selector]] of Object.entries(files)) {
const hash = String(css());
const extractedCss = extractCss();
const contents = extractedCss
.slice(lastExtractedLength)
.replaceAll(hash, selector);
const path = resolve(options.dir, 'themes');
promises.push(new Promise(async (res) => {
if (!await existsSync(path)) {
await mkdir(path);
}
res(writeFile(resolve(path, filename), contents));
}));
lastExtractedLength = extractedCss.length;
}
hasExtracted = true;
return Promise.all(promises).then();
}
};
}