Skip to content

Commit a2cc838

Browse files
committed
feat: Preprocessor dependency decoupling
1 parent 2c6c8a3 commit a2cc838

File tree

12 files changed

+159
-165
lines changed

12 files changed

+159
-165
lines changed

README.ZH-CN.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,47 @@ export interface Options {
143143
* 时压缩选项,`revoke` 则可以在打包时将注入的代码删除
144144
*/
145145
revoke?: boolean
146+
147+
/**
148+
* 预处理器
149+
* unplugin-vue-cssvars包没有集成预处理器,
150+
* 当你想在预处理器文件中使用unplugin-vue-cssvars时,
151+
* 请将预处理器传递给unplugin-vue-cssvars
152+
* @property { sass | less | stylus }
153+
*/
154+
preprocessor?: PreProcessor
155+
156+
/**
157+
* 选择需要处理编译的文件,默认是css
158+
* 例如:如果你想要处理.scss文件,那么你可以传入 ['** /**.sass']
159+
* @property { ['** /**.css', '** /**.less', '** /**.scss', '** /**.sass', '** /**.styl'] }
160+
* @default ['** /**.css']
161+
*/
162+
includeCompile?: Array<string>
146163
}
147164
```
165+
### 使用预处理器
166+
`unplugin-vue-cssvars` 包没有集成预处理器, 当你想在预处理器文件中使用 `unplugin-vue-cssvars` 时, 请将预处理器传递给 `unplugin-vue-cssvars`
167+
168+
````typescript
169+
// vite.config.ts
170+
import { defineConfig } from 'vite'
171+
import { viteVueCSSVars } from 'unplugin-vue-cssvars'
172+
import sass from 'sass'
173+
import type { PluginOption } from 'vite'
174+
export default defineConfig({
175+
plugins: [
176+
viteVueCSSVars({
177+
preprocessor: { sass },
178+
includeCompile: ['**/**.css', '**/**.scss'],
179+
}) as PluginOption,
180+
],
181+
})
182+
````
183+
在上面的例子中,如果你的项目使用了 `scss`,那么你需要配置 `preprocessor: { sass }`,
184+
值得注意的是,你还需要配置 `includeCompile: ['**/**.css', '**/**.scss']`,
185+
因为读取哪些文件(.sass 或 .less,还是 .styl)来使用 `unplugin-vue-cssvars` 完全由你来控制。
186+
148187
### 关于 revoke 详细说明
149188
> 💡 正式版本以解决重复代码问题,正式版本不再需要设置
150189

README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,49 @@ export interface Options {
143143
* there may be duplicate css after packaging, here we clear it
144144
*/
145145
revoke?: boolean
146+
147+
/**
148+
* preprocessor
149+
* the unplugin-vue-cssvars package does not integrate a preprocessor,
150+
* when you want to use unplugin-vue-cssvars in the preprocessor file,
151+
* please pass the preprocessor to unplugin-vue-cssvars
152+
* @property { sass | less | stylus }
153+
*/
154+
preprocessor?: PreProcessor
155+
156+
/**
157+
* Specify the file to be compiled, for example,
158+
* if you want to compile scss, then you can pass in ['** /**.sass']
159+
* @property { ['** /**.css', '** /**.less', '** /**.scss', '** /**.sass', '** /**.styl'] }
160+
* @default ['** /**.css']
161+
*/
162+
includeCompile?: Array<string>
146163
}
147164
```
165+
### use preprocessor
166+
the `unplugin-vue-cssvars` package does not integrate a preprocessor,
167+
when you want to use `unplugin-vue-cssvars` in the preprocessor file,
168+
please pass the preprocessor to `unplugin-vue-cssvars`
169+
170+
````typescript
171+
// vite.config.ts
172+
import { defineConfig } from 'vite'
173+
import { viteVueCSSVars } from 'unplugin-vue-cssvars'
174+
import sass from 'sass'
175+
import type { PluginOption } from 'vite'
176+
export default defineConfig({
177+
plugins: [
178+
viteVueCSSVars({
179+
preprocessor: { sass },
180+
includeCompile: ['**/**.css', '**/**.scss'],
181+
}) as PluginOption,
182+
],
183+
})
184+
````
185+
In the above example, if your project uses `scss`, then you need to configure `preprocessor: { sass }`,
186+
It is worth noting that you also need to configure `includeCompile: ['**/**.css', '**/**.scss']`,
187+
Because it is entirely up to you to read which files (.sass or .less, or .styl) to use `unplugin-vue-cssvars`.
188+
148189
### Details about revoke
149190
> 💡 v1.0.0 version to solve the problem of duplicate code, no longer need to set
150191

build/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const baseConfig = {
1717
],
1818
format: ['cjs', 'esm'],
1919
clean: true,
20-
minify: false,
20+
minify: true,
2121
dts: false,
2222
outDir: path.resolve(process.cwd(), '../dist'),
2323

package.json

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
"rspack",
1919
"webpack",
2020
"webpack4",
21-
"webpack5"
21+
"webpack5",
22+
"stylus"
2223
],
2324
"license": "MIT",
2425
"author": "baiwusanyu-c",
@@ -90,9 +91,7 @@
9091
"@types/estree": "^0.0.48",
9192
"@types/fs-extra": "^11.0.1",
9293
"@types/gulp": "^4.0.10",
93-
"@types/less": "^3.0.3",
9494
"@types/node": "^18.0.0",
95-
"@types/stylus": "^0.48.38",
9695
"@unplugin-vue-cssvars/build": "workspace:*",
9796
"@unplugin-vue-cssvars/core": "workspace:*",
9897
"@unplugin-vue-cssvars/entry": "workspace:*",
@@ -110,14 +109,11 @@
110109
"git-ensure": "^0.1.0",
111110
"gulp": "^4.0.2",
112111
"jsdom": "^20.0.0",
113-
"less": "^4.1.3",
114112
"lint-staged": "^13.1.1",
115113
"npm-run-all": "^4.1.5",
116114
"rimraf": "^4.3.1",
117115
"rollup": "^3.19.1",
118-
"sass": "^1.59.3",
119116
"simple-git-hooks": "^2.8.1",
120-
"stylus": "^0.59.0",
121117
"typescript": "4.9.4",
122118
"vite": "^4.0.1",
123119
"vitest": "^0.29.2"

packages/core/option/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { resolve } from 'path'
22
import {
33
DEFAULT_EXCLUDE_REG,
44
DEFAULT_INCLUDE_REG,
5+
SUPPORT_FILE_LIST,
56
extend,
67
} from '@unplugin-vue-cssvars/utils'
78
import type { Options } from '../types'
@@ -11,6 +12,8 @@ const defaultOption: Options = {
1112
include: DEFAULT_INCLUDE_REG,
1213
exclude: DEFAULT_EXCLUDE_REG,
1314
revoke: true,
15+
preprocessor: {},
16+
includeCompile: SUPPORT_FILE_LIST,
1417
}
1518

1619
export function initOption(option: Options) {

packages/core/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
"test": "echo \"Error: no test specified\" && exit 1"
1212
},
1313
"devDependencies": {
14-
"@unplugin-vue-cssvars/utils": "workspace:*"
14+
"@types/less": "^3.0.3",
15+
"@types/stylus": "^0.48.38",
16+
"@unplugin-vue-cssvars/utils": "workspace:*",
17+
"sass": "^1.60.0"
1518
}
1619
}

packages/core/runtime/pre-process-css.ts

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,15 @@ import {
88
INJECT_PREFIX_FLAG,
99
INJECT_SUFFIX_FLAG,
1010
SUPPORT_FILE,
11-
SUPPORT_FILE_LIST,
1211
completeSuffix,
1312
transformSymbol,
1413
} from '@unplugin-vue-cssvars/utils'
1514
import MagicString from 'magic-string'
16-
import sass from 'sass'
17-
import less from 'less'
18-
import stylus from 'stylus'
1915
import { parseImports } from '../parser'
2016
import { transformQuotes } from '../transform/transform-quotes'
17+
import type { ICSSFileMap, PreProcessor, SearchGlobOptions } from '../types'
2118
import type { ImportStatement } from '../parser'
22-
import type { ICSSFileMap, SearchGlobOptions } from '../types'
19+
2320
import type { CssNode } from 'css-tree'
2421

2522
/**
@@ -85,10 +82,10 @@ export function walkCSSTree(
8582
* @param options 选项参数 Options
8683
*/
8784
export function preProcessCSS(options: SearchGlobOptions): ICSSFileMap {
88-
const { rootDir } = options
85+
const { rootDir, preprocessor, includeCompile } = options
8986

9087
// 获得文件列表
91-
const files = fg.sync(SUPPORT_FILE_LIST, {
88+
const files = fg.sync(includeCompile!, {
9289
ignore: FG_IGNORE_LIST,
9390
cwd: rootDir,
9491
})
@@ -112,7 +109,7 @@ export function preProcessCSS(options: SearchGlobOptions): ICSSFileMap {
112109
const orgCode = fs.readFileSync(resolve(rootDir!, file), { encoding: 'utf-8' })
113110
const { imports } = parseImports(orgCode, [transformQuotes])
114111
const codeNoImporter = getContentNoImporter(orgCode, imports)
115-
let code = generateCSSCode(codeNoImporter, fileSuffix)
112+
let code = generateCSSCode(codeNoImporter, fileSuffix, preprocessor!)
116113
code = setImportToCompileRes(code, imports)
117114

118115
// parse css ast
@@ -154,29 +151,41 @@ export function preProcessCSS(options: SearchGlobOptions): ICSSFileMap {
154151
return cssFiles
155152
}
156153

157-
export function generateCSSCode(code: string, suffix: string) {
154+
export function generateCSSCode(code: string, suffix: string, preprocessor: PreProcessor) {
158155
let res = ''
159156
switch (suffix) {
160157
case `.${SUPPORT_FILE.SCSS}`: // scss
161158
// @import 有 css 和 scss的同名文件,会编译 scss
162159
// @import 编译 scss,会一直编译,一直到遇到 import 了一个 css 或没有 import 为止
163160
// 这里先分析出 imports,在根据其内容将 sass 中 import 删除
164161
// 编译 sass 为 css,再复原
165-
res = sass.compileString(code).css
162+
if (!preprocessor.sass)
163+
throw new Error('[unplugin-vue-cssvars]: Missing preprocessor \'sass\' dependency, please see readme to resolve this problem')
164+
165+
res = preprocessor.sass.compileString(code).css
166166
break
167167
case `.${SUPPORT_FILE.SASS}`: // sass
168-
res = sass.compileString(code, { syntax: 'indented' }).css
168+
if (!preprocessor.sass)
169+
throw new Error('[unplugin-vue-cssvars]: Missing preprocessor \'sass\' dependency, please see readme to resolve this problem')
170+
171+
res = preprocessor.sass.compileString(code, { syntax: 'indented' }).css
169172
break
170173
case `.${SUPPORT_FILE.LESS}`: // less
171-
less.render(code, {}, (error, output) => {
174+
if (!preprocessor.less)
175+
throw new Error('[unplugin-vue-cssvars]: Missing preprocessor \'less\' dependency, please see readme to resolve this problem')
176+
177+
preprocessor.less.render(code, {}, (error, output) => {
172178
if (error)
173179
throw error
174180

175181
res = output ? output.css : ''
176182
})
177183
break
178184
case `.${SUPPORT_FILE.STYL}`: // stylus
179-
stylus.render(code, {}, (error: Error, css: string) => {
185+
if (!preprocessor.stylus)
186+
throw new Error('[unplugin-vue-cssvars]: Missing preprocessor \'stylus\' dependency, please see readme to resolve this problem')
187+
188+
preprocessor.stylus.render(code, {}, (error: Error, css: string) => {
180189
if (error)
181190
throw error
182191

packages/core/types.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import type { Node } from '@babel/types'
22
import type { FilterPattern } from '@rollup/pluginutils'
3+
import type * as less from 'less'
4+
import type * as sass from 'sass'
5+
import type * as stylus from 'stylus'
36
export interface Options {
47
/**
58
* Provide path which will be transformed
@@ -22,8 +25,30 @@ export interface Options {
2225
* there may be duplicate css after packaging, here we clear it
2326
*/
2427
revoke?: boolean
28+
29+
/**
30+
* preprocessor
31+
* the unplugin-vue-cssvars package does not integrate a preprocessor,
32+
* when you want to use unplugin-vue-cssvars in the preprocessor file,
33+
* please pass the preprocessor to unplugin-vue-cssvars
34+
* @property { sass | less | stylus }
35+
*/
36+
preprocessor?: PreProcessor
37+
38+
/**
39+
* Specify the file to be compiled, for example,
40+
* if you want to compile scss, then you can pass in ['** /**.sass']
41+
* @property { ['** /**.css', '** /**.less', '** /**.scss', '** /**.sass', '** /**.styl'] }
42+
* @default ['** /**.css']
43+
*/
44+
includeCompile?: Array<string>
2545
}
2646

47+
export declare interface PreProcessor {
48+
sass?: typeof sass
49+
less?: typeof less
50+
stylus?: typeof stylus
51+
}
2752
export declare type SearchGlobOptions = Options
2853

2954
export interface ICSSFile {

play/src/App.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export default {
7070
</div>
7171
</template>
7272

73-
<style scoped lang="sass">
73+
<style scoped lang="scss">
7474
/* foo.scss -> test2.css -> test.css */
7575
/* foo.scss -> test.scss -> test2.css */
7676
@@ -79,5 +79,5 @@ export default {
7979
color: v-bind(color2)
8080
}*/
8181
82-
@import ./assets/sass/foo
82+
@import './assets/scss/foo';
8383
</style>

play/vite.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export default defineConfig({
2727
vue(),
2828
viteVueCSSVars({
2929
include: [/App.vue/],
30+
includeCompile: ['**/**.css'],
3031
}),
3132
],
3233
})

0 commit comments

Comments
 (0)