Skip to content

Commit e899ef7

Browse files
committed
feat: enhance stylusCompiler to support debug logging and improved error handling
fix: update transformCss to correctly handle CSS class patterns refactor: change import path for CssType in transformVue and utils chore: add isNodeEnvironment utility function to check runtime environment test: update snapshots to reflect changes in component structure and styles
1 parent 6a2fd2e commit e899ef7

File tree

13 files changed

+207
-1044
lines changed

13 files changed

+207
-1044
lines changed

package.json

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,26 +80,41 @@
8080
"test": "vitest",
8181
"typecheck": "tsc --noEmit"
8282
},
83+
"peerDependencies": {
84+
"less": "^3.0.0 || ^4.0.0",
85+
"less-plugin-module-resolver": "^1.0.0",
86+
"sass": "^1.0.0",
87+
"stylus": "^0.50.0 || ^0.60.0"
88+
},
89+
"peerDependenciesMeta": {
90+
"less": {
91+
"optional": true
92+
},
93+
"less-plugin-module-resolver": {
94+
"optional": true
95+
},
96+
"sass": {
97+
"optional": true
98+
},
99+
"stylus": {
100+
"optional": true
101+
}
102+
},
83103
"dependencies": {
84104
"@babel/core": "^7.27.4",
85105
"@rollup/pluginutils": "^5.2.0",
86-
"@simon_he/clack-prompts": "^0.8.11",
87106
"@unocss/core": "^0.50.8",
88107
"@unocss/preset-uno": "^0.50.8",
89108
"@vue/babel-plugin-jsx": "^1.4.0",
90109
"@vue/compiler-sfc": "^3.5.17",
91110
"fast-glob": "^3.3.3",
92-
"less": "^4.3.0",
93-
"less-plugin-module-resolver": "^1.0.3",
94111
"node-html-parser": "^7.0.1",
95-
"picocolors": "^1.1.1",
96-
"sass": "^1.89.2",
97-
"stylus": "^0.63.0",
98112
"transform-to-unocss-core": "^0.0.68",
99113
"unplugin": "^2.3.5"
100114
},
101115
"devDependencies": {
102116
"@antfu/eslint-config": "^4.16.1",
117+
"@simon_he/clack-prompts": "^0.8.11",
103118
"@simon_he/colorize": "^0.0.1",
104119
"@types/babel__core": "^7.20.5",
105120
"@types/less": "^3.0.8",
@@ -108,11 +123,16 @@
108123
"bumpp": "^8.2.1",
109124
"eslint": "^8.57.1",
110125
"find-up": "^6.3.0",
126+
"less": "^4.3.0",
127+
"less-plugin-module-resolver": "^1.0.3",
111128
"lint-staged": "^13.3.0",
112129
"magic-string": "^0.30.17",
130+
"picocolors": "^1.1.1",
113131
"prettier": "^3.6.0",
114132
"rimraf": "^6.0.1",
133+
"sass": "^1.89.2",
115134
"simple-git-hooks": "^2.13.0",
135+
"stylus": "^0.63.0",
116136
"transform-to-unocss": "workspace:^",
117137
"tsdown": "^0.9.9",
118138
"tsx": "^3.14.0",

pnpm-lock.yaml

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

src/compilerCss.ts

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,24 @@
1-
import type { CssType } from './utils'
1+
import type { CssType } from './type'
2+
import process from 'node:process'
23
import { lessCompiler } from './lessCompiler'
34
import { sassCompiler } from './sassCompiler'
45
import { stylusCompiler } from './stylusCompiler'
6+
import { isNodeEnvironment } from './utils'
57

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-
*/
158
export function compilerCss(
169
css: string,
1710
lang: CssType,
18-
filepath?: string,
19-
globalCss?: any,
11+
filepath: string = isNodeEnvironment() ? process.cwd() : '',
12+
globalCss?: string,
13+
debug?: boolean,
2014
) {
2115
switch (lang) {
22-
case 'styl':
23-
return stylusCompiler(css, filepath, globalCss?.styl?.additionalData)
16+
case 'stylus':
17+
return stylusCompiler(css, filepath, globalCss, debug)
2418
case 'less':
25-
return lessCompiler(css, filepath, globalCss?.less?.additionalData)
19+
return lessCompiler(css, filepath, globalCss, debug)
2620
case 'scss':
27-
return sassCompiler(css, filepath, globalCss?.scss?.additionalData)
21+
return sassCompiler(css, filepath, globalCss, debug)
2822
default:
2923
return css
3024
}

src/lessCompiler.ts

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,58 @@
1-
import path from 'node:path'
2-
import process from 'node:process'
3-
import * as Url from 'node:url'
4-
51
export async function lessCompiler(
62
css: string,
7-
filepath?: string,
3+
filepath: string,
84
globalCss?: string,
9-
alias?: { [key: string]: string },
5+
debug?: boolean,
106
) {
117
if (typeof window !== 'undefined')
128
throw new Error('lessCompiler is not supported in this browser')
13-
const { LessPluginModuleResolver } = await import(
14-
'less-plugin-module-resolver'
15-
)
169

17-
let result = globalCss
18-
? `${globalCss.replace(/@(?:include|import)\s+["']([^"']*)['"]/g, (_, v) =>
19-
_.replace(v, Url.pathToFileURL(path.resolve(process.cwd(), v)) as any))}${css}`
20-
: css
10+
if (debug) {
11+
console.log(
12+
`[transform-to-tailwindcss] Compiling LESS file: ${filepath || 'unknown file'}`,
13+
)
14+
}
15+
16+
let result = globalCss ? `${globalCss}${css}` : css
17+
2118
try {
19+
// 使用用户项目中的 less 版本(通过 peerDependencies)
20+
const less = await import('less')
21+
const { LessPluginModuleResolver } = await import(
22+
'less-plugin-module-resolver'
23+
)
24+
2225
result = (
23-
await (
24-
await import('less')
25-
).default.render(result, {
26+
await less.default.render(result, {
2627
filename: filepath,
2728
plugins: [
2829
new LessPluginModuleResolver({
29-
alias: alias || {},
30+
alias: {},
3031
}),
3132
],
3233
})
3334
).css
3435
return result
3536
}
3637
catch (error: any) {
38+
if (
39+
error.code === 'MODULE_NOT_FOUND'
40+
|| error.message.includes('Cannot resolve module')
41+
) {
42+
const missingModule = error.message.includes(
43+
'less-plugin-module-resolver',
44+
)
45+
? 'less-plugin-module-resolver'
46+
: 'less'
47+
throw new Error(
48+
`${missingModule} not found. Please install it in your project:\n`
49+
+ `npm install ${missingModule}\n`
50+
+ `or\n`
51+
+ `yarn add ${missingModule}\n`
52+
+ `or\n`
53+
+ `pnpm add ${missingModule}`,
54+
)
55+
}
3756
console.error(
3857
`Error:\n transform-to-unocss(lessCompiler) ${error.toString()}`,
3958
)

0 commit comments

Comments
 (0)