rspack_style 0.1.16

a rust toolkit bundled for front-end style files
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
use crate::css::fileinfo::{FileInfo, FileRef};
use crate::css::node::{NodeRef, StyleNode};
use crate::css::parse::Parse;
use crate::css::select_node::SelectorNode;
use crate::css::var::VarRuleNode;
use crate::extend::string::StringExtend;
use crate::sourcemap::loc::{Loc, LocMap};
use crate::style_core::context::ParseContext;
use crate::style_core::extension::StyleExtension;
use crate::style_core::filenode::StyleFileNode;
use crate::util::hash::StyleHash;
use crate::util::str_enum::StringToEnum;
use serde::Serialize;
use std::collections::{HashMap, HashSet};
use std::path::Path;

#[derive(Clone, Debug, Serialize)]
pub struct FileNode {
  pub info: FileRef,
}

impl FileNode {
  ///
  /// 收集当前节点下所有的Rule
  ///
  pub fn getrules(&self) -> Vec<NodeRef> {
    let mut list = vec![];
    self.info.borrow().block_node.iter().for_each(|x| {
      if let StyleNode::Rule(rule) = x {
        list.push(rule.clone())
      }
    });
    list
  }

  ///
  /// 生成代码
  ///
  pub fn code_gen(&self) -> Result<String, String> {
    let mut res = "".to_string();
    let mut set = HashSet::new();
    let info = self.info.borrow();
    let mut need_add_cache = false;
    if !info.import_files.is_empty() {
      for item in info.import_files.iter() {
        let has_codegen_record = {
          let context = info.context.lock().unwrap();
          context.has_codegen_record(&item.info.borrow().disk_location)
        };
        if !has_codegen_record {
          let import_res = item.code_gen()?;
          res += &import_res;
          res += "\n";
        }
      }
    }
    let mut self_code_gen_res = "".to_string();

    let source = {
      info
        .context
        .lock()
        .unwrap()
        .get_render_cache(info.disk_location.as_str())
    };
    if let Some(code) = source {
      self_code_gen_res = code;
    } else {
      for item in self.getrules() {
        item.borrow().code_gen(&mut self_code_gen_res, &mut set)?;
      }
      need_add_cache = true;
    }
    res += self_code_gen_res.as_str();
    let mut context = info.context.lock().unwrap();
    context.add_codegen_record(info.disk_location.as_str());
    // 增加缓存
    if need_add_cache {
      context.add_render_cache(info.disk_location.as_str(), self_code_gen_res.as_str());
    }
    drop(context);
    drop(info);
    if !set.is_empty() {
      self.info.borrow_mut().class_selector_collect = set.clone();
      let info = self.info.borrow();
      let key = info.disk_location.clone();
      let context = info.context.lock().unwrap();
      let mut parse_cache_map = context.filecache.lock().unwrap();
      parse_cache_map.remove(&key);
      let file_info_json = serde_json::to_string_pretty(self).unwrap();
      parse_cache_map.insert(key, file_info_json);
    }

    Ok(res)
  }

  ///
  /// 用来执行多 css 之间的 bundle
  /// 初始化 执行 需要 放入一个 空map 的引用 后续会自动填充到 该参数 上
  ///
  pub fn code_gen_into_map(&self, map: &mut HashMap<String, String>) -> Result<(), String> {
    let info = self.info.borrow();
    let mut set = HashSet::new();
    let mut need_add_cache = false;
    if !info.import_files.is_empty() {
      for item in info.import_files.iter() {
        let has_codegen_record = {
          let context = info.context.lock().unwrap();
          context.has_codegen_record(&item.info.borrow().disk_location)
        };
        if !has_codegen_record {
          item.code_gen_into_map(map)?;
        }
      }
    }
    let mut res = "".to_string();
    let source = {
      info
        .context
        .lock()
        .unwrap()
        .get_render_cache(info.disk_location.as_str())
    };
    if let Some(code) = source {
      res = code;
    } else {
      for item in self.getrules() {
        item.borrow().code_gen(&mut res, &mut set)?;
      }
      need_add_cache = true;
    }
    // 增加缓存
    let mut context = info.context.lock().unwrap();
    if need_add_cache {
      context.add_render_cache(info.disk_location.as_str(), res.as_str());
    }
    map.insert(self.info.borrow().disk_location.clone(), res);
    context.add_codegen_record(info.disk_location.as_str());
    drop(context);
    drop(info);
    // 拼接css_module 的内容
    if !set.is_empty() {
      self.info.borrow_mut().class_selector_collect = set.clone();
      let info = self.info.borrow();
      let key = info.disk_location.clone();
      let context = info.context.lock().unwrap();
      let mut parse_cache_map = context.filecache.lock().unwrap();
      parse_cache_map.remove(&key);
      let file_info_json = serde_json::to_string_pretty(self).unwrap();
      parse_cache_map.insert(key, file_info_json);
    }
    Ok(())
  }

  ///
  /// 拼接css_module 的 js 导出内容
  ///
  pub fn output_js_with_cssmodule(
    class_selector_collect: &Vec<String>,
    perfix_hash: &str,
  ) -> String {
    let mut res = "".to_string();
    for item in class_selector_collect {
      let key = if item.contains('-') {
        format!(r#"["{}"]"#, item)
      } else {
        item.to_string()
      };
      let value = format!("{}_{}", item, perfix_hash);
      res += format!(r#"{}: "{}","#, key, value).as_str();
      res += " \n";
    }
    res
  }

  ///
  /// parse 当前文件下 所有的 select 字符串
  /// 需要 第一遍 完成基本遍历
  ///
  pub fn parse_select_all_node(&self) -> Result<(), String> {
    // todo! 若要支持 @{abc} 变量 跨文件调用 select 需要 select 解析放到 codegen 里
    for node in self.info.borrow().block_node.iter() {
      if let StyleNode::Rule(heapnode) = node {
        let mut mut_node = heapnode.borrow_mut();
        if let Some(SelectorNode::Select(s_node)) = mut_node.selector.as_mut() {
          s_node.parse()?;
        }
        drop(mut_node);
        heapnode.borrow().parse_select_all_node()?;
      }
    }
    Ok(())
  }

  ///
  /// 是否需要 css module
  ///
  pub fn is_need_css_modules(filepath: &str, modules: Option<bool>) -> bool {
    if let Some(module) = modules {
      module
    } else {
      let path = Path::new(filepath);
      let filename = path.file_name().unwrap().to_str().unwrap().to_string();
      let ext = format!(".module.{}", path.extension().unwrap().to_str().unwrap());
      filename.to_lowercase().contains(&ext.to_lowercase())
    }
  }

  ///
  /// 根据文件路径 解析 文件
  ///
  pub fn create_disklocation_parse(
    filepath: String,
    context: ParseContext,
  ) -> Result<FileNode, String> {
    let cp_context = context.clone();
    let option = {
      let context_value = cp_context.lock().unwrap();
      context_value.get_options()
    };
    let need_modules = {
      let modules = context.lock().unwrap().option.modules;
      Self::is_need_css_modules(filepath.as_str(), modules)
    };
    let (abs_path, mut content) = FileInfo::resolve(filepath, &option.include_path)?;
    let content_transform = {
      context
        .lock()
        .unwrap()
        .option
        .hooks
        .content_interceptor
        .as_ref()
        .cloned()
    };
    if let Some(content_transform_fn) = content_transform {
      content = content_transform_fn(abs_path.as_str(), content.as_str())?;
    }
    let node = {
      let context_value = cp_context.lock().unwrap();
      context_value.get_parse_cache(&abs_path)?
    };
    // 缓存里有的话 直接跳出
    if let Some(StyleFileNode::Css(node)) = node {
      return Ok(node);
    }
    let text_content = content.clone();
    let charlist = content.to_char_vec();
    let mut locmap: Option<LocMap> = None;
    if option.sourcemap {
      locmap = Some(FileInfo::get_loc_by_content(&charlist));
    }
    let ext_str_value = Path::new(abs_path.as_str())
      .extension()
      .unwrap()
      .to_str()
      .unwrap();
    let ext = ext_str_value
      .to_string()
      .to_enum::<StyleExtension>()
      .unwrap();
    let obj = FileInfo {
      disk_location: abs_path.clone(),
      block_node: vec![],
      origin_txt_content: text_content,
      origin_charlist: charlist,
      locmap,
      context,
      self_weak: None,
      import_files: vec![],
      modules: need_modules,
      class_selector_collect: Default::default(),
      hash_perfix: StyleHash::generate_css_module_hash(&abs_path, &content),
      resolve_extension: ext,
    };
    let info = obj.toheap();
    let mut obj = Self { info: info.clone() };
    obj.parse_heap()?;
    obj.parse_select_all_node()?;
    // 把当前 节点 的 对象 指针 放到 节点上 缓存中
    let disk_location = info.borrow().disk_location.clone();
    let file_info_json = serde_json::to_string_pretty(&obj).unwrap();
    let mut context_value = cp_context.lock().unwrap();
    context_value.set_parse_cache(disk_location.as_str(), file_info_json);
    Ok(obj)
  }

  ///
  /// 根据文件路径 转换 文件(分页)
  ///
  pub fn create_disklocation(
    filepath: String,
    context: ParseContext,
  ) -> Result<(String, String), String> {
    let obj = Self::create_disklocation_parse(filepath, context.clone())?;
    let mut res = obj.code_gen()?;
    let mut sync_context = context.lock().unwrap();
    sync_context.clear_codegen_record();
    let mut js_content = "".to_string();
    if obj.info.borrow().modules {
      drop(sync_context);
      let perfix_hash = obj.info.borrow().hash_perfix.clone();
      res = res.replace("@@@hash_str_replace_value@@@", &perfix_hash);
      let mut js_modules_collect = HashSet::new();
      Self::collect_class_modules_set(&obj, &mut js_modules_collect);
      let mut js_modules_collect = js_modules_collect.into_iter().collect::<Vec<String>>();
      js_modules_collect.sort();
      js_content = format!(
        r#"
    const style = {}
      {}
    {};
    export default style;
    "#,
        "{",
        Self::output_js_with_cssmodule(&js_modules_collect, &perfix_hash),
        "}"
      );
    }
    Ok((res, js_content))
  }

  ///
  /// 根据文件路径 转换 文件
  ///
  pub fn create_disklocation_into_hashmap(
    filepath: String,
    context: ParseContext,
  ) -> Result<(HashMap<String, String>, String), String> {
    let obj = Self::create_disklocation_parse(filepath, context.clone())?;
    let mut map = HashMap::new();
    obj.code_gen_into_map(&mut map)?;
    let mut sync_context = context.lock().unwrap();
    sync_context.clear_codegen_record();
    let mut js_content = "".to_string();
    if obj.info.borrow().modules {
      drop(sync_context);
      let perfix_hash = obj.info.borrow().hash_perfix.clone();
      for res in map.values_mut() {
        *res = res
          .replace("@@@hash_str_replace_value@@@", &perfix_hash)
          .to_string()
      }
      let mut js_modules_collect = HashSet::new();
      Self::collect_class_modules_set(&obj, &mut js_modules_collect);
      let mut js_modules_collect = js_modules_collect.into_iter().collect::<Vec<String>>();
      js_modules_collect.sort();
      js_content = format!(
        r#"
    const style = {}
      {}
    {};
    export default style;
    "#,
        "{",
        Self::output_js_with_cssmodule(&js_modules_collect, &perfix_hash),
        "}"
      );
    }
    Ok((map, js_content))
  }

  ///
  /// 根据文件内容 解析文件
  /// 内存上 内容
  ///
  pub fn create_txt_content_parse(
    mut content: String,
    context: ParseContext,
    filename: String,
  ) -> Result<Self, String> {
    let node = {
      let context_value = context.lock().unwrap();
      context_value.get_parse_cache(&filename)?
    };
    let need_modules = {
      let modules = context.lock().unwrap().option.modules;
      Self::is_need_css_modules(filename.as_str(), modules)
    };
    // 缓存里有的话 直接跳出
    if let Some(StyleFileNode::Css(node_wrap_value)) = node {
      return Ok(node_wrap_value);
    }
    let content_transform = {
      context
        .lock()
        .unwrap()
        .option
        .hooks
        .content_interceptor
        .as_ref()
        .cloned()
    };
    if let Some(content_transform_fn) = content_transform {
      content = content_transform_fn(filename.as_str(), content.as_str())?;
    }
    let text_content: String = content.clone();
    let charlist = text_content.to_char_vec();
    let cp_context = context.clone();
    let option = {
      let sync_context = cp_context.lock().unwrap();
      sync_context.get_options()
    };
    let mut locmap: Option<LocMap> = None;
    if option.sourcemap {
      locmap = Some(FileInfo::get_loc_by_content(&charlist));
    }
    let ext_str_value = Path::new(filename.as_str())
      .extension()
      .unwrap()
      .to_str()
      .unwrap();
    let ext = ext_str_value
      .to_string()
      .to_enum::<StyleExtension>()
      .unwrap();
    let obj = FileInfo {
      disk_location: filename.clone(),
      block_node: vec![],
      origin_txt_content: text_content,
      origin_charlist: charlist,
      locmap,
      context,
      self_weak: None,
      import_files: vec![],
      modules: need_modules,
      class_selector_collect: Default::default(),
      hash_perfix: StyleHash::generate_css_module_hash(&filename, &content),
      resolve_extension: ext,
    };
    let info = obj.toheap();
    let mut obj = Self { info: info.clone() };
    obj.parse_heap()?;
    obj.parse_select_all_node()?;
    // 把当前 节点 的 对象 指针 放到 节点上 缓存中
    let disk_location = info.borrow().disk_location.clone();
    let file_info_json = serde_json::to_string_pretty(&obj).unwrap();
    {
      let mut sync_context = cp_context.lock().unwrap();
      sync_context.set_parse_cache(disk_location.as_str(), file_info_json);
    }
    Ok(obj)
  }

  ///
  /// codegen 样式内容
  /// 内存上 内容
  ///
  pub fn create_txt_content(
    content: String,
    filename: String,
    context: ParseContext,
  ) -> Result<(String, String), String> {
    let obj = Self::create_txt_content_parse(content, context.clone(), filename)?;
    let mut sync_context = context.lock().unwrap();
    let mut res = obj.code_gen()?;
    sync_context.clear_codegen_record();
    let mut js_content = "".to_string();
    if obj.info.borrow().modules {
      drop(sync_context);
      let perfix_hash = obj.info.borrow().hash_perfix.clone();
      res = res.replace("@@@hash_str_replace_value@@@", &perfix_hash);
      let mut js_modules_collect = HashSet::new();
      Self::collect_class_modules_set(&obj, &mut js_modules_collect);
      let mut js_modules_collect = js_modules_collect.into_iter().collect::<Vec<String>>();
      js_modules_collect.sort();
      js_content = format!(
        r#"
    const style = {}
      {}
    {};
    export default style;
    "#,
        "{",
        Self::output_js_with_cssmodule(&js_modules_collect, &perfix_hash),
        "}"
      );
    }
    Ok((res, js_content))
  }

  ///
  /// 根据文件路径 转换 文件(分页)
  /// 内存上 内容
  ///
  pub fn create_content_into_hashmap(
    content: String,
    filepath: String,
    context: ParseContext,
  ) -> Result<(HashMap<String, String>, String), String> {
    let obj = Self::create_txt_content_parse(content, context.clone(), filepath)?;
    let mut map = HashMap::new();
    obj.code_gen_into_map(&mut map)?;
    let mut sync_context = context.lock().unwrap();
    sync_context.clear_codegen_record();
    let mut js_content = "".to_string();
    if obj.info.borrow().modules {
      drop(sync_context);
      let perfix_hash = obj.info.borrow().hash_perfix.clone();
      for res in map.values_mut() {
        *res = res
          .replace("@@@hash_str_replace_value@@@", &perfix_hash)
          .to_string()
      }
      let mut js_modules_collect = HashSet::new();
      Self::collect_class_modules_set(&obj, &mut js_modules_collect);
      let mut js_modules_collect = js_modules_collect.into_iter().collect::<Vec<String>>();
      js_modules_collect.sort();
      js_content = format!(
        r#"
    const style = {}
      {}
    {};
    export default style;
    "#,
        "{",
        Self::output_js_with_cssmodule(&js_modules_collect, &perfix_hash),
        "}"
      );
    }
    Ok((map, js_content))
  }

  ///
  /// 收集 文件 上所有 rule 节点的 loc
  ///
  fn collect_loc(node: StyleNode, list: &mut Vec<Option<Loc>>) {
    if let StyleNode::Rule(rule) = node {
      list.push(rule.borrow().loc.as_ref().cloned());
      for child_node in &rule.borrow().block_node {
        Self::collect_loc(child_node.clone(), list);
      }
    } else if let StyleNode::Var(VarRuleNode::StyleRule(style)) = node {
      list.push(style.loc.as_ref().cloned());
    } else if let StyleNode::Comment(cc) = node {
      list.push(cc.loc.as_ref().cloned());
    }
  }

  ///
  ///  收集 文件 上所有 rule 节点的 loc
  ///
  pub fn collect_loc_list(&self) -> Vec<Option<Loc>> {
    let mut list = vec![];
    for node in &self.info.borrow().block_node {
      Self::collect_loc(node.clone(), &mut list);
    }
    list
  }

  ///
  /// 渲染时 获取 引用树上的 所有值
  ///
  pub fn collect_class_modules_set(filenode: &FileNode, set: &mut HashSet<String>) {
    let self_set = &filenode.info.borrow().class_selector_collect;
    for item in self_set {
      set.insert(item.to_string());
    }
    for file in &filenode.info.borrow().import_files {
      Self::collect_class_modules_set(file, set);
    }
  }
}