Skip to content

Commit 680176f

Browse files
committed
feat: 支持css变量
1 parent 4de27ea commit 680176f

File tree

10 files changed

+303
-62
lines changed

10 files changed

+303
-62
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@
1010
import { parse } from '@tarojs/parse-css-to-stylesheet'
1111

1212
// Harmony
13-
const code = parse(jsxCode, [cssCode1, cssCode2, ...], {
13+
const { code, cssVariables } = parse(jsxCode, [cssCode1, cssCode2, ...], {
1414
platformString: 'Harmony',
1515
isEnableNesting: true // 支持解析嵌套选择器,默认关闭
1616
})
17+
// code: jsx代码 string
18+
// cssVariables: css变量 string
1719
```
1820

1921
在 Harmony 中,编译结果会依赖`@tarojs/plugin-platform-harmony-ets`中提供的几个包方法:
@@ -203,6 +205,8 @@ const code = parse(jsxCode, [cssCode1, cssCode2, ...], {
203205

204206
若使用 CSS 变量,挂载的属性应当挂载`:root`选择器上
205207

208+
⚠️:暂不支持`var传递3个及以上参数`, 如`height: var(--h, --he, 30px);``margin: var(--m, 30px 30px);`
209+
206210
```css
207211
:root {
208212
--color: #403635;

__test__/fixure/pesudo.scss

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
:root {
2-
--color: #403635;
32
--angle: 30deg;
4-
--var: var(--color, 30px)
3+
--var: var(--aaa, #00f);
4+
--color: #f00;
55
}
66

7-
.hello {
8-
height: 30px;
9-
color: var(--color);
7+
.container {
8+
height: 200px;
9+
width: 200px;
10+
background-color: var(--var, --color);
1011
}

index.d.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,7 @@ export interface ParseOptions {
99
}
1010
export interface ParseResult {
1111
code: string
12+
cssVariables?: string
1213
}
13-
export function parse(component: string, styles: Array<string>, options: ParseOptions): string
14+
export function parse(component: string, styles: Array<string>, options: ParseOptions): ParseResult
15+
export function combineCssVariables(variables: Array<string>): string | null

index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ if (!nativeBinding) {
252252
throw new Error(`Failed to load native binding`)
253253
}
254254

255-
const { parse } = nativeBinding
255+
const { parse, combineCssVariables } = nativeBinding
256256

257257
module.exports.parse = parse
258+
module.exports.combineCssVariables = combineCssVariables

src/constants.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ pub const COMBINE_NESTING_STYLE: &'static str = "__combine_nesting_style__";
88
pub const NESTINT_STYLE_DATA: &'static str = "__nesting_style_data__";
99
pub const CALC_DYMAMIC_STYLE: &'static str = "calcDynamicStyle";
1010
pub const CALC_STATIC_STYLE: &'static str = "calcStaticStyle";
11+
pub const CSS_VARIABLE_MAP: &'static str = "__css_var_map__";
12+
pub const CSS_VAR_FN: &'static str = "__var_fn";
13+
pub const LAZY_CSS_VAR_FN: &'static str = "__lazy_var_fn";
14+
1115

1216

1317
pub const RN_CONVERT_STYLE_PX_FN: &'static str = "scalePx2dp";

src/document.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ impl JSXDocument {
3333
}
3434
}
3535

36-
pub fn parse(&mut self, jsx: String, cm: Lrc<SourceMap>, comments: &SingleThreadedComments) {
36+
pub fn jsx_parse (&mut self, jsx: String, cm: Lrc<SourceMap>, comments: &SingleThreadedComments) -> Program {
3737
// 初始化 swc 的错误处理器
3838
let handler = Handler::with_tty_emitter(ColorConfig::Auto, true, false, Some(cm.clone()));
3939

@@ -59,6 +59,11 @@ impl JSXDocument {
5959
.parse_program()
6060
.map_err(|e| e.into_diagnostic(&handler).emit())
6161
.expect("解析 JSX 失败");
62+
program
63+
}
64+
65+
pub fn parse(&mut self, jsx: String, cm: Lrc<SourceMap>, comments: &SingleThreadedComments) {
66+
let program = self.jsx_parse(jsx, cm, comments);
6267

6368
let globals = Globals::default();
6469
GLOBALS.set(&globals, || {

src/lib.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,11 @@ pub struct ParseOptions {
3636
#[napi(object)]
3737
pub struct ParseResult {
3838
pub code: String,
39+
pub css_variables: Option<String>,
3940
}
4041

4142
#[napi]
42-
pub fn parse(component: String, styles: Vec<String>, options: ParseOptions) -> String {
43+
pub fn parse(component: String, styles: Vec<String>, options: ParseOptions) -> ParseResult {
4344

4445
let platform = match options.platform_string.as_str() {
4546
"ReactNative" => Platform::ReactNative,
@@ -67,6 +68,12 @@ pub fn parse(component: String, styles: Vec<String>, options: ParseOptions) -> S
6768
is_enable_nesting = style_data.has_nesting;
6869
}
6970

71+
// 解析CSS变量
72+
let variable_code = parse_css_variables::write(
73+
parse_css_variables::parse(style_data.css_variables.borrow().clone())
74+
);
75+
76+
7077
let program = Rc::new(RefCell::new(document.program.as_ref().unwrap().clone()));
7178
let jsx_record = Rc::new(RefCell::new(document.jsx_record.as_ref().unwrap().clone()));
7279
let mut style_write = StyleWrite::new(
@@ -92,5 +99,13 @@ pub fn parse(component: String, styles: Vec<String>, options: ParseOptions) -> S
9299
}
93100
let code = String::from_utf8(buf).unwrap().replace("\r\n", "\n");
94101

95-
code
102+
ParseResult {
103+
code,
104+
css_variables: variable_code,
105+
}
96106
}
107+
108+
#[napi]
109+
pub fn combine_css_variables(variables: Vec<String>) -> Option<String> {
110+
parse_css_variables::combine_css_variables(variables)
111+
}

0 commit comments

Comments
 (0)