Skip to content

Commit 96bf044

Browse files
authored
feat: support sass indented syntax (#15)
* chore: temp commit * chore: 临时提交 * chore: 临时提交 * chore: temp commit * chore: temp commit * chore: temp cimmit * chore: temp commit * chore: temp commit * feat: Generic importer parser * feat: support sass indented syntax
1 parent fffb65d commit 96bf044

File tree

13 files changed

+703
-182
lines changed

13 files changed

+703
-182
lines changed

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
}
4545
},
4646
"scripts": {
47-
"pa": "esno packages/core/parser/parser-import.ts",
4847
"init": "pnpm i",
4948
"lint:fix": "eslint --fix ./ --ext .vue,.js,.ts,.jsx,.tsx,.json ",
5049
"dev": "pnpm run --filter @unplugin-vue-cssvars/build dev",

packages/core/css/__test__/__snapshots__/pre-process-css.spec.ts.snap

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,7 @@ div{color:v-bind(color)}
7676
`;
7777

7878
exports[`pre process css > getCurFileContent: basic 1`] = `
79-
"
80-
81-
82-
#app {
79+
"#app {
8380
div {
8481
color: v-bind(fooColor);
8582
}
@@ -90,9 +87,7 @@ exports[`pre process css > getCurFileContent: basic 1`] = `
9087
`;
9188

9289
exports[`pre process css > getCurFileContent: no ; 1`] = `
93-
"
94-
95-
#app {
90+
"#app {
9691
div {
9792
color: v-bind(fooColor);
9893
}

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

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import sass from 'sass'
1717
import less from 'less'
1818
import stylus from 'stylus'
1919
import { parseImports } from '../parser/parser-import'
20+
import { transformQuotes } from '../transform/transform-quotes'
2021
import type { ImportStatement } from '../parser/parser-import'
2122
import type { ICSSFileMap, SearchGlobOptions } from '../types'
2223

@@ -200,26 +201,31 @@ export function generateCSSCode(path: string, suffix: string) {
200201
const code = fs.readFileSync(path, { encoding: 'utf-8' })
201202
let res = ''
202203
switch (suffix) {
203-
case `.${SUPPORT_FILE.SCSS}`: // scss / sass
204+
case `.${SUPPORT_FILE.SCSS}`: // scss
204205
// @import 有 css 和 scss的同名文件,会编译 scss
205206
// @import 编译 scss,会一直编译,一直到遇到 import 了一个 css 或没有 import 为止
206207
// 这里先分析出 imports,在根据其内容将 sass 中 import 删除
207208
// 编译 sass 为 css,再复原
208209
// eslint-disable-next-line no-case-declarations
209-
const parseSassImporter = parseImports(code)
210+
const parseScssImporter = parseImports(code, [transformQuotes])
210211
// eslint-disable-next-line no-case-declarations
211-
const codeNoImporter = getCurFileContent(code, parseSassImporter.imports)
212+
const codeScssNoImporter = getCurFileContent(code, parseScssImporter.imports)
212213
// eslint-disable-next-line no-case-declarations
213-
const sassParseRes = sass.compileString(codeNoImporter)
214-
res = setImportToCompileRes(sassParseRes.css, parseSassImporter.imports)
214+
const scssParseRes = sass.compileString(codeScssNoImporter)
215+
res = setImportToCompileRes(scssParseRes.css, parseScssImporter.imports)
215216
break
216217
case `.${SUPPORT_FILE.SASS}`: // sass
217-
// ⭐TODO: 支持 sass
218-
res = ''
218+
// eslint-disable-next-line no-case-declarations
219+
const parseSassImporter = parseImports(code, [transformQuotes])
220+
// eslint-disable-next-line no-case-declarations
221+
const codeNoImporter = getCurFileContent(code, parseSassImporter.imports)
222+
// eslint-disable-next-line no-case-declarations
223+
const sassParseRes = sass.compileString(codeNoImporter, { syntax: 'indented' })
224+
res = setImportToCompileRes(sassParseRes.css, parseSassImporter.imports)
219225
break
220226
case `.${SUPPORT_FILE.LESS}`: // less
221227
// eslint-disable-next-line no-case-declarations
222-
const parseLessImporter = parseImports(code)
228+
const parseLessImporter = parseImports(code, [transformQuotes])
223229
// eslint-disable-next-line no-case-declarations
224230
const codeLessNoImporter = getCurFileContent(code, parseLessImporter.imports)
225231
less.render(codeLessNoImporter, {}, (error, output) => {
@@ -230,9 +236,8 @@ export function generateCSSCode(path: string, suffix: string) {
230236
})
231237
break
232238
case `.${SUPPORT_FILE.STYL}`: // stylus
233-
// TODO unit test
234239
// eslint-disable-next-line no-case-declarations
235-
const parseStylusImporter = parseImports(code)
240+
const parseStylusImporter = parseImports(code, [transformQuotes])
236241
// eslint-disable-next-line no-case-declarations
237242
const codeStylusNoImporter = getCurFileContent(code, parseStylusImporter.imports)
238243
stylus.render(codeStylusNoImporter, {}, (error: Error, css: string) => {
@@ -262,7 +267,7 @@ export function getCurFileContent(content: string, parseRes: ImportStatement[])
262267
mgcStr.replaceAll('@require', '')
263268
}
264269
})
265-
return mgcStr.toString()
270+
return mgcStr.toString().trimStart()
266271
}
267272

268273
export function setImportToCompileRes(content: string, parseRes: ImportStatement[]) {

packages/core/css/process-css.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import path from 'path'
2-
import * as csstree from 'css-tree'
32
import { SUPPORT_FILE, completeSuffix, transformSymbol } from '@unplugin-vue-cssvars/utils'
4-
import { walkCSSTree } from './pre-process-css'
3+
import { parseImports } from '../parser/parser-import'
54
import type { ICSSFile, ICSSFileMap } from '../types'
65
import type { SFCDescriptor } from '@vue/compiler-sfc'
76

@@ -32,10 +31,11 @@ export const createCSSModule = (descriptor: SFCDescriptor, id: string, cssFiles:
3231
// 遍历 sfc 的 style 标签内容
3332
for (let i = 0; i < descriptor.styles.length; i++) {
3433
const content = descriptor.styles[i].content
35-
const cssAst = csstree.parse(content)
36-
// 根据其 ast,获取 @import 信息
37-
walkCSSTree(cssAst, (importer) => {
38-
const lang = descriptor.styles[i].lang === SUPPORT_FILE.STYLUS ? SUPPORT_FILE.STYL : descriptor.styles[i].lang
34+
const lang = descriptor.styles[i].lang === SUPPORT_FILE.STYLUS ? SUPPORT_FILE.STYL : descriptor.styles[i].lang
35+
36+
const parseImporterRes = parseImports(content)
37+
parseImporterRes.imports.forEach((res) => {
38+
const importer = res.path
3939
// 添加后缀
4040
// sfc中规则:如果@import 指定了后缀,则根据后缀,否则根据当前 script 标签的 lang 属性(默认css)
4141
let key = completeSuffix(transformSymbol(path.resolve(path.parse(id).dir, importer)), lang)
@@ -47,7 +47,7 @@ export const createCSSModule = (descriptor: SFCDescriptor, id: string, cssFiles:
4747
getCSSFileRecursion(key, cssFiles, (res: ICSSFile) => {
4848
importModule.push(res)
4949
})
50-
}, { i: true, v: false })
50+
})
5151
}
5252
return importModule
5353
}

0 commit comments

Comments
 (0)