-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
54 lines (50 loc) · 1.48 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
const fs = require('fs');
const { resolve } = require('path');
const minimatch = require('minimatch');
let viteConfig;
module.exports = function (options = {}) {
return {
name: 'lib-css',
apply: 'build',
enforce: 'post',
configResolved(resolvedConfig) {
viteConfig = resolvedConfig;
},
writeBundle(option, bundle) {
if (!viteConfig.build || !viteConfig.build.lib) {
// only for lib build
console.warn('vite-plugin-libcss only works in lib mode.');
return;
}
if (option.format !== 'es') {
// only for es built
return;
}
const files = Object.keys(bundle);
const cssFile = files.find((v) => v.endsWith('.css'));
if (!cssFile) {
return;
}
for (const file of files) {
if (!bundle[file].isEntry) {
// only for entry
continue;
}
if (options.include && !minimatch(file, options.include)) {
// check if the file matches the include pattern
continue;
}
if (options.exclude && minimatch(file, options.exclude)) {
// check if the file matches the exclude pattern
continue;
}
const outDir = viteConfig.build.outDir || 'dist';
const filePath = resolve(viteConfig.root, outDir, file);
const data = fs.readFileSync(filePath, {
encoding: 'utf8',
});
fs.writeFileSync(filePath, `import './${cssFile}';\n${data}`);
}
},
};
};