Skip to content

Commit f767717

Browse files
committed
feat: 将处理动态类的方法踢出去
1 parent 27b8581 commit f767717

File tree

4 files changed

+36
-67
lines changed

4 files changed

+36
-67
lines changed

__test__/index.spec.mjs.md

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Generated by [AVA](https://avajs.dev).
1010
1111
`import React from 'react';␊
1212
import './Mod.scss';␊
13+
import { calcDynamicStyle as calcDynamicStyle } from "@tarojs/runtime";␊
1314
var __inner_style__ = {␊
1415
"cnt_col": {␊
1516
display: "flex",␊
@@ -158,17 +159,6 @@ Generated by [AVA](https://avajs.dev).
158159
display: "inline-flex"␊
159160
}␊
160161
};␊
161-
function __calc_style__(classnames, styleObj) {␊
162-
var styleObjs = [];␊
163-
var classes = classnames.split(' ');␊
164-
for(var i = 0; i < classes.length; i++){␊
165-
styleObjs.push(classes[i] in window.__inner_style__ ? window.__inner_style__[classes[i]] : {});␊
166-
}␊
167-
styleObjs.push(styleObj);␊
168-
return Object.assign.apply(null, [␊
169-
{}␊
170-
].concat(styleObjs));␊
171-
}␊
172162
function Cc() {␊
173163
return <div className='cc'>␊
174164
@@ -242,7 +232,7 @@ Generated by [AVA](https://avajs.dev).
242232
height: 800␊
243233
}}>␊
244234
245-
<div className={classnames('cnt_row')} style={__calc_style__(classnames('cnt_row'), null)}>␊
235+
<div className={classnames('cnt_row')} style={calcDynamicStyle(__inner_style__, classnames('cnt_row'), null)}>␊
246236
247237
<>␊
248238

__test__/index.spec.mjs.snap

-128 Bytes
Binary file not shown.

src/style_write.rs

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
use std::{cell::RefCell, collections::HashMap, rc::Rc};
22

3-
use swc_common::{
4-
errors::{ColorConfig, Handler},
5-
sync::Lrc,
6-
SourceMap,
7-
};
83
use swc_ecma_ast::Program;
9-
use swc_ecma_parser::{lexer::Lexer, Parser, StringInput, Syntax};
104
use swc_ecma_visit::VisitMutWith;
115

126
use crate::{
@@ -37,33 +31,6 @@ impl<'i> StyleWrite<'i> {
3731
}
3832

3933
pub fn write(&mut self) {
40-
let insert_code = "
41-
function __calc_style__(classnames, styleObj) {
42-
var styleObjs = [];
43-
var classes = classnames.split(' ');
44-
for (var i = 0; i < classes.length; i++) {
45-
styleObjs.push(classes[i] in window.__inner_style__ ? window.__inner_style__[classes[i]] : {});
46-
}
47-
styleObjs.push(styleObj);
48-
return Object.assign.apply(null, [{}].concat(styleObjs));
49-
}
50-
";
51-
let cm: Lrc<SourceMap> = Default::default();
52-
let fm = cm.new_source_file(swc_common::FileName::Anon, insert_code.to_string());
53-
let handler = Handler::with_tty_emitter(ColorConfig::Auto, true, false, Some(cm.clone()));
54-
let lexer = Lexer::new(
55-
Syntax::default(),
56-
Default::default(),
57-
StringInput::from(&*fm),
58-
None,
59-
);
60-
let mut parser = Parser::new_from(lexer);
61-
let insert_module = Rc::new(RefCell::new(
62-
parser
63-
.parse_module()
64-
.map_err(|e| e.into_diagnostic(&handler).emit())
65-
.expect("解析插入代码失败"),
66-
));
6734
{
6835
let mut jsx_mut_visitor =
6936
JSXMutVisitor::new(self.jsx_record.clone(), self.style_record.clone());
@@ -73,8 +40,7 @@ function __calc_style__(classnames, styleObj) {
7340
.visit_mut_with(&mut jsx_mut_visitor);
7441
}
7542
{
76-
let mut insert_mut_visitor =
77-
ModuleMutVisitor::new(self.all_style.clone(), insert_module.clone());
43+
let mut insert_mut_visitor = ModuleMutVisitor::new(self.all_style.clone());
7844
self
7945
.module
8046
.borrow_mut()

src/visitor.rs

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@ use lightningcss::{
1313
};
1414
use swc_common::{Span, DUMMY_SP};
1515
use swc_ecma_ast::{
16-
BindingIdent, CallExpr, Callee, Decl, Expr, ExprOrSpread, Ident, ImportDecl, ImportSpecifier,
17-
JSXAttr, JSXAttrName, JSXAttrOrSpread, JSXAttrValue, JSXElement, JSXElementName, JSXExpr,
18-
JSXExprContainer, JSXFragment, KeyValueProp, Lit, Module, ModuleDecl, ModuleItem, Null,
19-
ObjectLit, Pat, Prop, PropName, PropOrSpread, Stmt, Str, VarDecl, VarDeclKind, VarDeclarator,
16+
BindingIdent, CallExpr, Callee, Decl, Expr, ExprOrSpread, Ident, ImportDecl,
17+
ImportNamedSpecifier, ImportSpecifier, JSXAttr, JSXAttrName, JSXAttrOrSpread, JSXAttrValue,
18+
JSXElement, JSXElementName, JSXExpr, JSXExprContainer, JSXFragment, KeyValueProp, Lit, Module,
19+
ModuleDecl, ModuleExportName, ModuleItem, Null, ObjectLit, Pat, Prop, PropName, PropOrSpread,
20+
Stmt, Str, VarDecl, VarDeclKind, VarDeclarator,
2021
};
2122
use swc_ecma_visit::{
2223
noop_visit_mut_type, noop_visit_type, Visit, VisitAll, VisitAllWith, VisitMut, VisitMutWith,
@@ -177,19 +178,12 @@ impl<'a> VisitAll for AstVisitor<'a> {
177178
}
178179

179180
pub struct ModuleMutVisitor<'a> {
180-
pub insert_module: Rc<RefCell<Module>>,
181181
pub all_style: Rc<RefCell<HashMap<String, StyleDeclaration<'a>>>>,
182182
}
183183

184184
impl<'a> ModuleMutVisitor<'a> {
185-
pub fn new(
186-
all_style: Rc<RefCell<HashMap<String, StyleDeclaration<'a>>>>,
187-
insert_module: Rc<RefCell<Module>>,
188-
) -> Self {
189-
ModuleMutVisitor {
190-
all_style,
191-
insert_module,
192-
}
185+
pub fn new(all_style: Rc<RefCell<HashMap<String, StyleDeclaration<'a>>>>) -> Self {
186+
ModuleMutVisitor { all_style }
193187
}
194188
}
195189

@@ -271,15 +265,29 @@ impl<'a> VisitMut for ModuleMutVisitor<'a> {
271265
if last_import_index != 0 {
272266
last_import_index += 1;
273267
}
268+
// 插入代码 import { calcDynamicStyle } from '@tarojs/runtime'
269+
module.body.insert(
270+
last_import_index,
271+
ModuleItem::ModuleDecl(ModuleDecl::Import(ImportDecl {
272+
span: DUMMY_SP,
273+
specifiers: vec![ImportSpecifier::Named(ImportNamedSpecifier {
274+
span: DUMMY_SP,
275+
local: Ident::new("calcDynamicStyle".into(), DUMMY_SP),
276+
imported: Some(ModuleExportName::Ident(Ident::new(
277+
"calcDynamicStyle".into(),
278+
DUMMY_SP,
279+
))),
280+
is_type_only: false,
281+
})],
282+
src: Box::new(Str::from("@tarojs/runtime")),
283+
type_only: false,
284+
with: None,
285+
})),
286+
);
287+
last_import_index += 1;
274288
module
275289
.body
276290
.insert(last_import_index, ModuleItem::Stmt(inner_style_stmt));
277-
for item in self.insert_module.borrow().body.iter() {
278-
if last_import_index != 0 {
279-
last_import_index += 1;
280-
}
281-
module.body.insert(last_import_index, item.clone());
282-
}
283291
}
284292
}
285293

@@ -586,10 +594,15 @@ impl<'a> VisitMut for JSXMutVisitor<'a> {
586594
let fun_call_expr = Expr::Call(CallExpr {
587595
span: DUMMY_SP,
588596
callee: Callee::Expr(Box::new(Expr::Ident(Ident::new(
589-
"__calc_style__".into(),
597+
"calcDynamicStyle".into(),
590598
DUMMY_SP,
591599
)))),
592600
args: vec![
601+
ExprOrSpread::from(Box::new(Expr::Ident(Ident {
602+
span: DUMMY_SP,
603+
sym: "__inner_style__".into(),
604+
optional: false,
605+
}))),
593606
match class_attr_value {
594607
Some(value) => ExprOrSpread::from(Box::new(value)),
595608
None => ExprOrSpread::from(Box::new(Expr::Lit(Lit::Null(Null { span: DUMMY_SP })))),

0 commit comments

Comments
 (0)