swc_bundler/modules/sort/
mod.rs

1use std::time::Instant;
2
3use swc_common::{sync::Lrc, SourceMap, DUMMY_SP};
4use swc_ecma_ast::*;
5
6use super::Modules;
7use crate::{dep_graph::ModuleGraph, ModuleId};
8
9mod chunk;
10mod graph;
11mod stmt;
12#[cfg(test)]
13mod tests;
14
15impl Modules {
16    /// If module graph proves that one module can com before other module, it
17    /// will be simply injected. If it is not the case, we will consider the
18    /// dependency between statements.
19    ///
20    /// TODO: Change this to return [Module].
21    #[allow(clippy::ptr_arg)]
22    pub fn sort(
23        &mut self,
24        entry_id: ModuleId,
25        module_graph: &ModuleGraph,
26        cycles: &Vec<Vec<ModuleId>>,
27        cm: &Lrc<SourceMap>,
28    ) {
29        tracing::debug!("Sorting {:?}", entry_id);
30
31        let injected_ctxt = self.injected_ctxt;
32
33        #[cfg(not(target_arch = "wasm32"))]
34        let start = Instant::now();
35        let chunks = self.take_chunks(entry_id, module_graph, cycles, cm);
36        #[cfg(not(target_arch = "wasm32"))]
37        let dur = Instant::now() - start;
38        #[cfg(not(target_arch = "wasm32"))]
39        tracing::debug!("Sorting took {:?}", dur);
40
41        let buf = chunks
42            .into_iter()
43            .flat_map(|chunk| chunk.stmts)
44            .collect::<Vec<_>>();
45
46        let module = Module {
47            span: DUMMY_SP,
48            body: buf,
49            shebang: None,
50        };
51
52        // print_hygiene("after sort", cm, &module);
53
54        *self = Modules::from(entry_id, module, injected_ctxt);
55        tracing::debug!("Sorted {:?}", entry_id);
56    }
57}