Skip to content

Commit 3de2d81

Browse files
authored
feat: Add file inclusion and exclusion options (#6)
* feat: Add file inclusion and exclusion options * Update index.d.ts * fix: remove console.log * fix: minimatch package dependencies * docs: readme --------- Co-authored-by: mtt <>
1 parent a791892 commit 3de2d81

File tree

5 files changed

+64
-8
lines changed

5 files changed

+64
-8
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,22 @@ export default defineConfig({
3030
});
3131
```
3232

33+
or with include/exclude options
34+
35+
```js
36+
// vite.config.js
37+
import libCss from 'vite-plugin-libcss';
38+
39+
// https://vitejs.dev/config/
40+
export default defineConfig({
41+
plugins: [
42+
// any other plugins
43+
libCss({
44+
include: 'src/**/*', // Include all entry files
45+
exclude: 'src/utils/*', // Exclude entry files in the "utils" directory
46+
})
47+
],
48+
});
49+
```
50+
3351
Note that this plugin will only work with [library-mode](https://vitejs.dev/guide/build.html#library-mode) and es format build.

index.d.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1-
import type { ResolvedConfig, PluginOption } from 'vite';
1+
import type { PluginOption } from 'vite';
22

3-
export default function (): PluginOption
3+
type LibCssOptions = {
4+
include?: string;
5+
exclude?: string;
6+
}
7+
8+
export default function (option: LibCssOptions): PluginOption

index.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
const fs = require('fs');
22
const { resolve } = require('path');
3+
const minimatch = require('minimatch');
34

45
let viteConfig;
56

6-
module.exports = function () {
7+
module.exports = function (options = {}) {
78
return {
89
name: 'lib-css',
910
apply: 'build',
1011
enforce: 'post',
1112

12-
configResolved (resolvedConfig) {
13+
configResolved(resolvedConfig) {
1314
viteConfig = resolvedConfig;
1415
},
1516

16-
writeBundle (option, bundle) {
17+
writeBundle(option, bundle) {
1718
if (!viteConfig.build || !viteConfig.build.lib) {
1819
// only for lib build
19-
console.warn('vite-plugin-libcss only works in lib mode.')
20+
console.warn('vite-plugin-libcss only works in lib mode.');
2021
return;
2122
}
2223
if (option.format !== 'es') {
@@ -33,6 +34,14 @@ module.exports = function () {
3334
// only for entry
3435
continue;
3536
}
37+
if (options.include && !minimatch(file, options.include)) {
38+
// check if the file matches the include pattern
39+
continue;
40+
}
41+
if (options.exclude && minimatch(file, options.exclude)) {
42+
// check if the file matches the exclude pattern
43+
continue;
44+
}
3645
const outDir = viteConfig.build.outDir || 'dist';
3746
const filePath = resolve(viteConfig.root, outDir, file);
3847
const data = fs.readFileSync(filePath, {

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,8 @@
2626
},
2727
"peerDependencies": {
2828
"vite": "*"
29+
},
30+
"dependencies": {
31+
"minimatch": "^9.0.1"
2932
}
30-
}
33+
}

pnpm-lock.yaml

Lines changed: 22 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)