bun_bundler 0.1.0

A Rust-native programmable browser runtime built on Servo and SpiderMonkey
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
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
use crate::mal_prelude::*;
use bstr::BStr;

use bun_alloc::Arena;
use bun_ast::{ImportKind, ImportRecord, ImportRecordFlags};
use bun_collections::{ArrayHashMap, StringArrayHashMap, VecExt};
use bun_core::handle_oom;

use crate::Graph::Graph;
use crate::bun_css::css_parser::BundlerCssRule;
use crate::bun_css::{BundlerStyleSheet, ImportConditions, LayerName};
use crate::chunk::{CssImportOrder, CssImportOrderKind, Layers};
use crate::linker_context_mod::debug;
use crate::{Index, LinkerContext};
use bun_ast::Index as AstIndex;

// PORT NOTE: Zig `entry.*` / `@memcpy` are bitwise copies of arena-backed
// `CssImportOrder` values (the inner `Vec`s point into bump arenas and are
// never individually freed). Rust's `Vec` has `Drop`, so a literal `*entry`
// is not `Copy`. We replicate the Zig bitwise-copy semantics here.
// `conditions` slabs come from `deep_clone_conditions`, which allocates them
// from the `LinkerGraph` arena (`graph.heap`). The `Vec` headers aliasing a
// slab are `mem::forget`'d everywhere (`CssImportOrder::drop` + the
// post-`visit()` forget below); the slab itself is bulk-freed with the arena.
// `wip_order`/`order` shuffles use `len`-truncation rather than
// `clear_retaining_capacity` so moved-from slots are never dropped.
#[inline(always)]
unsafe fn bitwise_copy<T>(src: &T) -> T {
    // SAFETY: `src` is a valid aligned `&T`; the `unsafe fn` contract requires
    // the caller to ensure the duplicated value's `Drop` is suppressed (arena
    // ownership, see PORT NOTE above) so the aliased buffer is never freed twice.
    unsafe { core::ptr::read(src) }
}

/// Zig: `order.len = wip_order.len; @memcpy(order.slice(), wip_order.slice());
/// wip_order.clearRetainingCapacity();` — bitwise move of arena-backed entries
/// from `wip` back into `order`'s buffer (which always has `cap >= wip.len`).
#[inline]
fn memcpy_and_reset(order: &mut Vec<CssImportOrder>, wip: &mut Vec<CssImportOrder>) {
    debug_assert!(order.capacity() >= wip.len());
    // PORT NOTE: do not Drop `order`'s prior entries — they were already
    // bitwise-copied into `wip` (see `bitwise_copy` callers above), so dropping
    // here would double-free their `conditions` buffers.
    // SAFETY: `set_len(0)` is unconditionally sound (0 ≤ capacity; shrinking
    // exposes no uninitialized range).
    unsafe { order.set_len(0) };
    // `Vec::append` = reserve (no-op given the debug_assert) +
    // `copy_nonoverlapping` into `order[0..]` + `wip.set_len(0)` — exactly the
    // original `@memcpy` + `clearRetainingCapacity` sequence.
    order.append(wip);
}

/// CSS files are traversed in depth-first postorder just like JavaScript. But
/// unlike JavaScript import statements, CSS "@import" rules are evaluated every
/// time instead of just the first time.
///
/// ```text
///    A
///   / \
///  B   C
///   \ /
///    D
/// ```
///
/// If A imports B and then C, B imports D, and C imports D, then the CSS
/// traversal order is D B D C A.
///
/// However, evaluating a CSS file multiple times is sort of equivalent to
/// evaluating it once at the last location. So we basically drop all but the
/// last evaluation in the order.
///
/// The only exception to this is "@layer". Evaluating a CSS file multiple
/// times is sort of equivalent to evaluating it once at the first location
/// as far as "@layer" is concerned. So we may in some cases keep both the
/// first and last locations and only write out the "@layer" information
/// for the first location.
pub fn find_imported_files_in_css_order<'a>(
    this: &'a mut LinkerContext,
    temp_arena: &'a Arena,
    entry_points: &[Index],
) -> Vec<CssImportOrder> {
    let _ = temp_arena;

    struct Visitor<'a> {
        arena: &'a Arena,
        // `BundledAst.css` SoA column.
        css_asts: &'a [crate::bundled_ast::CssCol],
        all_import_records: &'a [bun_ast::import_record::List<'a>],

        // PORT NOTE: Zig's `graph: *LinkerGraph` is never read in `visit()`;
        // dropped here to avoid an aliasing `&mut this.graph` borrow against
        // `arena`/`css_asts` (which already borrow `this.graph`).
        // `BackRef` (not `&'a Graph`) so the visitor's `'a` borrow stays
        // disjoint from `LinkerContext` (constructed from the raw `parse_graph`
        // backref, valid for the link step).
        parse_graph: bun_ptr::BackRef<Graph<'a>>,

        has_external_import: bool,
        visited: Vec<Index>,
        order: Vec<CssImportOrder>,
    }

    impl<'a> Visitor<'a> {
        #[inline]
        fn input_file_pretty(&self, source_index: Index) -> &BStr {
            let sources = self.parse_graph.input_files.items_source();
            BStr::new(&sources[source_index.get() as usize].path.pretty)
        }

        pub(crate) fn visit(
            &mut self,
            source_index: Index,
            wrapping_conditions: &mut Vec<ImportConditions>,
            wrapping_import_records: &mut Vec<ImportRecord>,
        ) {
            debug!(
                "Visit file: {}={}",
                source_index.get(),
                self.input_file_pretty(source_index),
            );
            // The CSS specification strangely does not describe what to do when there
            // is a cycle. So we are left with reverse-engineering the behavior from a
            // real browser. Here's what the WebKit code base has to say about this:
            //
            //   "Check for a cycle in our import chain. If we encounter a stylesheet
            //   in our parent chain with the same URL, then just bail."
            //
            // So that's what we do here. See "StyleRuleImport::requestStyleSheet()" in
            // WebKit for more information.
            for visited_source_index in self.visited.slice() {
                if visited_source_index.get() == source_index.get() {
                    debug!(
                        "Skip file: {}={}",
                        source_index.get(),
                        self.input_file_pretty(source_index),
                    );
                    return;
                }
            }

            self.visited.push(source_index);

            let Some(repr): Option<&BundlerStyleSheet> =
                self.css_asts[source_index.get() as usize].as_deref()
            else {
                return; // Sanity check
            };
            let top_level_rules = &repr.rules;

            // TODO: should we even do this? @import rules have to be the first rules in the stylesheet, why even allow pre-import layers?
            // Any pre-import layers come first
            // if len(repr.AST.LayersPreImport) > 0 {
            //     order = append(order, cssImportOrder{
            //         kind:                   cssImportLayers,
            //         layers:                 repr.AST.LayersPreImport,
            //         conditions:             wrappingConditions,
            //         conditionImportRecords: wrappingImportRecords,
            //     })
            // }

            // PORT NOTE: `defer { _ = visitor.visited.pop(); }` — explicit pop at end.
            // The defer is registered AFTER the `orelse return` above, so skipping the
            // pop on that early-return path matches the original semantics.

            // Iterate over the top-level "@import" rules
            let mut import_record_idx: usize = 0;
            for rule in top_level_rules.v.iter() {
                if let BundlerCssRule::Import(import_rule) = rule {
                    // `defer import_record_idx += 1;` — increment at end of this arm
                    let record =
                        &self.all_import_records[source_index.get() as usize][import_record_idx];

                    // Follow internal dependencies
                    if record.source_index.is_valid() {
                        // If this import has conditions, fork our state so that the entire
                        // imported stylesheet subtree is wrapped in all of the conditions
                        if import_rule.has_conditions() {
                            // Fork our state. `visit` stores a bitwise copy of
                            // `nested_conditions` into `self.order`; the slab is
                            // arena-owned, so wrap the local header in
                            // `ManuallyDrop` to avoid a double-free.
                            let mut nested_conditions = core::mem::ManuallyDrop::new(
                                deep_clone_conditions(wrapping_conditions, self.arena),
                            );
                            let mut nested_import_records =
                                shallow_clone_records(wrapping_import_records);

                            // Clone these import conditions and append them to the state
                            nested_conditions.append_assume_capacity(
                                import_rule.conditions_with_import_records(
                                    self.arena,
                                    &mut nested_import_records,
                                ),
                            );
                            self.visit(
                                record.source_index,
                                &mut nested_conditions,
                                wrapping_import_records,
                            );
                            // `nested_import_records` is *not* passed to `visit` (the
                            // outer `wrapping_import_records` is), so it is uniquely
                            // owned here — drop it normally to free the buffer.
                            drop(nested_import_records);
                            import_record_idx += 1;
                            continue;
                        }
                        self.visit(
                            record.source_index,
                            wrapping_conditions,
                            wrapping_import_records,
                        );
                        import_record_idx += 1;
                        continue;
                    }

                    // Record external depednencies
                    if !record.flags.contains(ImportRecordFlags::IS_INTERNAL) {
                        // If this import has conditions, append it to the list of overall
                        // conditions for this external import. Note that an external import
                        // may actually have multiple sets of conditions that can't be
                        // merged. When this happens we need to generate a nested imported
                        // CSS file using a data URL.
                        if import_rule.has_conditions() {
                            let mut all_conditions =
                                deep_clone_conditions(wrapping_conditions, self.arena);
                            let mut all_import_records =
                                shallow_clone_records(wrapping_import_records);
                            all_conditions.append_assume_capacity(
                                import_rule.conditions_with_import_records(
                                    self.arena,
                                    &mut all_import_records,
                                ),
                            );
                            self.order.push(CssImportOrder {
                                kind: CssImportOrderKind::ExternalPath(record.path),
                                conditions: all_conditions,
                                condition_import_records: all_import_records,
                            });
                        } else {
                            self.order.push(CssImportOrder {
                                kind: CssImportOrderKind::ExternalPath(record.path),
                                // PORT NOTE: Zig `wrapping_conditions.*` is a bitwise struct copy.
                                // SAFETY: arena-backed `Vec` header; the pushed
                                // `CssImportOrder` never drops it (see PORT NOTE at
                                // `bitwise_copy`), so the aliased buffer is freed once
                                // with the arena.
                                conditions: unsafe { bitwise_copy(wrapping_conditions) },
                                // SAFETY: same single-free invariant as `conditions`
                                // above; `CssImportOrder` suppresses `Drop`.
                                condition_import_records: unsafe {
                                    bitwise_copy(wrapping_import_records)
                                },
                            });
                        }
                        debug!(
                            "Push external: {}={}",
                            source_index.get(),
                            self.input_file_pretty(source_index),
                        );
                        self.has_external_import = true;
                    }

                    import_record_idx += 1;
                }
            }

            // Iterate over the "composes" directives. Note that the order doesn't
            // matter for these because the output order is explicitly undfened
            // in the specification.
            for record in self.all_import_records[source_index.get() as usize].as_slice() {
                if record.kind == ImportKind::Composes && record.source_index.is_valid() {
                    self.visit(
                        record.source_index,
                        wrapping_conditions,
                        wrapping_import_records,
                    );
                }
            }

            if cfg!(debug_assertions) {
                debug!(
                    "Push file: {}={}",
                    source_index.get(),
                    self.input_file_pretty(source_index),
                );
            }
            // Accumulate imports in depth-first postorder
            self.order.push(CssImportOrder {
                // PORT NOTE: `crate::Index` (= `bun_ast::Index`) and the
                // `bun_ast::Index` carried by `CssImportOrderKind::SourceIndex`
                // are TYPE_ONLY mirrors of the same Zig `bun.ast.Index`;
                // both are `#[repr(transparent)]` over `u32`.
                kind: CssImportOrderKind::SourceIndex(AstIndex(source_index.get())),
                // PORT NOTE: Zig `wrapping_conditions.*` is a bitwise struct copy.
                // SAFETY: arena-backed `Vec` header; `CssImportOrder` suppresses
                // `Drop` on it (see PORT NOTE at `bitwise_copy`), so the aliased
                // buffer is freed once with the arena.
                conditions: unsafe { bitwise_copy(wrapping_conditions) },
                condition_import_records: Vec::new(),
            });

            // PORT NOTE: explicit pop replacing `defer { _ = visitor.visited.pop(); }`
            let _ = self.visited.pop();
        }
    }

    // PORT NOTE: reshaped for borrowck — read MultiArrayList columns before constructing visitor.
    let css_asts_slice: &[crate::bundled_ast::CssCol] = this.graph.ast.items_css();
    let all_import_records_slice = this.graph.ast.items_import_records();
    let arena = this.graph.arena();

    let mut visitor = Visitor {
        arena,
        parse_graph: bun_ptr::BackRef::from(
            core::ptr::NonNull::new(this.parse_graph).expect("parse_graph set in load()"),
        ),
        visited: Vec::<Index>::init_capacity(16),
        css_asts: css_asts_slice,
        all_import_records: all_import_records_slice,
        has_external_import: false,
        order: Vec::new(),
    };
    let mut wrapping_conditions: Vec<ImportConditions> = Vec::new();
    let mut wrapping_import_records: Vec<ImportRecord> = Vec::new();
    // Include all files reachable from any entry point
    for entry_point in entry_points {
        visitor.visit(
            *entry_point,
            &mut wrapping_conditions,
            &mut wrapping_import_records,
        );
    }

    let has_external_import = visitor.has_external_import;
    let mut order = visitor.order;
    let mut wip_order = Vec::<CssImportOrder>::init_capacity(order.len() as usize);

    let css_asts: &[crate::bundled_ast::CssCol] = css_asts_slice;

    debug_css_order(this, &order, CssOrderDebugStep::BeforeHoisting);

    // CSS syntax unfortunately only allows "@import" rules at the top of the
    // file. This means we must hoist all external "@import" rules to the top of
    // the file when bundling, even though doing so will change the order of CSS
    // evaluation.
    if has_external_import {
        // Pass 1: Pull out leading "@layer" and external "@import" rules
        let mut is_at_layer_prefix = true;
        for entry in order.slice() {
            if (matches!(entry.kind, CssImportOrderKind::Layers(_)) && is_at_layer_prefix)
                || matches!(entry.kind, CssImportOrderKind::ExternalPath(_))
            {
                // SAFETY: `entry` is moved back into `order` via
                // `memcpy_and_reset` (which `set_len(0)`s without dropping), so
                // each `CssImportOrder` value is dropped at most once.
                wip_order.push(unsafe { bitwise_copy(entry) });
            }
            if !matches!(entry.kind, CssImportOrderKind::Layers(_)) {
                is_at_layer_prefix = false;
            }
        }

        // Pass 2: Append everything that we didn't pull out in pass 1
        is_at_layer_prefix = true;
        for entry in order.slice() {
            if (!matches!(entry.kind, CssImportOrderKind::Layers(_)) || !is_at_layer_prefix)
                && !matches!(entry.kind, CssImportOrderKind::ExternalPath(_))
            {
                // SAFETY: `entry` is moved back into `order` via
                // `memcpy_and_reset` (which `set_len(0)`s without dropping), so
                // each `CssImportOrder` value is dropped at most once.
                wip_order.push(unsafe { bitwise_copy(entry) });
            }
            if !matches!(entry.kind, CssImportOrderKind::Layers(_)) {
                is_at_layer_prefix = false;
            }
        }

        memcpy_and_reset(&mut order, &mut wip_order);
    }
    debug_css_order(this, &order, CssOrderDebugStep::AfterHoisting);

    // Next, optimize import order. If there are duplicate copies of an imported
    // file, replace all but the last copy with just the layers that are in that
    // file. This works because in CSS, the last instance of a declaration
    // overrides all previous instances of that declaration.
    {
        let mut source_index_duplicates: ArrayHashMap<u32, Vec<u32>> = ArrayHashMap::new();
        let mut external_path_duplicates: StringArrayHashMap<Vec<u32>> = StringArrayHashMap::new();

        let mut i: u32 = order.len() as u32;
        // PORT NOTE: reshaped for borrowck — `order.at(i)` and `order.mut_(i)`
        // cannot overlap, and `is_conditional_import_redundant` needs to read
        // both `entry.conditions` and `order.at(j).conditions`. Hold raw
        // pointers into the Vec buffer (Zig used the same pattern via
        // `*const` slice returns); `order.mut_(i)` only writes `.kind` and
        // never reallocates, so the conditions pointer stays valid.
        let order_ptr = order.as_mut_ptr();
        'next_backward: while i != 0 {
            i -= 1;
            // SAFETY: i < order.len; buffer is not reallocated in this loop.
            let entry: &CssImportOrder = unsafe { &*order_ptr.add(i as usize) };
            match &entry.kind {
                CssImportOrderKind::SourceIndex(idx) => {
                    let idx = *idx;
                    let gop = handle_oom(source_index_duplicates.get_or_put(idx.get()));
                    if !gop.found_existing {
                        *gop.value_ptr = Vec::<u32>::default();
                    }
                    for &j in gop.value_ptr.slice() {
                        // SAFETY: j < order.len; see note above.
                        let later = unsafe { &(*order_ptr.add(j as usize)).conditions };
                        if is_conditional_import_redundant(&entry.conditions, later) {
                            // This import is redundant, but it might have @layer rules.
                            // So we should keep the @layer rules so that the cascade ordering of layers
                            // is preserved
                            //
                            // PORT NOTE: `crate::bun_css::LayerName` (lifetime-erased
                            // shadow) and `::bun_css::LayerName` are distinct nominal
                            // types until the ungate shadow is removed; cast through
                            // `NonNull` to satisfy `Layers::borrow`.
                            let layer_names_ptr = core::ptr::NonNull::from(
                                &css_asts[idx.get() as usize].as_deref().unwrap().layer_names,
                            )
                            .cast::<Vec<LayerName>>();
                            order.mut_(i as usize).kind =
                                CssImportOrderKind::Layers(Layers::borrow(layer_names_ptr));
                            continue 'next_backward;
                        }
                    }
                    gop.value_ptr.push(i);
                }
                CssImportOrderKind::ExternalPath(p) => {
                    let gop = handle_oom(external_path_duplicates.get_or_put(p.text));
                    if !gop.found_existing {
                        *gop.value_ptr = Vec::<u32>::default();
                    }
                    for &j in gop.value_ptr.slice() {
                        // SAFETY: j < order.len; see note above.
                        let later = unsafe { &(*order_ptr.add(j as usize)).conditions };
                        if is_conditional_import_redundant(&entry.conditions, later) {
                            // Don't remove duplicates entirely. The import conditions may
                            // still introduce layers to the layer order. Represent this as a
                            // file with an empty layer list.
                            order.mut_(i as usize).kind =
                                CssImportOrderKind::Layers(Layers::Owned(Vec::new()));
                            continue 'next_backward;
                        }
                    }
                    gop.value_ptr.push(i);
                }
                CssImportOrderKind::Layers(_) => {}
            }
        }
    }
    debug_css_order(this, &order, CssOrderDebugStep::AfterRemovingDuplicates);

    // Then optimize "@layer" rules by removing redundant ones. This loop goes
    // forward instead of backward because "@layer" takes effect at the first
    // copy instead of the last copy like other things in CSS.
    {
        struct DuplicateEntry {
            // PORT NOTE: lifetime-erased slice header — borrows either
            // `css_asts[..].layer_names` (real `::bun_css::LayerName`) or
            // `Layers::inner()` (shadow `LayerName`). Both nominal types should
            // be reconciled; until then we compare via
            // `LayerName::eql` on the shadow type and cast at the boundary.
            // `RawSlice` (vs raw `*const [_]`) so reads go through safe
            // `.slice()` under the back-reference invariant: the borrowed
            // storage (`css_asts` arena / `Layers` Vec) outlives this loop.
            layers: bun_ptr::RawSlice<LayerName>,
            indices: Vec<u32>,
        }
        let mut layer_duplicates: Vec<DuplicateEntry> = Vec::new();

        'next_forward: for entry in order.slice_mut() {
            debug_css_order(
                this,
                &wip_order,
                CssOrderDebugStep::WhileOptimizingRedundantLayerRules,
            );
            match &mut entry.kind {
                // Simplify the conditions since we know they only wrap "@layer"
                CssImportOrderKind::Layers(layers) => {
                    // Truncate the conditions at the first anonymous layer
                    for (i, conditions) in entry.conditions.slice().iter().enumerate() {
                        // The layer is anonymous if it's a "layer" token without any
                        // children instead of a "layer(...)" token with children:
                        //
                        //   /* entry.css */
                        //   @import "foo.css" layer;
                        //
                        //   /* foo.css */
                        //   @layer foo;
                        //
                        // We don't need to generate this (as far as I can tell):
                        //
                        //   @layer {
                        //     @layer foo;
                        //   }
                        //
                        if conditions.has_anonymous_layer() {
                            // SAFETY: `i < entry.conditions.len() <= capacity`;
                            // shrinking exposes no uninitialized range. The
                            // truncated tail is arena-owned (`deep_clone_conditions`)
                            // and bulk-freed with the arena, so skipping `Drop` is sound.
                            unsafe { entry.conditions.set_len((i as u32) as usize) };
                            layers.replace(Vec::new());
                            break;
                        }
                    }

                    // If there are no layer names for this file, trim all conditions
                    // without layers because we know they have no effect.
                    //
                    // (They have no effect because this is a `.layer` import with no rules
                    //  and only layer declarations.)
                    //
                    //   /* entry.css */
                    //   @import "foo.css" layer(foo) supports(display: flex);
                    //
                    //   /* foo.css */
                    //   @import "empty.css" supports(display: grid);
                    //
                    // That would result in this:
                    //
                    //   @supports (display: flex) {
                    //     @layer foo {
                    //       @supports (display: grid) {}
                    //     }
                    //   }
                    //
                    // Here we can trim "supports(display: grid)" to generate this:
                    //
                    //   @supports (display: flex) {
                    //     @layer foo;
                    //   }
                    //
                    if layers.inner().len() == 0 {
                        let mut i: u32 = entry.conditions.len() as u32;
                        while i != 0 {
                            i -= 1;
                            let condition = entry.conditions.at(i as usize);
                            if condition.layer.is_some() {
                                break;
                            }
                            // SAFETY: `i` was just decremented from a value
                            // `<= len`, so `i < len <= capacity`. Truncated tail
                            // is arena-owned (`deep_clone_conditions`) and
                            // bulk-freed with the arena.
                            unsafe { entry.conditions.set_len((i) as usize) };
                        }
                    }

                    // Remove unnecessary entries entirely
                    if entry.conditions.len() == 0 && layers.inner().len() == 0 {
                        continue;
                    }
                }
                _ => {}
            }

            // Omit redundant "@layer" rules with the same set of layer names. Note
            // that this tests all import order entries (not just layer ones) because
            // sometimes non-layer ones can make following layer ones redundant.
            // layers_post_import
            let layers_key: *const [LayerName] = match &entry.kind {
                CssImportOrderKind::SourceIndex(idx) => {
                    // PORT NOTE: see LayerName nominal-type note above.
                    std::ptr::from_ref::<[_]>(
                        css_asts[idx.get() as usize]
                            .as_deref()
                            .unwrap()
                            .layer_names
                            .slice_const(),
                    ) as *const [LayerName]
                }
                CssImportOrderKind::Layers(layers) => layers.inner().slice_const(),
                CssImportOrderKind::ExternalPath(_) => &[][..],
            };
            // SAFETY: every match arm yields a pointer to a live slice (`css_asts`
            // arena, `entry.kind`'s `Layers`, or a static empty); the source-index
            // arm is a `*const [_]`-level cast between layout-identical `LayerName`
            // shadows (see PORT NOTE above). Valid for this loop iteration.
            let layers_key: &[LayerName] = unsafe { &*layers_key };
            let mut index: usize = 0;
            while index < layer_duplicates.len() as usize {
                let dup_layers: &[LayerName] = layer_duplicates.at(index).layers.slice();
                let both_equal = 'both_equal: {
                    if layers_key.len() != dup_layers.len() {
                        break 'both_equal false;
                    }

                    for (a, b) in layers_key.iter().zip(dup_layers) {
                        if !a.eql(b) {
                            break 'both_equal false;
                        }
                    }

                    break 'both_equal true;
                };

                if both_equal {
                    break;
                }
                index += 1;
            }
            if index == layer_duplicates.len() as usize {
                // This is the first time we've seen this combination of layer names.
                // Allocate a new set of duplicate indices to track this combination.
                layer_duplicates.push(DuplicateEntry {
                    layers: bun_ptr::RawSlice::new(layers_key),
                    indices: Vec::new(),
                });
            }
            let duplicates: &[u32] = layer_duplicates.at(index).indices.slice();
            let mut j = duplicates.len();
            while j != 0 {
                j -= 1;
                let duplicate_index = duplicates[j];
                if is_conditional_import_redundant(
                    &entry.conditions,
                    &wip_order.at(duplicate_index as usize).conditions,
                ) {
                    if !matches!(entry.kind, CssImportOrderKind::Layers(_)) {
                        // If an empty layer is followed immediately by a full layer and
                        // everything else is identical, then we don't need to emit the
                        // empty layer. For example:
                        //
                        //   @media screen {
                        //     @supports (display: grid) {
                        //       @layer foo;
                        //     }
                        //   }
                        //   @media screen {
                        //     @supports (display: grid) {
                        //       @layer foo {
                        //         div {
                        //           color: red;
                        //         }
                        //       }
                        //     }
                        //   }
                        //
                        // This can be improved by dropping the empty layer. But we can
                        // only do this if there's nothing in between these two rules.
                        if j == duplicates.len() - 1
                            && duplicate_index as usize == wip_order.len() - 1
                        {
                            let other = wip_order.at(duplicate_index as usize);
                            if matches!(other.kind, CssImportOrderKind::Layers(_))
                                && import_conditions_are_equal(
                                    entry.conditions.slice_const(),
                                    other.conditions.slice_const(),
                                )
                            {
                                // Remove the previous entry and then overwrite it below
                                // SAFETY: `duplicate_index == wip_order.len() - 1`
                                // (checked above), so the new len is `< capacity`.
                                // The truncated entry's buffers are arena-owned
                                // (`CssImportOrder` suppresses `Drop`), so skipping
                                // its destructor is the intended semantics.
                                unsafe { wip_order.set_len((duplicate_index) as usize) };
                                break;
                            }
                        }

                        // Non-layer entries still need to be present because they have
                        // other side effects beside inserting things in the layer order
                        // SAFETY: `entry` is moved back into `order` via
                        // `memcpy_and_reset` below (no `Drop` on the source slot),
                        // so each value is dropped at most once.
                        wip_order.push(unsafe { bitwise_copy(entry) });
                    }

                    // Don't add this to the duplicate list below because it's redundant
                    continue 'next_forward;
                }
            }

            layer_duplicates
                .mut_(index)
                .indices
                .push(wip_order.len() as u32);
            // SAFETY: `entry` is moved back into `order` via `memcpy_and_reset`
            // below (which `set_len(0)`s without dropping), so each value is
            // dropped at most once.
            wip_order.push(unsafe { bitwise_copy(entry) });
        }

        debug_css_order(
            this,
            &wip_order,
            CssOrderDebugStep::WhileOptimizingRedundantLayerRules,
        );

        memcpy_and_reset(&mut order, &mut wip_order);
    }
    debug_css_order(
        this,
        &order,
        CssOrderDebugStep::AfterOptimizingRedundantLayerRules,
    );

    // Finally, merge adjacent "@layer" rules with identical conditions together.
    {
        let mut did_clone: i32 = -1;
        for entry in order.slice() {
            if matches!(entry.kind, CssImportOrderKind::Layers(_)) && wip_order.len() > 0 {
                let prev_index = wip_order.len() - 1;
                let prev = wip_order.at(prev_index as usize);
                if matches!(prev.kind, CssImportOrderKind::Layers(_))
                    && import_conditions_are_equal(
                        prev.conditions.slice_const(),
                        entry.conditions.slice_const(),
                    )
                {
                    let prev_index_i32 = prev_index as i32;
                    if did_clone != prev_index_i32 {
                        did_clone = prev_index_i32;
                    }
                    // need to clone the layers here as they could be references to css ast
                    if let CssImportOrderKind::Layers(prev_layers) =
                        &mut wip_order.mut_(prev_index as usize).kind
                    {
                        if let CssImportOrderKind::Layers(entry_layers) = &entry.kind {
                            prev_layers
                                .to_owned()
                                .append_slice(entry_layers.inner().slice_const());
                        }
                    }
                }
            }
        }
        let _ = did_clone;
    }
    debug_css_order(
        this,
        &order,
        CssOrderDebugStep::AfterMergingAdjacentLayerRules,
    );

    order
}

/// Zig: `wrapping_conditions.deepCloneInfallible(visitor.arena)`.
///
/// The returned list is later bitwise-copied into `CssImportOrder` entries via
/// `bitwise_copy(wrapping_conditions)`, so callers `mem::forget` the local after
/// the recursive `visit()` to keep the aliased buffer alive. The slab is
/// allocated from `arena` (`LinkerGraph::arena()` = `graph.heap`, which
/// outlives every chunk) and is bulk-freed with the arena — every `Vec` header
/// aliasing it must be `mem::forget`'d (see `CssImportOrder::drop`). Reserves
/// one extra slot for the single `append_assume_capacity` each call site
/// performs, so the header never reallocates.
#[inline]
fn deep_clone_conditions(list: &Vec<ImportConditions>, arena: &Arena) -> Vec<ImportConditions> {
    let cap = list.len() as usize + 1;
    let slab = arena.alloc_uninit_slice::<ImportConditions>(cap);
    for (dst, src) in slab.iter_mut().zip(list.slice_const()) {
        dst.write(src.deep_clone(arena));
    }
    // SAFETY: `slab[..list.len()]` was just initialized; cap is the slab
    // length. The resulting `Vec` is never dropped (always `mem::forget`'d)
    // and never reallocates (callers only push one element via
    // `append_assume_capacity` and otherwise truncate), so the
    // global-allocator invariant of `Vec::from_raw_parts` is never exercised.
    unsafe {
        Vec::from_raw_parts(
            slab.as_mut_ptr().cast::<ImportConditions>(),
            list.len() as usize,
            cap,
        )
    }
}

/// Zig: `bun.handleOom(wrapping_import_records.clone(arena))` — shallow
/// memcpy of `ImportRecord` values into a fresh allocation.
#[inline]
fn shallow_clone_records(list: &Vec<ImportRecord>) -> Vec<ImportRecord> {
    let mut out = Vec::<ImportRecord>::init_capacity(list.len() as usize);
    for r in list.slice_const() {
        // PORT NOTE: `ImportRecord` is plain-old-data in Zig (no destructor);
        // `Path<'static>` slices borrow resolver storage. Bitwise copy matches
        // the Zig `clone(arena)` semantics.
        // SAFETY: `ImportRecord` is POD (borrowed slices, no owning `Drop`); a
        // bitwise duplicate aliasing the same resolver storage is sound and
        // neither copy frees it.
        out.append_assume_capacity(unsafe { bitwise_copy(r) });
    }
    out
}

fn import_conditions_are_equal(a: &[ImportConditions], b: &[ImportConditions]) -> bool {
    if a.len() != b.len() {
        return false;
    }

    for (ai, bi) in a.iter().zip(b) {
        if !ImportConditions::layers_eql(ai, bi)
            || !ImportConditions::supports_eql(ai, bi)
            || !ai.media.eql(&bi.media)
        {
            return false;
        }
    }

    true
}

/// Given two "@import" rules for the same source index (an earlier one and a
/// later one), the earlier one is masked by the later one if the later one's
/// condition list is a prefix of the earlier one's condition list.
///
/// For example:
///
///    // entry.css
///    @import "foo.css" supports(display: flex);
///    @import "bar.css" supports(display: flex);
///
///    // foo.css
///    @import "lib.css" screen;
///
///    // bar.css
///    @import "lib.css";
///
/// When we bundle this code we'll get an import order as follows:
///
///  1. lib.css [supports(display: flex), screen]
///  2. foo.css [supports(display: flex)]
///  3. lib.css [supports(display: flex)]
///  4. bar.css [supports(display: flex)]
///  5. entry.css []
///
/// For "lib.css", the entry with the conditions [supports(display: flex)] should
/// make the entry with the conditions [supports(display: flex), screen] redundant.
///
/// Note that all of this deliberately ignores the existence of "@layer" because
/// that is handled separately. All of this is only for handling unlayered styles.
pub(crate) fn is_conditional_import_redundant(
    earlier: &Vec<ImportConditions>,
    later: &Vec<ImportConditions>,
) -> bool {
    if later.len() > earlier.len() {
        return false;
    }

    for i in 0..later.len() as usize {
        let a = earlier.at(i);
        let b = later.at(i);

        // Only compare "@supports" and "@media" if "@layers" is equal
        if ImportConditions::layers_eql(a, b) {
            let same_supports = ImportConditions::supports_eql(a, b);
            let same_media = a.media.eql(&b.media);

            // If the import conditions are exactly equal, then only keep
            // the later one. The earlier one is redundant. Example:
            //
            //   @import "foo.css" layer(abc) supports(display: flex) screen;
            //   @import "foo.css" layer(abc) supports(display: flex) screen;
            //
            // The later one makes the earlier one redundant.
            if same_supports && same_media {
                continue;
            }

            // If the media conditions are exactly equal and the later one
            // doesn't have any supports conditions, then the later one will
            // apply in all cases where the earlier one applies. Example:
            //
            //   @import "foo.css" layer(abc) supports(display: flex) screen;
            //   @import "foo.css" layer(abc) screen;
            //
            // The later one makes the earlier one redundant.
            if same_media && b.supports.is_none() {
                continue;
            }

            // If the supports conditions are exactly equal and the later one
            // doesn't have any media conditions, then the later one will
            // apply in all cases where the earlier one applies. Example:
            //
            //   @import "foo.css" layer(abc) supports(display: flex) screen;
            //   @import "foo.css" layer(abc) supports(display: flex);
            //
            // The later one makes the earlier one redundant.
            if same_supports && b.media.media_queries.is_empty() {
                continue;
            }
        }

        return false;
    }

    true
}

#[derive(Clone, Copy)]
enum CssOrderDebugStep {
    BeforeHoisting,
    AfterHoisting,
    AfterRemovingDuplicates,
    WhileOptimizingRedundantLayerRules,
    AfterOptimizingRedundantLayerRules,
    AfterMergingAdjacentLayerRules,
}

impl CssOrderDebugStep {
    #[cfg(debug_assertions)]
    fn tag_name(self) -> &'static str {
        match self {
            Self::BeforeHoisting => "BEFORE_HOISTING",
            Self::AfterHoisting => "AFTER_HOISTING",
            Self::AfterRemovingDuplicates => "AFTER_REMOVING_DUPLICATES",
            Self::WhileOptimizingRedundantLayerRules => "WHILE_OPTIMIZING_REDUNDANT_LAYER_RULES",
            Self::AfterOptimizingRedundantLayerRules => "AFTER_OPTIMIZING_REDUNDANT_LAYER_RULES",
            Self::AfterMergingAdjacentLayerRules => "AFTER_MERGING_ADJACENT_LAYER_RULES",
        }
    }
}

fn debug_css_order(this: &LinkerContext, order: &Vec<CssImportOrder>, step: CssOrderDebugStep) {
    // PERF(port): `step` was a comptime enum param; debug-only so demoted to runtime.
    #[cfg(debug_assertions)]
    {
        // PORT NOTE: comptime `"BUN_DEBUG_CSS_ORDER_" ++ @tagName(step)` —
        // runtime concat is fine here (debug-only).
        let tag = step.tag_name();
        let env_var = format!("BUN_DEBUG_CSS_ORDER_{}\0", tag);
        let enable_all = bun_core::env_var::BUN_DEBUG_CSS_ORDER
            .get()
            .unwrap_or(false);
        let enable_step =
            bun_core::getenv_z(bun_core::ZStr::from_slice_with_nul(env_var.as_bytes()))
                .map(|v| !v.is_empty() && v != b"0" && v != b"false")
                .unwrap_or(false);
        if enable_all || enable_step {
            debug_css_order_impl(this, order, step);
        }
    }
    #[cfg(not(debug_assertions))]
    {
        let _ = (this, order, step);
    }
}

#[cfg(debug_assertions)]
fn debug_css_order_impl(
    this: &LinkerContext,
    order: &Vec<CssImportOrder>,
    step: CssOrderDebugStep,
) {
    #[cfg(debug_assertions)]
    {
        use crate::bun_css::{ImportInfo, LocalsResultsMap, Printer, PrinterOptions};

        let tag = step.tag_name();
        debug!("CSS order {}:\n", tag);

        let arena = bun_alloc::Arena::new();
        let parse_graph = this.parse_graph();
        let ast_urls_for_css = parse_graph.ast.items_url_for_css();
        // SAFETY: read-only fan-out of `&[Box<[u8]>]` as `&[&[u8]]`; relies on
        // fat-pointer field-order equivalence (see `boxed_slices_as_borrowed`).
        let unique_keys: &[&[u8]] = unsafe {
            bun_ptr::boxed_slices_as_borrowed(
                parse_graph
                    .input_files
                    .items_unique_key_for_additional_file(),
            )
        };
        // `LocalsResultsMap` is the same `ArrayHashMap<Ref, Box<[u8]>>` alias as
        // `bun_js_printer::MangledProps`; no cast needed.
        let local_names: &LocalsResultsMap = &this.mangled_props;
        let symbols = bun_ast::symbol::Map::init_list(Default::default());

        for (i, entry) in order.slice().iter().enumerate() {
            let conditions_str: std::borrow::Cow<'_, str> = if entry.conditions.len() > 0 {
                let mut writer: Vec<u8> = Vec::new();
                writer.extend_from_slice(b"[");
                for (j, condition) in entry.conditions.slice_const().iter().enumerate() {
                    let mut printer = Printer::new(
                        &arena,
                        bun_alloc::ArenaVec::new_in(&arena),
                        &mut writer,
                        &PrinterOptions::default(),
                        Some(ImportInfo {
                            import_records: &entry.condition_import_records,
                            ast_urls_for_css,
                            ast_unique_key_for_additional_file: unique_keys,
                        }),
                        Some(local_names),
                        &symbols,
                    );
                    let _ = condition.to_css(&mut printer);
                    drop(printer);
                    if j != entry.conditions.len() as usize - 1 {
                        writer.extend_from_slice(b", ");
                    }
                }
                writer.extend_from_slice(b" ]");
                String::from_utf8_lossy(&writer).into_owned().into()
            } else {
                "[]".into()
            };
            debug!("  {}: {} {}\n", i, entry.fmt(this), conditions_str);
        }
    }
    #[cfg(not(debug_assertions))]
    {
        let _ = (this, order, step);
    }
}

pub use crate::DeferredBatchTask;
pub use crate::ParseTask;
pub use crate::ThreadPool;

// ported from: src/bundler/linker_context/findImportedFilesInCSSOrder.zig