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
//! size-limit plugin.
//!
//! Detects size-limit projects, marks config files as always used, and credits
//! installed `@size-limit/*` and `size-limit-*` presets and plugins (loaded by
//! convention, not import).
use std::path::Path;
use fallow_config::PackageJson;
use super::Plugin;
const ENABLERS: &[&str] = &["size-limit"];
/// The two package-name prefixes size-limit's `load-plugins.js` imports from the
/// manifest: the official scope and the community `size-limit-*` form. The
/// `size-limit` tooling dependency itself matches neither prefix.
const PLUGIN_PREFIXES: &[&str] = &["@size-limit/", "size-limit-"];
/// Mirrors the lilconfig `searchPlaces` in size-limit's `get-config.js`: the
/// extensionless `.size-limit` is a JSON file there, not a stray entry.
///
/// These globs anchor at the package that activates the plugin. A hoisted
/// monorepo keeps the tool at the root and the config inside a workspace
/// package, which the unused-file config predicate covers by basename.
const ALWAYS_USED: &[&str] = &[".size-limit", ".size-limit.{json,js,cjs,mjs,ts,cts,mts}"];
const TOOLING_DEPENDENCIES: &[&str] = &["size-limit"];
pub struct SizeLimitPlugin;
impl Plugin for SizeLimitPlugin {
fn name(&self) -> &'static str {
"size-limit"
}
fn enablers(&self) -> &'static [&'static str] {
ENABLERS
}
fn always_used(&self) -> &'static [&'static str] {
ALWAYS_USED
}
fn tooling_dependencies(&self) -> &'static [&'static str] {
TOOLING_DEPENDENCIES
}
fn package_json_referenced_dependencies(&self, pkg: &PackageJson, _root: &Path) -> Vec<String> {
let mut deps: Vec<String> = pkg
.all_dependency_names()
.into_iter()
.filter(|dep| PLUGIN_PREFIXES.iter().any(|prefix| dep.starts_with(prefix)))
.collect();
deps.sort();
deps.dedup();
deps
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn credits_installed_size_limit_packages() {
let pkg: PackageJson = serde_json::from_str(
r#"{
"devDependencies": {
"size-limit": "13.0.3",
"@size-limit/preset-small-lib": "13.0.3",
"@size-limit/file": "13.0.3",
"size-limit-node-esbuild": "0.4.0",
"oxlint": "1.0.0"
}
}"#,
)
.unwrap();
assert_eq!(
SizeLimitPlugin.package_json_referenced_dependencies(&pkg, Path::new("/")),
vec![
"@size-limit/file".to_string(),
"@size-limit/preset-small-lib".to_string(),
"size-limit-node-esbuild".to_string()
]
);
}
}