-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
35 lines (33 loc) · 1.13 KB
/
index.js
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
import {PurgeCSS} from 'purgecss';
/**
* purgeCSS插件
* @param {object} options
* @returns {import('vite').PluginOption}
*/
export default function myPlugin(options) {
return {
name: 'vite-plugin-purgeCSS',
async generateBundle(_, bundle) {
const content = [];
const css = [];
Object.entries(bundle).forEach(([filename, options]) => {
if (/\.(js|html)$/.test(filename)) {
content.push({ filename, code: options.code });
return;
}
if (/\.(css)$/.test(filename)) {
css.push({ filename, code: options.source });
}
});
await Promise.all(
css.map(async item => {
const purgeCSSResult = await new PurgeCSS().purge({
content: ['./index.html'].concat(content.map(item => ({ raw: item.code }))),
css: [{ raw: item.code }],
});
bundle[item.filename].source = purgeCSSResult[0].css;
})
);
}
};
}