rspack_loader_swc 0.102.2

rspack builtin swc loader
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
use rspack_util::json_stringify_str;
use swc::atoms::Wtf8Atom;
use swc_core::{
  atoms::Atom,
  common::{DUMMY_SP, Span, SyntaxContext},
  ecma::{ast::*, utils::ExprFactory},
};

const RSC_SERVER_MODULE: &str = "react-server-dom-rspack/server";
const REGISTER_CLIENT_REFERENCE: &str = "registerClientReference";
const DYNAMIC_EXPORT_BINDING_PREFIX: &str = "__rspack_export_";

/// Replaces a `"use client"` module on the RSC server layer with client
/// reference proxy exports.
///
/// For ESM modules with `default` and `Button` exports, this generates code
/// with this shape:
///
/// ```text
/// import { registerClientReference } from "react-server-dom-rspack/server";
///
/// export default registerClientReference(
///   function() { throw new Error(...); },
///   resource,
///   "default"
/// );
/// export const Button = registerClientReference(
///   function() { throw new Error(...); },
///   resource,
///   "Button"
/// );
/// ```
///
/// CJS modules import through `require` and assign the references directly to
/// `module.exports` / `exports[exportName]`:
///
/// ```text
/// const { registerClientReference } = require("react-server-dom-rspack/server");
///
/// module.exports = registerClientReference(
///   function() { throw new Error(...); },
///   resource,
///   "default"
/// );
/// exports["Button"] = registerClientReference(
///   function() { throw new Error(...); },
///   resource,
///   "Button"
/// );
/// ```
///
/// Returns `false` for `export *` client refs, so the caller can keep the
/// original module and report the unsupported whole-module reference later.
pub fn to_client_ref(
  module: &mut swc_core::ecma::ast::Module,
  resource: &str,
  client_refs: &[Wtf8Atom],
  is_cjs: bool,
) -> bool {
  if client_refs
    .iter()
    .any(|client_ref| client_ref.as_str() == Some("*"))
  {
    return false;
  }

  module.body = if is_cjs {
    to_cjs_client_ref(resource, client_refs)
  } else {
    to_esm_client_ref(resource, client_refs)
  };
  true
}

fn to_esm_client_ref(resource: &str, client_refs: &[Wtf8Atom]) -> Vec<ModuleItem> {
  to_client_ref_module(
    resource,
    client_refs,
    import_named(RSC_SERVER_MODULE, &[REGISTER_CLIENT_REFERENCE]),
    false,
  )
}

fn to_cjs_client_ref(resource: &str, client_refs: &[Wtf8Atom]) -> Vec<ModuleItem> {
  to_client_ref_module(
    resource,
    client_refs,
    const_object_decl(
      &[REGISTER_CLIENT_REFERENCE],
      require_call(RSC_SERVER_MODULE),
    ),
    true,
  )
}

fn to_client_ref_module(
  resource: &str,
  client_refs: &[Wtf8Atom],
  register_client_reference_decl: ModuleItem,
  is_cjs: bool,
) -> Vec<ModuleItem> {
  let mut items = Vec::with_capacity(client_refs.len() + 1);
  items.push(register_client_reference_decl);
  items.extend(client_exports(resource, client_refs, is_cjs));
  items
}

fn client_exports(resource: &str, client_refs: &[Wtf8Atom], is_cjs: bool) -> Vec<ModuleItem> {
  let call_error = client_reference_call_error(resource);
  let mut items = Vec::with_capacity(client_refs.len());
  let mut dynamic_export_count = 0;

  for export_name in client_refs
    .iter()
    .filter_map(|client_ref| client_ref.as_str())
  {
    let reference = register_client_reference_expr(resource, export_name, &call_error);
    items.extend(client_export_decls(
      export_name,
      reference,
      client_refs,
      &mut dynamic_export_count,
      is_cjs,
    ));
  }

  items
}

fn client_export_decls(
  export_name: &str,
  reference: Expr,
  client_refs: &[Wtf8Atom],
  dynamic_export_count: &mut usize,
  is_cjs: bool,
) -> Vec<ModuleItem> {
  match (is_cjs, export_name) {
    (false, "default") => vec![ModuleItem::ModuleDecl(ModuleDecl::ExportDefaultExpr(
      ExportDefaultExpr {
        span: DUMMY_SP,
        expr: Box::new(reference),
      },
    ))],
    (true, "default") => vec![assign_member_stmt("module", "exports", reference)],
    (false, ident) if Ident::verify_symbol(ident).is_ok() => {
      vec![export_const_decl(ident, reference)]
    }
    (false, export_name) => {
      let local_name = next_dynamic_export_binding(client_refs, dynamic_export_count);
      vec![
        const_decl(&local_name, reference),
        export_named_decl(&local_name, export_name),
      ]
    }
    (true, ident) => vec![assign_computed_member_stmt(
      "exports",
      str_expr(ident),
      reference,
    )],
  }
}

fn client_reference_call_error(resource: &str) -> String {
  format!(
    "Attempted to call the default export of {} from \
    the server, but it's on the client. It's not possible to invoke a \
    client function from the server, it can only be rendered as a \
    Component or passed to props of a Client Component.",
    json_stringify_str(resource)
  )
}

fn next_dynamic_export_binding(
  client_refs: &[Wtf8Atom],
  dynamic_export_count: &mut usize,
) -> String {
  loop {
    *dynamic_export_count += 1;
    let name = format!("{DYNAMIC_EXPORT_BINDING_PREFIX}{dynamic_export_count}__");
    if !client_refs
      .iter()
      .any(|client_ref| client_ref.as_str() == Some(name.as_str()))
    {
      return name;
    }
  }
}

fn register_client_reference_expr(resource: &str, export_name: &str, call_error: &str) -> Expr {
  call_expr(
    ident_expr(REGISTER_CLIENT_REFERENCE),
    vec![
      throw_error_function(call_error),
      str_expr(resource),
      str_expr(export_name),
    ],
    DUMMY_SP,
  )
}

fn throw_error_function(call_error: &str) -> Expr {
  Expr::Fn(FnExpr {
    ident: None,
    function: Box::new(Function {
      body: Some(FunctionBody {
        span: DUMMY_SP,
        stmts: vec![Stmt::Throw(ThrowStmt {
          span: DUMMY_SP,
          arg: Box::new(Expr::New(NewExpr {
            span: DUMMY_SP,
            ctxt: SyntaxContext::empty(),
            callee: Box::new(ident_expr("Error")),
            args: Some(vec![expr_arg(str_expr(call_error))]),
            type_args: None,
          })),
        })],
      }),
      ..Default::default()
    }),
  })
}

fn import_named(source: &str, names: &[&str]) -> ModuleItem {
  ModuleItem::ModuleDecl(ModuleDecl::Import(ImportDecl {
    span: DUMMY_SP,
    specifiers: names
      .iter()
      .map(|name| {
        ImportSpecifier::Named(ImportNamedSpecifier {
          span: DUMMY_SP,
          local: ident(name),
          imported: None,
          is_type_only: false,
        })
      })
      .collect(),
    src: Box::new(Str {
      span: DUMMY_SP,
      value: Wtf8Atom::from(source),
      raw: None,
    }),
    type_only: false,
    with: None,
    phase: Default::default(),
  }))
}

fn const_decl(name: &str, init: Expr) -> ModuleItem {
  ModuleItem::Stmt(Stmt::Decl(Decl::Var(Box::new(VarDecl {
    span: DUMMY_SP,
    kind: VarDeclKind::Const,
    decls: vec![VarDeclarator {
      span: DUMMY_SP,
      name: Pat::Ident(ident(name).into()),
      init: Some(Box::new(init)),
      definite: false,
    }],
    ..Default::default()
  }))))
}

fn const_object_decl(names: &[&str], init: Expr) -> ModuleItem {
  ModuleItem::Stmt(Stmt::Decl(Decl::Var(Box::new(VarDecl {
    span: DUMMY_SP,
    kind: VarDeclKind::Const,
    decls: vec![VarDeclarator {
      span: DUMMY_SP,
      name: Pat::Object(ObjectPat {
        span: DUMMY_SP,
        props: names
          .iter()
          .map(|name| {
            ObjectPatProp::Assign(AssignPatProp {
              span: DUMMY_SP,
              key: ident(name).into(),
              value: None,
            })
          })
          .collect(),
        optional: false,
        type_ann: None,
      }),
      init: Some(Box::new(init)),
      definite: false,
    }],
    ..Default::default()
  }))))
}

fn export_const_decl(name: &str, init: Expr) -> ModuleItem {
  ModuleItem::ModuleDecl(ModuleDecl::ExportDecl(ExportDecl {
    span: DUMMY_SP,
    decl: Decl::Var(Box::new(VarDecl {
      span: DUMMY_SP,
      kind: VarDeclKind::Const,
      decls: vec![VarDeclarator {
        span: DUMMY_SP,
        name: Pat::Ident(ident(name).into()),
        init: Some(Box::new(init)),
        definite: false,
      }],
      ..Default::default()
    })),
  }))
}

fn export_named_decl(local_name: &str, export_name: &str) -> ModuleItem {
  ModuleItem::ModuleDecl(ModuleDecl::ExportNamed(NamedExport {
    span: DUMMY_SP,
    specifiers: vec![ExportSpecifier::Named(ExportNamedSpecifier {
      span: DUMMY_SP,
      orig: ModuleExportName::Ident(ident(local_name)),
      exported: Some(ModuleExportName::Str(Str {
        span: DUMMY_SP,
        value: Wtf8Atom::from(export_name),
        raw: None,
      })),
      is_type_only: false,
    })],
    src: None,
    type_only: false,
    with: None,
  }))
}

fn assign_member_stmt(obj: &str, prop: &str, right: Expr) -> ModuleItem {
  ModuleItem::Stmt(Stmt::Expr(ExprStmt {
    span: DUMMY_SP,
    expr: Box::new(Expr::Assign(AssignExpr {
      span: DUMMY_SP,
      op: AssignOp::Assign,
      left: AssignTarget::Simple(SimpleAssignTarget::Member(member(obj, prop))),
      right: Box::new(right),
    })),
  }))
}

fn assign_computed_member_stmt(obj: &str, prop: Expr, right: Expr) -> ModuleItem {
  ModuleItem::Stmt(Stmt::Expr(ExprStmt {
    span: DUMMY_SP,
    expr: Box::new(Expr::Assign(AssignExpr {
      span: DUMMY_SP,
      op: AssignOp::Assign,
      left: AssignTarget::Simple(SimpleAssignTarget::Member(computed_member(
        ident_expr(obj),
        prop,
      ))),
      right: Box::new(right),
    })),
  }))
}

fn require_call(source: &str) -> Expr {
  call_expr(ident_expr("require"), vec![str_expr(source)], DUMMY_SP)
}

fn call_expr(callee: Expr, args: Vec<Expr>, span: Span) -> Expr {
  Expr::Call(CallExpr {
    span,
    callee: callee.as_callee(),
    args: args.into_iter().map(expr_arg).collect(),
    ..Default::default()
  })
}

fn computed_member(obj: Expr, prop: Expr) -> MemberExpr {
  MemberExpr {
    span: DUMMY_SP,
    obj: Box::new(obj),
    prop: MemberProp::Computed(ComputedPropName {
      span: DUMMY_SP,
      expr: Box::new(prop),
    }),
  }
}

fn member(obj: &str, prop: &str) -> MemberExpr {
  member_expr_inner(ident_expr(obj), prop)
}

fn member_expr_inner(obj: Expr, prop: &str) -> MemberExpr {
  MemberExpr {
    span: DUMMY_SP,
    obj: Box::new(obj),
    prop: MemberProp::Ident(ident_name(prop)),
  }
}

fn ident_expr(name: &str) -> Expr {
  Expr::Ident(ident(name))
}

fn str_expr(value: &str) -> Expr {
  Expr::Lit(Lit::Str(Str {
    span: DUMMY_SP,
    value: Wtf8Atom::from(value),
    raw: None,
  }))
}

fn expr_arg(expr: Expr) -> ExprOrSpread {
  ExprOrSpread {
    spread: None,
    expr: Box::new(expr),
  }
}

fn ident(name: &str) -> Ident {
  Ident::new(Atom::from(name), DUMMY_SP, SyntaxContext::empty())
}

fn ident_name(name: &str) -> IdentName {
  IdentName::new(Atom::from(name), DUMMY_SP)
}