swc_bundler/modules/sort/
chunk.rs

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
use std::{collections::VecDeque, iter::from_fn, mem::take, time::Instant};

use indexmap::IndexSet;
use petgraph::EdgeDirection::Outgoing;
use swc_common::{
    collections::{AHashSet, ARandomState},
    sync::Lrc,
    SourceMap, SyntaxContext,
};
use swc_ecma_ast::*;
use swc_ecma_utils::prepend_stmts;

use super::stmt::sort_stmts;
use crate::{dep_graph::ModuleGraph, modules::Modules, ModuleId};

/// The unit of sorting.
#[derive(Debug)]
pub(super) struct Chunk {
    pub stmts: Vec<ModuleItem>,
}

impl Modules {
    /// Modules with circular import relations will be in same chunk.
    #[allow(clippy::ptr_arg)]
    pub(super) fn take_chunks(
        &mut self,
        entry_id: ModuleId,
        graph: &ModuleGraph,
        cycle: &Vec<Vec<ModuleId>>,
        cm: &Lrc<SourceMap>,
    ) -> Vec<Chunk> {
        let injected_ctxt = self.injected_ctxt;
        let mut chunks = Vec::new();

        let mut modules = take(&mut self.modules);

        for (id, module) in &mut modules {
            if let Some(prepended) = self.prepended_stmts.remove(&*id) {
                prepend_stmts(&mut module.body, prepended.into_iter());
            }

            if let Some(items) = self.appended_stmts.remove(&*id) {
                module.body.extend(items);
            }
        }

        chunks.extend(toposort_real_modules(
            injected_ctxt,
            modules,
            entry_id,
            graph,
            cycle,
            cm,
        ));

        chunks
    }
}

/// Sort items topologically, while merging cycles as a
fn toposort_real_modules<'a>(
    injected_ctxt: SyntaxContext,
    mut modules: Vec<(ModuleId, Module)>,
    entry: ModuleId,
    graph: &'a ModuleGraph,
    cycles: &'a [Vec<ModuleId>],
    cm: &Lrc<SourceMap>,
) -> Vec<Chunk> {
    let mut queue = modules.iter().map(|v| v.0).collect::<VecDeque<_>>();
    queue.push_front(entry);

    let mut chunks = Vec::new();

    tracing::debug!(
        "Topologically sorting modules based on the dependency graph: ({} items)",
        modules.len()
    );

    #[cfg(not(target_arch = "wasm32"))]
    let start = Instant::now();
    let sorted_ids = toposort_real_module_ids(queue, graph, cycles).collect::<Vec<_>>();
    #[cfg(not(target_arch = "wasm32"))]
    let end = Instant::now();
    #[cfg(not(target_arch = "wasm32"))]
    tracing::debug!("Toposort of module ids took {:?}", end - start);
    for ids in sorted_ids {
        if ids.is_empty() {
            continue;
        }

        let mut stmts = Vec::new();

        for id in ids.iter().copied().rev() {
            if let Some((_, module)) = modules.iter_mut().find(|(module_id, _)| *module_id == id) {
                module
                    .body
                    .retain(|item| !matches!(item, ModuleItem::Stmt(Stmt::Empty(..))));
                if module.body.is_empty() {
                    continue;
                }

                stmts.push(take(&mut module.body));
            }
        }

        if stmts.is_empty() {
            continue;
        }

        // Skip sorting statements if there is no import.
        if ids.len() == 1 && graph.neighbors_directed(ids[0], Outgoing).count() == 0 {
            chunks.push(Chunk {
                stmts: stmts.into_iter().next().unwrap(),
            });
            continue;
        }

        let stmts = sort_stmts(injected_ctxt, stmts, cm);

        // print_hygiene(
        //     &format!("after sort: {:?}", ids),
        //     cm,
        //     &Module {
        //         span: DUMMY_SP,
        //         body: stmts.clone(),
        //         shebang: None,
        //     },
        // );

        chunks.push(Chunk { stmts })
    }

    chunks
}

fn cycles_for(
    cycles: &[Vec<ModuleId>],
    id: ModuleId,
    checked: &mut Vec<ModuleId>,
) -> IndexSet<ModuleId, ARandomState> {
    checked.push(id);
    let mut v = cycles
        .iter()
        .filter(|v| v.contains(&id))
        .flatten()
        .copied()
        .collect::<IndexSet<_, _>>();

    let ids = v.clone();

    for added in ids {
        if checked.contains(&added) {
            continue;
        }
        v.extend(cycles_for(cycles, added, checked));
    }

    v
}

fn toposort_real_module_ids<'a>(
    mut queue: VecDeque<ModuleId>,
    graph: &'a ModuleGraph,
    cycles: &'a [Vec<ModuleId>],
) -> impl 'a + Iterator<Item = Vec<ModuleId>> {
    let mut done = AHashSet::<ModuleId>::default();
    let mut errorred = AHashSet::<ModuleId>::default();

    from_fn(move || {
        while let Some(id) = queue.pop_front() {
            if done.contains(&id) {
                continue;
            }

            // dbg!(id);

            let deps = graph
                .neighbors_directed(id, Outgoing)
                .filter(|dep| !done.contains(dep))
                .collect::<Vec<_>>();

            if deps.is_empty() {
                // TODO: Add dependants

                // Emit
                done.insert(id);
                errorred.clear();
                return Some(vec![id]);
            }

            // dbg!(&deps);

            let mut all_modules_in_circle = cycles_for(cycles, id, &mut Default::default());
            all_modules_in_circle.reverse();

            // dbg!(&all_modules_in_circle);

            if all_modules_in_circle.is_empty() {
                queue.push_front(id);

                // This module does not have any circular imports.
                for dep in deps.iter().copied().rev() {
                    queue.push_front(dep);
                }

                continue;
            }

            // We need to handle dependencies of all circular modules.
            let deps_of_circle = all_modules_in_circle
                .iter()
                .flat_map(|&id| {
                    graph
                        .neighbors_directed(id, Outgoing)
                        .filter(|dep| !done.contains(dep) && !all_modules_in_circle.contains(dep))
                })
                .collect::<Vec<_>>();

            // dbg!(&deps_of_circle);

            if !deps_of_circle.is_empty() {
                if errorred.insert(id) {
                    queue.push_front(id);

                    // Handle dependencies first.
                    for dep in deps_of_circle.iter().copied().rev() {
                        queue.push_front(dep);
                    }

                    continue;
                }
                tracing::info!("Using slow, fallback logic for topological sorting");
                all_modules_in_circle.extend(deps_of_circle);
            }

            // TODO: Add dependants

            // Emit
            done.extend(all_modules_in_circle.iter().copied());
            errorred.clear();
            return Some(all_modules_in_circle.into_iter().collect());
        }

        None
    })
}