Skip to content

Commit f9d52a3

Browse files
committed
feat: add plugin for bundle css file
1 parent 503ef1b commit f9d52a3

File tree

8 files changed

+670
-23
lines changed

8 files changed

+670
-23
lines changed

docs/examples/say-hello/basic.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<script setup>
22
import { StSayHello } from 'starter-lib-vue3'
3-
import 'starter-lib-vue3/dist/es/say-hello/SayHello.css'
3+
import 'starter-lib-vue3/dist/es/say-hello/style.css'
44
</script>
55

66
<template>
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import type { Plugin } from 'vite'
2+
import fs from 'node:fs'
3+
import path from 'node:path'
4+
import { cwd } from 'node:process'
5+
import cssnano from 'cssnano'
6+
import postcss from 'postcss'
7+
8+
interface PluginOptions {
9+
target: string
10+
formats?: string[]
11+
fileName?: string
12+
}
13+
14+
function plugin(options: PluginOptions): Plugin {
15+
return {
16+
name: 'vite-plugin-bundle-styles',
17+
enforce: 'post',
18+
async generateBundle() {
19+
const cssFiles: string[] = []
20+
const srcDir = path.resolve(cwd(), options.target)
21+
const formats = options.formats || ['css', 'scss', 'sass', 'less']
22+
23+
function collectCssFiles(dir: string): void {
24+
const files = fs.readdirSync(dir)
25+
for (const file of files) {
26+
const fullPath = path.join(dir, file)
27+
const stat = fs.statSync(fullPath)
28+
if (stat.isDirectory()) {
29+
collectCssFiles(fullPath)
30+
}
31+
else if (formats.some(ext => file.endsWith(`.${ext}`))) {
32+
cssFiles.push(fullPath)
33+
}
34+
}
35+
}
36+
37+
collectCssFiles(srcDir)
38+
39+
const combinedCss = cssFiles
40+
.map(file => fs.readFileSync(file, 'utf-8'))
41+
.join('\n')
42+
43+
const result = await postcss([cssnano]).process(combinedCss, { from: undefined })
44+
const compressedCss = result.css
45+
46+
const outputFileName = options.fileName || 'styles.css'
47+
this.emitFile({
48+
type: 'asset',
49+
fileName: outputFileName,
50+
source: compressedCss,
51+
})
52+
},
53+
}
54+
}
55+
56+
export default plugin
57+
export { plugin as vitePluginBundleStyles }

package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"type": "module",
44
"version": "0.5.0",
55
"packageManager": "pnpm@9.10.0",
6-
"description": "Vue 3 component library starter template, provides VitePress documentation, supports building ESM, CJS and IIFE formats. ",
6+
"description": "Vue 3 component library starter template, provides VitePress documentation, supports building ESM, CJS and UMD formats. ",
77
"author": "Kieran Wang <kieranwme@gmail.com> (https://github.com/kieranwv/)",
88
"license": "MIT",
99
"homepage": "https://github.com/starter-collective/starter-lib-vue3#readme",
@@ -34,8 +34,8 @@
3434
},
3535
"main": "dist/lib/index.cjs",
3636
"module": "dist/es/index.js",
37-
"unpkg": "dist/iife/index.js",
38-
"jsdelivr": "dist/iife/index.js",
37+
"unpkg": "dist/umd/index.js",
38+
"jsdelivr": "dist/umd/index.js",
3939
"types": "dist/es/index.d.ts",
4040
"files": [
4141
"dist"
@@ -72,11 +72,13 @@
7272
"@vue/test-utils": "^2.4.6",
7373
"@vueuse/core": "^13.1.0",
7474
"bumpp": "^10.1.0",
75+
"cssnano": "^7.0.7",
7576
"eslint": "^9.24.0",
7677
"jsdom": "^26.0.0",
7778
"lint-staged": "^15.5.0",
7879
"markdown-it": "^14.1.0",
7980
"markdown-it-container": "^4.0.0",
81+
"postcss": "^8.5.3",
8082
"rimraf": "^6.0.1",
8183
"shiki": "^3.2.2",
8284
"simple-git-hooks": "^2.12.1",

0 commit comments

Comments
 (0)