webpack_stats/common/
module.rs

1/*
2 * Copyright [2022] [Kevin Velasco]
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17use crate::common::chunk::ChunkId;
18use crate::import::{ImportType, ResolvedModule};
19use meshed::prelude::*;
20use serde::{Deserialize, Serialize};
21use std::borrow::Cow;
22use std::collections::HashSet;
23use std::fmt::{Display, Formatter};
24
25use crate::rc::RefCount;
26
27pub struct IncludedModuleNames(pub HashSet<String>);
28pub type ModuleChunks = HashSet<ChunkId>;
29
30impl IncludedModuleNames {
31    pub fn included_name_suffix(&self, pattern: impl AsRef<str>) -> Option<&String> {
32        self.0.iter().find(|name| name.ends_with(pattern.as_ref()))
33    }
34
35    pub fn included_names_contains(&self, pattern: impl AsRef<str>) -> Option<&String> {
36        self.0.iter().find(|name| name.contains(pattern.as_ref()))
37    }
38}
39
40/// An internal webpack identifier for the module. Guaranteed to uniquely identify it
41/// Docs say: "(webpack)\\test\\browsertest\\lib\\index.web.js"
42#[derive(Deserialize, Debug, Hash, Eq, PartialEq, Ord, PartialOrd, Serialize)]
43#[serde(transparent)]
44pub struct ModuleIdentifier(pub RefCount<str>);
45
46impl Clone for ModuleIdentifier {
47    fn clone(&self) -> Self {
48        Self(RefCount::clone(&self.0))
49    }
50}
51
52impl Display for ModuleIdentifier {
53    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
54        Display::fmt(&self.0, f)
55    }
56}
57
58impl Identity for ModuleIdentifier {}
59
60#[derive(Deserialize, Serialize, Debug)]
61#[serde(transparent)]
62pub struct ModuleName(pub RefCount<str>);
63
64impl Default for ModuleName {
65    fn default() -> Self {
66        Self("".into())
67    }
68}
69
70impl Display for ModuleName {
71    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
72        <str as Display>::fmt(&self.0, f)
73    }
74}
75
76impl Clone for ModuleName {
77    fn clone(&self) -> Self {
78        Self(RefCount::clone(&self.0))
79    }
80}
81
82#[derive(Deserialize, Debug, Default)]
83#[serde(transparent)]
84pub struct ModuleId(u32);
85
86#[derive(Deserialize, Debug, Default)]
87#[serde(transparent)]
88pub struct RelativeModulePath<'a>(#[serde(borrow)] Cow<'a, str>);
89
90pub trait Modules<T>: Query<ModuleIdentifier, T>
91where
92    T: Module,
93{
94}
95pub trait Module:
96    Identifiable<ModuleIdentifier>
97    + Edges<ModuleIdentifier, (ImportType, ResolvedModule)>
98    + ExtractData<IncludedModuleNames>
99    + ExtractData<ModuleChunks>
100    + Label<Label = ModuleName>
101{
102}