swc_bundler/bundler/chunk/
cjs.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
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
use std::{collections::HashMap, sync::atomic::Ordering};

use anyhow::Error;
use swc_common::{collections::AHashMap, Span, SyntaxContext, DUMMY_SP};
use swc_ecma_ast::*;
use swc_ecma_utils::{quote_ident, ExprFactory};
use swc_ecma_visit::{noop_visit_mut_type, VisitMut, VisitMutWith};

use crate::{
    bundler::{chunk::merge::Ctx, load::TransformedModule},
    modules::Modules,
    Bundler, Load, Resolve,
};

impl<L, R> Bundler<'_, L, R>
where
    L: Load,
    R: Resolve,
{
    fn make_cjs_load_var(&self, info: &TransformedModule, span: Span) -> Ident {
        Ident::new("load".into(), span, info.export_ctxt())
    }

    pub(super) fn replace_cjs_require_calls(
        &self,
        info: &TransformedModule,
        module: &mut Modules,
        is_entry: bool,
    ) {
        if !self.config.require {
            return;
        }

        let mut v = RequireReplacer {
            is_entry,
            base: info,
            bundler: self,
            replaced: false,
        };
        module.visit_mut_with(&mut v);

        if v.replaced {
            info.helpers.require.store(true, Ordering::SeqCst);
        }
    }

    /// Creates a variable named `load` (see `make_cjs_load_var`) if it's a
    /// common js module.
    pub(super) fn wrap_cjs_module(
        &self,
        ctx: &Ctx,
        info: &TransformedModule,
        mut module: Modules,
    ) -> Result<Modules, Error> {
        if !self.config.require || (!self.scope.is_cjs(info.id) && info.is_es6) {
            return Ok(module);
        }

        tracing::debug!("Merging as a common js module: {}", info.fm.name);

        let load_var = self.make_cjs_load_var(info, DUMMY_SP);

        module.visit_mut_with(&mut DefaultHandler {
            local_ctxt: info.local_ctxt(),
        });
        module.sort(info.id, &ctx.graph, &ctx.cycles, &self.cm);

        let stmt = wrap_module(
            SyntaxContext::empty(),
            SyntaxContext::empty().apply_mark(self.unresolved_mark),
            info.local_ctxt(),
            load_var,
            module.into(),
        )
        .into();

        let wrapped = Modules::from(
            info.id,
            Module {
                span: DUMMY_SP,
                body: vec![stmt],
                shebang: None,
            },
            self.injected_ctxt,
        );

        tracing::debug!("Injected a variable named `load` for a common js module");

        Ok(wrapped)
    }
}

fn wrap_module(
    helper_ctxt: SyntaxContext,
    unresolved_ctxt: SyntaxContext,
    local_ctxt: SyntaxContext,
    load_var: Ident,
    mut dep: Module,
) -> Stmt {
    {
        // Remap syntax context of `module` and `exports`
        // Those are unresolved, but it's actually an injected variable.

        let mut from = HashMap::default();
        from.insert(("module".into(), unresolved_ctxt), local_ctxt);
        from.insert(("exports".into(), unresolved_ctxt), local_ctxt);

        dep.visit_mut_with(&mut Remapper { vars: from })
    }

    // ... body of foo
    let module_fn: Expr = FnExpr {
        ident: None,
        function: Box::new(Function {
            params: vec![
                // module
                Param {
                    span: DUMMY_SP,
                    decorators: Default::default(),
                    pat: Pat::Ident(Ident::new("module".into(), DUMMY_SP, local_ctxt).into()),
                },
                // exports
                Param {
                    span: DUMMY_SP,
                    decorators: Default::default(),
                    pat: Pat::Ident(Ident::new("exports".into(), DUMMY_SP, local_ctxt).into()),
                },
            ],
            decorators: Vec::new(),
            span: DUMMY_SP,
            body: Some(BlockStmt {
                span: dep.span,
                stmts: dep
                    .body
                    .into_iter()
                    .map(|v| match v {
                        ModuleItem::ModuleDecl(i) => {
                            unreachable!("module item found but is_es6 is false: {:?}", i)
                        }
                        ModuleItem::Stmt(s) => s,
                    })
                    .collect(),
                ..Default::default()
            }),
            is_generator: false,
            is_async: false,
            ..Default::default()
        }),
    }
    .into();

    // var load = __swcpack_require__.bind(void 0, moduleDecl)

    VarDecl {
        span: DUMMY_SP,
        kind: VarDeclKind::Var,
        declare: false,
        decls: vec![VarDeclarator {
            span: DUMMY_SP,
            name: Pat::Ident(load_var.into()),
            init: Some(Box::new(Expr::Call(CallExpr {
                span: DUMMY_SP,
                callee: Ident::new("__swcpack_require__".into(), DUMMY_SP, helper_ctxt)
                    .make_member(quote_ident!("bind"))
                    .as_callee(),
                args: vec![Expr::undefined(DUMMY_SP).as_arg(), module_fn.as_arg()],
                ..Default::default()
            }))),
            definite: false,
        }],
        ..Default::default()
    }
    .into()
}

struct RequireReplacer<'a, 'b, L, R>
where
    L: Load,
    R: Resolve,
{
    base: &'a TransformedModule,
    bundler: &'a Bundler<'b, L, R>,
    replaced: bool,
    is_entry: bool,
}

impl<L, R> VisitMut for RequireReplacer<'_, '_, L, R>
where
    L: Load,
    R: Resolve,
{
    noop_visit_mut_type!(fail);

    fn visit_mut_call_expr(&mut self, node: &mut CallExpr) {
        node.visit_mut_children_with(self);

        if let Callee::Expr(e) = &node.callee {
            if let Expr::Ident(i) = &**e {
                // TODO: Check for global mark
                if i.sym == *"require" && node.args.len() == 1 {
                    if let Expr::Lit(Lit::Str(module_name)) = &*node.args[0].expr {
                        if self.bundler.is_external(&module_name.value) {
                            return;
                        }
                        let load = CallExpr {
                            span: node.span,
                            callee: Ident::new("load".into(), i.span, i.ctxt).as_callee(),
                            args: Vec::new(),
                            ..Default::default()
                        };
                        self.replaced = true;
                        *node = load;

                        tracing::trace!("Found, and replacing require");
                    }
                }
            }
        }
    }

    fn visit_mut_module_item(&mut self, node: &mut ModuleItem) {
        node.visit_mut_children_with(self);

        if !self.is_entry {
            return;
        }

        if let ModuleItem::ModuleDecl(ModuleDecl::Import(i)) = node {
            let dep_module_id = self
                .base
                .imports
                .specifiers
                .iter()
                .find(|(src, _)| src.src.value == i.src.value)
                .map(|v| v.0.module_id);
            let dep_module_id = match dep_module_id {
                Some(v) => v,
                _ => {
                    return;
                }
            };
            // Replace imports iff dependency is common js module.
            let dep_module = self.bundler.scope.get_module(dep_module_id).unwrap();
            if !self.bundler.scope.is_cjs(dep_module_id) && dep_module.is_es6 {
                return;
            }

            let load_var = self.bundler.make_cjs_load_var(&dep_module, i.span);
            // Replace import progress from 'progress';
            // Side effect import
            if i.specifiers.is_empty() {
                self.replaced = true;
                *node = CallExpr {
                    span: DUMMY_SP,
                    callee: load_var.as_callee(),
                    args: Vec::new(),

                    ..Default::default()
                }
                .into_stmt()
                .into();
                return;
            }

            let mut props = Vec::new();
            // TODO
            for spec in i.specifiers.clone() {
                match spec {
                    ImportSpecifier::Named(s) => match s.imported {
                        Some(ModuleExportName::Ident(imported)) => {
                            props.push(ObjectPatProp::KeyValue(KeyValuePatProp {
                                key: imported.into(),
                                value: Box::new(s.local.into()),
                            }));
                        }
                        Some(ModuleExportName::Str(..)) => {
                            unimplemented!("module string names unimplemented")
                        }
                        _ => {
                            props.push(ObjectPatProp::Assign(AssignPatProp {
                                span: s.span,
                                key: s.local.into(),
                                value: None,
                            }));
                        }
                    },
                    ImportSpecifier::Default(s) => {
                        props.push(ObjectPatProp::KeyValue(KeyValuePatProp {
                            key: PropName::Ident(IdentName::new("default".into(), DUMMY_SP)),
                            value: Box::new(s.local.into()),
                        }));
                    }
                    ImportSpecifier::Namespace(ns) => {
                        self.replaced = true;
                        *node = VarDecl {
                            span: i.span,
                            kind: VarDeclKind::Var,
                            declare: false,
                            decls: vec![VarDeclarator {
                                span: ns.span,
                                name: ns.local.into(),
                                init: Some(Box::new(
                                    CallExpr {
                                        span: DUMMY_SP,
                                        callee: load_var.as_callee(),
                                        args: Vec::new(),

                                        ..Default::default()
                                    }
                                    .into(),
                                )),
                                definite: false,
                            }],
                            ..Default::default()
                        }
                        .into();
                        return;
                    }
                }
            }

            self.replaced = true;
            *node = VarDecl {
                span: i.span,
                kind: VarDeclKind::Var,
                declare: false,
                decls: vec![VarDeclarator {
                    span: i.span,
                    name: Pat::Object(ObjectPat {
                        span: DUMMY_SP,
                        props,
                        optional: false,
                        type_ann: None,
                    }),
                    init: Some(Box::new(Expr::Call(CallExpr {
                        span: DUMMY_SP,
                        callee: load_var.as_callee(),
                        args: Vec::new(),
                        ..Default::default()
                    }))),
                    definite: false,
                }],
                ..Default::default()
            }
            .into();
        }
    }
}

struct DefaultHandler {
    local_ctxt: SyntaxContext,
}

impl VisitMut for DefaultHandler {
    noop_visit_mut_type!(fail);

    fn visit_mut_expr(&mut self, e: &mut Expr) {
        e.visit_mut_children_with(self);

        if let Expr::Ident(i) = e {
            if i.sym == "default" {
                *e = MemberExpr {
                    span: i.span,
                    obj: Ident::new("module".into(), DUMMY_SP, self.local_ctxt).into(),
                    prop: MemberProp::Ident(quote_ident!("exports")),
                }
                .into();
            }
        }
    }
}

struct Remapper {
    vars: AHashMap<Id, SyntaxContext>,
}

impl VisitMut for Remapper {
    noop_visit_mut_type!(fail);

    fn visit_mut_ident(&mut self, i: &mut Ident) {
        if let Some(v) = self.vars.get(&i.to_id()).copied() {
            i.ctxt = v;
        }
    }
}