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
//! CSS dependency tracking — `@import` and `url()` references collected during printing.
use crate::SourceLocation;
// const Location = css.Location; — shadowed by the local `Location` below in Zig too.
/// Options for `analyze_dependencies` in `PrinterOptions`.
pub struct DependencyOptions {
/// Whether to remove `@import` rules.
pub remove_imports: bool,
}
/// A dependency.
pub enum Dependency {
/// An `@import` dependency.
Import(ImportDependency),
/// A `url()` dependency.
Url(UrlDependency),
}
/// A line and column position within a source file.
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub struct Location {
/// The line number, starting from 1.
pub line: u32,
/// The column number, starting from 1.
pub column: u32,
}
impl Location {
pub fn from_source_location(loc: SourceLocation) -> Location {
Location {
line: loc.line + 1,
column: loc.column,
}
}
// PORT NOTE: Zig `hash` / `eql` methods called `css.implementHash` / `css.implementEql`
// (comptime struct-field reflection). Replaced by `#[derive(Hash, PartialEq, Eq)]` above
// per PORTING.md §Comptime reflection.
}
/// An `@import` dependency.
pub struct ImportDependency {
/// The url to import.
// TODO(port): lifetime — arena-borrowed from `rule.url` (CSS arena); consider `&'bump [u8]`.
pub url: *const [u8],
/// The placeholder that the URL was replaced with.
// TODO(port): lifetime — arena-allocated by `css_modules::hash`.
pub placeholder: *const [u8],
/// An optional `supports()` condition.
// TODO(port): lifetime — arena-allocated by `to_css::string`.
pub supports: Option<*const [u8]>,
/// A media query.
// TODO(port): lifetime — arena-allocated by `to_css::string`.
pub media: Option<*const [u8]>,
/// The location of the dependency in the source file.
pub loc: SourceRange,
}
impl ImportDependency {
pub fn new<'bump>(
bump: &'bump bun_alloc::Arena,
rule: &crate::css_rules::import::ImportRule,
filename: &[u8],
local_names: Option<&crate::LocalsResultsMap>,
symbols: &bun_ast::symbol::Map,
) -> ImportDependency {
let supports: Option<*const [u8]> = if let Some(supports) = &rule.supports {
let s = crate::to_css::string(
bump,
supports,
&crate::PrinterOptions::default(),
None,
local_names,
symbols,
)
.unwrap_or_else(|_| {
panic!(
"Unreachable code: failed to stringify SupportsCondition.\n\n\
This is a bug in Bun's CSS printer. Please file a bug report at \
https://github.com/oven-sh/bun/issues/new/choose"
)
});
Some(std::ptr::from_ref::<[u8]>(bump.alloc_slice_copy(&s)))
} else {
None
};
let media: Option<*const [u8]> = if !rule.media.media_queries.is_empty() {
let s = crate::to_css::string(
bump,
&rule.media,
&crate::PrinterOptions::default(),
None,
local_names,
symbols,
)
.unwrap_or_else(|_| {
panic!(
"Unreachable code: failed to stringify MediaList.\n\n\
This is a bug in Bun's CSS printer. Please file a bug report at \
https://github.com/oven-sh/bun/issues/new/choose"
)
});
Some(std::ptr::from_ref::<[u8]>(bump.alloc_slice_copy(&s)))
} else {
None
};
let placeholder = crate::css_modules::hash(
bump,
// PORT NOTE: Zig "{s}_{s}", .{ filename, rule.url } → fmt::Arguments
format_args!(
"{}_{}",
bstr::BStr::new(filename),
bstr::BStr::new(rule.url)
),
false,
);
ImportDependency {
// TODO(zack): should we clone this? lightningcss does that
url: std::ptr::from_ref::<[u8]>(rule.url),
placeholder: std::ptr::from_ref::<[u8]>(placeholder),
supports,
media,
loc: SourceRange::new(
filename,
Location {
line: rule.loc.line + 1,
column: rule.loc.column,
},
8,
rule.url.len() + 2,
), // TODO: what about @import url(...)?
}
}
}
/// A `url()` dependency.
pub struct UrlDependency {
/// The url of the dependency.
// TODO(port): lifetime — arena-borrowed from `import_records[..].path.pretty`.
pub url: *const [u8],
/// The placeholder that the URL was replaced with.
// TODO(port): lifetime — arena-allocated by `css_modules::hash`.
pub placeholder: *const [u8],
/// The location of the dependency in the source file.
pub loc: SourceRange,
}
impl UrlDependency {
pub fn new<'bump>(
bump: &'bump bun_alloc::Arena,
url: &crate::values::url::Url,
filename: &[u8],
import_records: &[bun_ast::ImportRecord],
) -> UrlDependency {
// TODO(port): `bun_paths::fs::Path::pretty` is currently `&'static str`;
// should become `&[u8]` per PORTING.md §Strings. Until then, `.as_bytes()`.
let theurl: &[u8] = import_records[url.import_record_idx as usize].path.pretty;
let placeholder = crate::css_modules::hash(
bump,
format_args!("{}_{}", bstr::BStr::new(filename), bstr::BStr::new(theurl)),
false,
);
UrlDependency {
url: std::ptr::from_ref::<[u8]>(theurl),
placeholder: std::ptr::from_ref::<[u8]>(placeholder),
loc: SourceRange::new(filename, url.loc, 4, theurl.len()),
}
}
}
/// Represents the range of source code where a dependency was found.
pub struct SourceRange {
/// The filename in which the dependency was found.
// TODO(port): lifetime — borrowed from caller (printer's filename); arena/static.
pub file_path: *const [u8],
/// The starting line and column position of the dependency.
pub start: Location,
/// The ending line and column position of the dependency.
pub end: Location,
}
impl SourceRange {
pub fn new(filename: &[u8], loc: Location, offset: u32, len: usize) -> SourceRange {
SourceRange {
file_path: std::ptr::from_ref::<[u8]>(filename),
start: Location {
line: loc.line,
column: loc.column + offset,
},
end: Location {
line: loc.line,
column: loc.column + offset + u32::try_from(len).expect("int cast") - 1,
},
}
}
}
// ported from: src/css/dependencies.zig