Skip to content

Commit 6e6f681

Browse files
committed
refactor: enhance function signatures with debug and globalCss parameters for improved flexibility
1 parent 1b8a2eb commit 6e6f681

16 files changed

+226
-77
lines changed

src/cli.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { setTimeout } from 'node:timers/promises'
66
import * as p from '@simon_he/clack-prompts'
77
import colorize from '@simon_he/colorize'
88
import fg from 'fast-glob'
9-
import { transfromCode } from './transformCode'
9+
import { transformCode } from './transformCode'
1010
import { TRANSFER_FLAG } from './utils'
1111

1212
export async function cli() {
@@ -47,7 +47,7 @@ Options:
4747
s.start('Converting...')
4848
const suffix = fileDir.slice(fileDir.lastIndexOf('.') + 1) as SuffixType
4949
const code = await fs.promises.readFile(fileDir, 'utf-8')
50-
const codeTransfer = await transfromCode(code, {
50+
const codeTransfer = await transformCode(code, {
5151
filepath: fileDir,
5252
type: suffix,
5353
debug: isDebug,
@@ -128,7 +128,7 @@ Options:
128128
return
129129
}
130130
const code = await fs.promises.readFile(filepath, 'utf-8')
131-
const codeTransfer = await transfromCode(code, {
131+
const codeTransfer = await transformCode(code, {
132132
filepath,
133133
type: suffix,
134134
debug: isDebug,

src/compilerCss.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,15 @@ import { lessCompiler } from './lessCompiler'
33
import { sassCompiler } from './sassCompiler'
44
import { stylusCompiler } from './stylusCompiler'
55

6+
/**
7+
* Compiles CSS from different preprocessors (Sass, Less, Stylus) to standard CSS
8+
* @param css - The CSS/preprocessor code to compile
9+
* @param lang - The language type ('css' | 'scss' | 'less' | 'styl')
10+
* @param filepath - The file path for resolving imports and relative paths.
11+
* In plugin mode, this should always be provided by the build tool.
12+
* @param globalCss - Global CSS configuration for preprocessors
13+
* @returns Compiled CSS string or original CSS if no compilation needed
14+
*/
615
export function compilerCss(
716
css: string,
817
lang: CssType,

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { transformAstro } from './transformAstro'
2-
import { transfromCode } from './transformCode'
2+
import { transformCode } from './transformCode'
33
import { transformHtml } from './transformHtml'
44
import { transformJsx } from './transformJsx'
55
import { transformSvelte } from './transformSvelte'
@@ -21,11 +21,11 @@ export {
2121
rollupTransformToUnocss,
2222
rspackTransformToUnocss,
2323
transformAstro,
24+
transformCode,
2425
transformHtml,
2526
transformJsx,
2627
transformSvelte,
2728
transformVue,
28-
transfromCode,
2929
viteTransformToUnocss,
3030
webpackTransformToUnocss,
3131
}

src/transformAstro.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,29 @@ import { prettierCode } from './prettierCode'
22
import { transformVue } from './transformVue'
33
import { wrapperVueTemplate } from './wrapperVueTemplate'
44

5-
export async function transformAstro(
6-
code: string,
7-
isRem?: boolean,
8-
globalCss?: any,
9-
) {
5+
interface Options {
6+
filepath?: string
7+
isRem?: boolean
8+
globalCss?: any
9+
debug?: boolean
10+
}
11+
12+
export async function transformAstro(code: string, options?: Options) {
13+
const { filepath, isRem, globalCss, debug = false } = options || {}
1014
const match = code.match(/(---.*---)?(.*(?=<style>))(<style>.*<\/style>)?/s)
1115
if (!match)
1216
return code
1317

1418
const [_all, _js, template, css] = match
1519
const _css = css ? css.replace(/<style>(.*)<\/style>/s, '$1') : ''
1620
const _template = wrapperVueTemplate(template, _css)
17-
const vue = await transformVue(_template, { isJsx: true, isRem, globalCss })
21+
const vue = await transformVue(_template, {
22+
isJsx: true,
23+
isRem,
24+
globalCss,
25+
filepath,
26+
debug,
27+
})
1828
vue.replace(
1929
/<template>(.*)<\/template>\s*<style scoped>(.*)<\/style>/s,
2030
(_, newTemplate, newCss) =>

src/transformCode.ts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ interface Options {
1414
debug?: boolean
1515
}
1616

17-
export async function transfromCode(code: string, options: Options) {
17+
export async function transformCode(code: string, options: Options) {
1818
const {
1919
filepath,
2020
isRem,
@@ -25,25 +25,28 @@ export async function transfromCode(code: string, options: Options) {
2525
} = options || {}
2626

2727
if (debug) {
28-
console.log('[DEBUG] transfromCode started:', {
29-
filepath,
30-
type,
31-
isJsx,
32-
isRem,
33-
codeLength: code.length,
34-
})
28+
console.log(
29+
'[DEBUG] transformCode started:',
30+
JSON.stringify({
31+
filepath,
32+
type,
33+
isJsx,
34+
isRem,
35+
codeLength: code.length,
36+
}),
37+
)
3538
}
3639

3740
// 删除代码中的注释部分
3841
// code = code.replace(/\/\*[\s\S]*?\*\/|\/\/.*/g, '')
3942
if (type === 'tsx')
40-
return transformJsx(code, filepath, isRem, globalCss)
43+
return transformJsx(code, { filepath, isRem, globalCss, debug })
4144
if (type === 'html')
42-
return transformHtml(code, filepath, isRem)
45+
return transformHtml(code, { filepath, globalCss, debug })
4346
if (type === 'svelte')
44-
return transformSvelte(code, isRem, globalCss)
47+
return transformSvelte(code, { filepath, isRem, globalCss, debug })
4548
if (type === 'astro')
46-
return transformAstro(code, isRem, globalCss)
49+
return transformAstro(code, { filepath, isRem, globalCss, debug })
4750

4851
return transformVue(code, { isJsx, filepath, isRem, globalCss, debug })
4952
}

src/transformCss.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,19 @@ export async function transformCss(
5353
filepath?: string,
5454
_isRem?: boolean,
5555
debug = false,
56+
globalCss?: any,
5657
): Promise<string> {
5758
isRem = _isRem
5859
const allChanges: AllChange[] = []
5960
const { parse } = await getVueCompilerSfc()
60-
let newCode = (await importCss(code, style, filepath, isJsx, debug)) as string
61+
let newCode = (await importCss(
62+
code,
63+
style,
64+
filepath,
65+
isJsx,
66+
debug,
67+
globalCss,
68+
)) as string
6169

6270
if (debug) {
6371
console.log(
@@ -330,6 +338,7 @@ async function importCss(
330338
filepath?: string,
331339
isJsx?: boolean,
332340
debug = false,
341+
globalCss?: any,
333342
) {
334343
if (debug) {
335344
console.log(
@@ -367,14 +376,20 @@ async function importCss(
367376
'utf-8',
368377
)
369378
const type = getCssType(url)
370-
const css = await compilerCss(content, type)
379+
const css = await compilerCss(content, type, url, globalCss)
371380

372381
const [_, beforeStyle] = code.match(/<style.*>(.*)<\/style>/s)!
373382
code = code.replace(beforeStyle, '')
374383

375384
const vue = wrapperVueTemplate(code, css)
376385

377-
const transfer = await transformVue(vue, { isJsx, isRem })
386+
const transfer = await transformVue(vue, {
387+
isJsx,
388+
isRem,
389+
filepath,
390+
globalCss,
391+
debug,
392+
})
378393

379394
if (diffTemplateStyle(transfer, vue)) {
380395
code = originCode

src/transformHtml.ts

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,25 @@ import { wrapperVueTemplate } from './wrapperVueTemplate'
88
const linkCssReg = /<link.*href="(.+css)".*>/g
99
const styleReg = /\s*<style[^>]*>(.*)<\/style>\s*/s
1010

11-
export async function transformHtml(
12-
code: string,
13-
filepath?: string,
14-
isRem?: boolean,
15-
) {
11+
interface Options {
12+
filepath?: string
13+
isRem?: boolean
14+
globalCss?: any
15+
debug?: boolean
16+
}
17+
18+
export async function transformHtml(code: string, options?: Options) {
19+
const { filepath, isRem, globalCss, debug = false } = options || {}
1620
const css = await getLinkCss(code, filepath!)
1721
const style = getStyleCss(code)
18-
const newCode = await generateNewCode(css, style, code, isRem)
22+
const newCode = await generateNewCode(
23+
css,
24+
style,
25+
code,
26+
isRem,
27+
globalCss,
28+
debug,
29+
)
1930
return prettierCode(newCode)
2031
}
2132

@@ -58,13 +69,20 @@ async function generateNewCode(
5869
style: string,
5970
code: string,
6071
isRem?: boolean,
72+
globalCss?: any,
73+
debug = false,
6174
) {
6275
// 先处理style
6376
let template = getBody(code)
6477
const originBody = template
6578
if (style) {
6679
const vue = wrapperVueTemplate(template, style)
67-
const transferCode = await transformVue(vue, { isJsx: true, isRem })
80+
const transferCode = await transformVue(vue, {
81+
isJsx: true,
82+
isRem,
83+
globalCss,
84+
debug,
85+
})
6886
template = transferCode
6987

7088
// 如果没有style scoped 删除style
@@ -76,7 +94,12 @@ async function generateNewCode(
7694
const { url, content } = c
7795
const vue = wrapperVueTemplate(template, content)
7896

79-
const transferCode = await transformVue(vue, { isJsx: true, isRem })
97+
const transferCode = await transformVue(vue, {
98+
isJsx: true,
99+
isRem,
100+
globalCss,
101+
debug,
102+
})
80103

81104
if (diffTemplateStyle(template, transferCode)) {
82105
// 新增的css全部被转换了,这个link可以被移除了

src/transformJsx.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@ import { parse as babelParse, traverse as babelTraverse } from '@babel/core'
44
import vueJsxPlugin from '@vue/babel-plugin-jsx'
55
import { transformVue } from './transformVue'
66

7-
export async function transformJsx(
8-
code: string,
9-
filepath?: string,
10-
isRem?: boolean,
11-
globalCss?: any,
12-
) {
7+
interface Options {
8+
filepath?: string
9+
isRem?: boolean
10+
globalCss?: any
11+
debug?: boolean
12+
}
13+
14+
export async function transformJsx(code: string, options?: Options) {
15+
const { filepath, isRem, globalCss, debug = false } = options || {}
1316
try {
1417
const ast = babelParse(code, {
1518
babelrc: false,
@@ -49,6 +52,8 @@ export async function transformJsx(
4952
isJsx,
5053
isRem,
5154
globalCss,
55+
filepath,
56+
debug,
5257
})
5358
// vueTransfer = vueTransfer.replace(/class/g, 'className')
5459
if (cssPath) {

0 commit comments

Comments
 (0)