bun_css 0.1.1

A Rust-native programmable browser runtime built on Servo and SpiderMonkey
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
use core::fmt::Arguments;

use bun_alloc::Arena as Bump;
use bun_alloc::{ArenaVec as BumpVec, ArenaVecExt as _};
use bun_collections::ArrayHashMap;

use crate as css;
// TODO(port): narrow error set
pub use crate::Error;

// ─────────────────────────────────────────────────────────────────────────
// `reference_dashed`'s `dest.importRecord()` lookup is hoisted to the caller (see PORT NOTE on
// the method) to satisfy Rust borrowck (caller holds `&mut dest.css_module`).
// ─────────────────────────────────────────────────────────────────────────
pub struct CssModule<'a> {
    pub config: &'a Config,
    pub sources: &'a Vec<Box<[u8]>>,
    pub hashes: BumpVec<'a, &'a [u8]>,
    pub exports_by_source_index: BumpVec<'a, CssModuleExports<'a>>,
    pub references: &'a mut CssModuleReferences<'a>,
}

impl<'a> CssModule<'a> {
    pub fn new(
        bump: &'a Bump,
        config: &'a Config,
        sources: &'a Vec<Box<[u8]>>,
        project_root: Option<&[u8]>,
        references: &'a mut CssModuleReferences<'a>,
    ) -> CssModule<'a> {
        // TODO: this is BAAAAAAAAAAD we are going to remove it
        let hashes = 'hashes: {
            let mut hashes = BumpVec::with_capacity_in(sources.len(), bump);
            for path in sources.iter() {
                let mut alloced = false;
                let source: &[u8] = 'source: {
                    // Make paths relative to project root so hashes are stable
                    if let Some(root) = project_root {
                        // Zig: `bun.path.Platform.auto.isAbsolute(root)`
                        if bun_paths::is_absolute(root) {
                            alloced = true;
                            break 'source bump.alloc_slice_copy(
                                bun_paths::resolve_path::relative(root, path.as_ref()),
                            );
                        }
                    }
                    break 'source path.as_ref();
                };
                // PORT NOTE: Zig `defer if (alloced) arena.free(source);` — arena-allocated, bulk-freed on bump.reset()
                let _ = alloced;
                // PERF(port): was appendAssumeCapacity — profile if it shows up on a hot path
                hashes.push(hash(
                    bump,
                    format_args!("{}", bstr::BStr::new(source)),
                    matches!(config.pattern.segments.at(0), Segment::Hash),
                ));
            }
            break 'hashes hashes;
        };
        let exports_by_source_index = 'exports_by_source_index: {
            let mut exports_by_source_index = BumpVec::with_capacity_in(sources.len(), bump);
            // PERF(port): was appendNTimesAssumeCapacity — profile if it shows up on a hot path
            for _ in 0..sources.len() {
                exports_by_source_index.push(CssModuleExports::default());
            }
            break 'exports_by_source_index exports_by_source_index;
        };
        CssModule {
            config,
            sources,
            references,
            hashes,
            exports_by_source_index,
        }
    }

    // PORT NOTE: `deinit` was a no-op (`// TODO: deinit`); Drop is implicit. No `impl Drop` needed.

    pub fn get_reference(&mut self, bump: &'a Bump, name: &'a [u8], source_index: u32) {
        // PORT NOTE: Zig `getOrPut` returns an uninitialized value slot;
        // bun_collections::ArrayHashMap::get_or_put requires `V: Default`
        // (CssModuleExport can't be Default — BumpVec field). Reshaped to the
        // entry()-API instead.
        use bun_collections::array_hash_map::MapEntry;
        match self.exports_by_source_index[source_index as usize].entry(name) {
            MapEntry::Occupied(mut o) => {
                o.get_mut().is_referenced = true;
            }
            MapEntry::Vacant(v) => {
                v.insert(CssModuleExport {
                    name: self.config.pattern.write_to_string(
                        bump,
                        BumpVec::new_in(bump),
                        self.hashes[source_index as usize],
                        self.sources[source_index as usize].as_ref(),
                        name,
                    ),
                    composes: BumpVec::new_in(bump),
                    is_referenced: true,
                });
            }
        }
    }

    // PORT NOTE: Zig `referenceDashed` took `*Printer` so it could read
    // `dest.arena` and call `dest.importRecord(idx)`. In Rust the only
    // caller (`DashedIdentReference::to_css`) already holds a `&mut` borrow of
    // `dest.css_module` (which *is* `self`), so threading `&mut Printer` in
    // here would alias. The caller pre-resolves the import-record path and
    // hands it down as `specifier_path`; the fallible `importRecord` lookup
    // therefore lives at the call site, which is why this no longer returns
    // `Result<_, PrintErr>`.
    pub fn reference_dashed(
        &mut self,
        bump: &'a Bump,
        name: &'a [u8],
        from: Option<css::css_properties::css_modules::Specifier>,
        specifier_path: Option<&'a [u8]>,
        source_index: u32,
    ) -> Option<&'a [u8]> {
        use css::css_properties::css_modules::Specifier;
        let (reference, key): (CssModuleReference<'a>, &'a [u8]) = match from {
            Some(Specifier::Global) => return Some(&name[2..]),
            Some(Specifier::ImportRecordIndex(_)) => {
                let path = specifier_path
                    .expect("specifier_path required for Specifier::ImportRecordIndex");
                (
                    CssModuleReference::Dependency {
                        name: &name[2..],
                        specifier: path,
                    },
                    path,
                )
            }
            None => {
                // Local export. Mark as used.
                // PORT NOTE: Zig `getOrPut` returns an uninitialized value
                // slot; `CssModuleExport` cannot be `Default` (BumpVec field),
                // so reshape to the `entry()` API like `get_reference` above.
                use bun_collections::array_hash_map::MapEntry;
                match self.exports_by_source_index[source_index as usize].entry(name) {
                    MapEntry::Occupied(mut o) => {
                        o.get_mut().is_referenced = true;
                    }
                    MapEntry::Vacant(v) => {
                        let mut res = BumpVec::new_in(bump);
                        res.extend_from_slice(b"--");
                        v.insert(CssModuleExport {
                            name: self.config.pattern.write_to_string(
                                bump,
                                res,
                                self.hashes[source_index as usize],
                                self.sources[source_index as usize].as_ref(),
                                &name[2..],
                            ),
                            composes: BumpVec::new_in(bump),
                            is_referenced: true,
                        });
                    }
                }
                return None;
            }
        };

        let the_hash = hash(
            bump,
            format_args!(
                "{}_{}_{}",
                bstr::BStr::new(self.hashes[source_index as usize]),
                bstr::BStr::new(name),
                bstr::BStr::new(key)
            ),
            false,
        );

        // PORT NOTE: std.fmt.allocPrint(arena, "--{s}", .{the_hash}) → bump Vec
        // (bumpalo::Vec<u8> lacks io::Write; the format string was a pure concat anyway).
        let mut k = BumpVec::with_capacity_in(2 + the_hash.len(), bump);
        k.extend_from_slice(b"--");
        k.extend_from_slice(the_hash);
        let _ = self.references.put(k.into_bump_slice(), reference);

        Some(the_hash)
    }

    pub fn handle_composes(
        &mut self,
        _dest: &mut css::Printer,
        selectors: &css::selector::parser::SelectorList,
        _composes: &css::css_properties::css_modules::Composes,
        _source_index: u32,
    ) -> css::Maybe<(), css::PrinterErrorKind> {
        // let bump = dest.arena;
        for sel in selectors.v.slice() {
            if sel.len() == 1
                && matches!(
                    sel.components[0],
                    css::selector::parser::Component::Class(_)
                )
            {
                continue;
            }

            // The composes property can only be used within a simple class selector.
            return Err(css::PrinterErrorKind::invalid_composes_selector);
        }

        Ok(())
    }

    pub fn add_dashed(&mut self, bump: &'a Bump, local: &'a [u8], source_index: u32) {
        use bun_collections::array_hash_map::MapEntry;
        if let MapEntry::Vacant(v) =
            self.exports_by_source_index[source_index as usize].entry(local)
        {
            v.insert(CssModuleExport {
                // todo_stuff.depth
                name: self.config.pattern.write_to_string_with_prefix(
                    bump,
                    b"--",
                    self.hashes[source_index as usize],
                    self.sources[source_index as usize].as_ref(),
                    &local[2..],
                ),
                composes: BumpVec::new_in(bump),
                is_referenced: false,
            });
        }
    }

    pub fn add_local(
        &mut self,
        bump: &'a Bump,
        exported: &'a [u8],
        local: &'a [u8],
        source_index: u32,
    ) {
        use bun_collections::array_hash_map::MapEntry;
        if let MapEntry::Vacant(v) =
            self.exports_by_source_index[source_index as usize].entry(exported)
        {
            v.insert(CssModuleExport {
                // todo_stuff.depth
                name: self.config.pattern.write_to_string(
                    bump,
                    BumpVec::new_in(bump),
                    self.hashes[source_index as usize],
                    self.sources[source_index as usize].as_ref(),
                    local,
                ),
                composes: BumpVec::new_in(bump),
                is_referenced: false,
            });
        }
    }
}

/// Configuration for CSS modules.
pub struct Config {
    /// The name pattern to use when renaming class names and other identifiers.
    /// Default is `[hash]_[local]`.
    pub pattern: Pattern,

    /// Whether to rename dashed identifiers, e.g. custom properties.
    pub dashed_idents: bool,

    /// Whether to scope animation names.
    /// Default is `true`.
    pub animation: bool,

    /// Whether to scope grid names.
    /// Default is `true`.
    pub grid: bool,

    /// Whether to scope custom identifiers
    /// Default is `true`.
    pub custom_idents: bool,
}

impl Default for Config {
    fn default() -> Self {
        Self {
            pattern: Pattern::default(),
            dashed_idents: false,
            animation: true,
            grid: true,
            custom_idents: true,
        }
    }
}

/// A CSS modules class name pattern.
pub struct Pattern {
    /// The list of segments in the pattern.
    pub segments: crate::SmallList<Segment, 3>,
}

impl Default for Pattern {
    fn default() -> Self {
        Self {
            segments: crate::SmallList::init_inlined(&[
                Segment::Local,
                Segment::Literal(b"_"),
                Segment::Hash,
            ]),
        }
    }
}

impl Pattern {
    /// Write the substituted pattern to a destination.
    pub fn write(
        &self,
        hash_: &[u8],
        path: &[u8],
        local: &[u8],
        mut writefn: impl FnMut(&[u8], /* replace_dots: */ bool),
    ) {
        for segment in self.segments.slice() {
            match segment {
                Segment::Literal(s) => {
                    writefn(s, false);
                }
                Segment::Name => {
                    let stem = bun_paths::stem(path);
                    if bun_core::index_of(stem, b".").is_some() {
                        writefn(stem, true);
                    } else {
                        writefn(stem, false);
                    }
                }
                Segment::Local => {
                    writefn(local, false);
                }
                Segment::Hash => {
                    writefn(hash_, false);
                }
            }
        }
    }

    pub fn write_to_string_with_prefix<'a>(
        &self,
        bump: &'a Bump,
        prefix: &'static [u8],
        hash_: &[u8],
        path: &[u8],
        local: &[u8],
    ) -> &'a [u8] {
        let mut res: BumpVec<'a, u8> = BumpVec::new_in(bump);
        self.write(hash_, path, local, |slice: &[u8], replace_dots: bool| {
            res.extend_from_slice(prefix);
            if replace_dots {
                let start = res.len();
                res.extend_from_slice(slice);
                let end = res.len();
                for c in &mut res[start..end] {
                    if *c == b'.' {
                        *c = b'-';
                    }
                }
                return;
            }
            res.extend_from_slice(slice);
        });
        res.into_bump_slice()
    }

    pub fn write_to_string<'a>(
        &self,
        _bump: &'a Bump,
        res_: BumpVec<'a, u8>,
        hash_: &[u8],
        path: &[u8],
        local: &[u8],
    ) -> &'a [u8] {
        let mut res = res_;
        self.write(hash_, path, local, |slice: &[u8], replace_dots: bool| {
            if replace_dots {
                let start = res.len();
                res.extend_from_slice(slice);
                let end = res.len();
                for c in &mut res[start..end] {
                    if *c == b'.' {
                        *c = b'-';
                    }
                }
                return;
            }
            res.extend_from_slice(slice);
        });

        res.into_bump_slice()
    }
}

/// A segment in a CSS modules class name pattern.
///
/// See [Pattern](Pattern).
#[derive(Clone, Copy, PartialEq, Eq)]
pub enum Segment {
    /// A literal string segment.
    Literal(&'static [u8]),

    /// The base file name.
    Name,

    /// The original class name.
    Local,

    /// A hash of the file name.
    Hash,
}

/// A map of exported names to values.
// TODO(port): std.StringArrayHashMapUnmanaged → bun_collections::ArrayHashMap; key is arena &[u8]
pub type CssModuleExports<'a> = ArrayHashMap<&'a [u8], CssModuleExport<'a>>;

/// A map of placeholders to references.
pub type CssModuleReferences<'a> = ArrayHashMap<&'a [u8], CssModuleReference<'a>>;

/// An exported value from a CSS module.
pub struct CssModuleExport<'a> {
    /// The local (compiled) name for this export.
    pub name: &'a [u8],
    /// Other names that are composed by this export.
    pub composes: BumpVec<'a, CssModuleReference<'a>>,
    /// Whether the export is referenced in this file.
    pub is_referenced: bool,
}

/// A referenced name within a CSS module, e.g. via the `composes` property.
///
/// See [CssModuleExport](CssModuleExport).
pub enum CssModuleReference<'a> {
    /// A local reference.
    Local {
        /// The local (compiled) name for the reference.
        name: &'a [u8],
    },
    /// A global reference.
    Global {
        /// The referenced global name.
        name: &'a [u8],
    },
    /// A reference to an export in a different file.
    Dependency {
        /// The name to reference within the dependency.
        name: &'a [u8],
        /// The dependency specifier for the referenced file.
        ///
        /// import record idx
        specifier: &'a [u8],
    },
}

impl<'a> CssModuleReference<'a> {
    pub fn eql(&self, other: &Self) -> bool {
        match (self, other) {
            (Self::Local { name: a }, Self::Local { name: b }) => a == b,
            (Self::Global { name: a }, Self::Global { name: b }) => a == b,
            // .dependency => |v| bun.strings.eql(v.name, other.dependency.name) and bun.strings.eql(v.specifier, other.dependency.specifier),
            (
                Self::Dependency {
                    name: an,
                    specifier: asp,
                },
                Self::Dependency {
                    name: bn,
                    specifier: bsp,
                },
            ) => an == bn && asp == bsp,
            _ => false,
        }
    }
}

/// LAYERING: canonical implementation lives in `bun_base64::wyhash_url_safe`
/// (a leaf crate) so `bun_bundler::LinkerContext::mangle_local_css` can call
/// the *same* hasher without depending on `bun_css`. Re-export here so
/// in-crate callers (`dependencies.rs`, `rules/import.rs`) keep the
/// `css_modules::hash` path from the Zig spec.
#[inline]
pub fn hash<'a>(bump: &'a Bump, args: Arguments<'_>, at_start: bool) -> &'a [u8] {
    bun_base64::wyhash_url_safe(bump, args, at_start)
}

// ported from: src/css/css_modules.zig