swc_bundler/bundler/helpers/
mod.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
use std::sync::atomic::{AtomicBool, Ordering::SeqCst};

use once_cell::sync::Lazy;
use swc_common::{FileName, FilePathMapping, SourceMap};
use swc_ecma_ast::*;
use swc_ecma_parser::parse_file_as_module;
use swc_ecma_utils::{drop_span, prepend_stmts};

#[derive(Debug, Default)]
pub(crate) struct Helpers {
    /// `__swcpack_require__`
    pub require: AtomicBool,
}

fn parse(code: &'static str, name: &'static str) -> Vec<ModuleItem> {
    let cm = SourceMap::new(FilePathMapping::empty());
    let fm = cm.new_source_file(FileName::Custom(name.into()).into(), code.into());
    parse_file_as_module(
        &fm,
        Default::default(),
        Default::default(),
        None,
        &mut Vec::new(),
    )
    .map(|script| drop_span(script.body))
    .map_err(|_| {})
    .unwrap()
}

macro_rules! define {
    (
        $(
            $name:ident {
                build: $build:ident
            }
        )*
    ) => {
        $(
            fn $build(to: &mut Vec<ModuleItem>) {
                static STMTS: Lazy<Vec<ModuleItem>> = Lazy::new(|| {
                    parse(include_str!(concat!("_", stringify!($name), ".js")), stringify!($name))
                });

                to.extend((*STMTS).clone());
            }
        )*
    };
}

define!(require {
    build: build_swcpack_require
});

impl Helpers {
    pub fn extend(&self, rhs: &Self) {
        if rhs.require.load(SeqCst) {
            self.require.store(true, SeqCst);
        }
    }

    pub fn add_to(&self, to: &mut Vec<ModuleItem>) {
        let mut buf = Vec::new();

        if self.require.load(SeqCst) {
            build_swcpack_require(&mut buf);
        }

        prepend_stmts(to, buf.into_iter());
    }
}