rspack_binding_api 0.100.7

Rspack shared binding API
Documentation
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
use std::sync::Arc;

use napi::Either;
use napi_derive::napi;
use rspack_plugin_mf::{
  CollectSharedEntryPluginOptions, ConsumeOptions, ConsumeSharedPluginOptions, ConsumeVersion,
  ContainerPluginOptions, ContainerReferencePluginOptions, ExposeOptions, ManifestExposeOption,
  ManifestSharedOption, ModuleFederationManifestPluginOptions,
  ModuleFederationRuntimeExperimentsOptions, ModuleFederationRuntimePluginOptions,
  OptimizeSharedConfig, ProvideOptions, ProvideVersion, RemoteAliasTarget, RemoteOptions,
  ShareScope, SharedContainerPluginOptions, SharedUsedExportsOptimizerPluginOptions,
  StatsBuildInfo,
};
use rspack_util::fx_hash::FxHashMap as HashMap;

use crate::options::{
  entry::{JsEntryRuntime, JsEntryRuntimeWrapper},
  library::JsLibraryOptions,
};

fn into_share_scope(value: Either<String, Vec<String>>) -> ShareScope {
  match value {
    Either::A(s) => ShareScope::Single(s),
    Either::B(list) => ShareScope::Multiple(list),
  }
}

#[derive(Debug)]
#[napi(object)]
pub struct RawContainerPluginOptions {
  pub name: String,
  pub share_scope: Either<String, Vec<String>>,
  pub library: JsLibraryOptions,
  #[napi(ts_type = "false | string")]
  pub runtime: Option<JsEntryRuntime>,
  pub filename: Option<String>,
  pub exposes: Vec<RawExposeOptions>,
  pub enhanced: bool,
}

impl From<RawContainerPluginOptions> for ContainerPluginOptions {
  fn from(value: RawContainerPluginOptions) -> Self {
    let share_scope = into_share_scope(value.share_scope);
    Self {
      name: value.name,
      share_scope,
      library: value.library.into(),
      runtime: value.runtime.map(|r| JsEntryRuntimeWrapper(r).into()),
      filename: value.filename.map(|f| f.into()),
      exposes: value.exposes.into_iter().map(|e| e.into()).collect(),
      enhanced: value.enhanced,
    }
  }
}

#[derive(Debug, Clone)]
#[napi(object)]
pub struct RawExposeOptions {
  pub key: String,
  pub name: Option<String>,
  pub import: Vec<String>,
}

impl From<RawExposeOptions> for (String, ExposeOptions) {
  fn from(value: RawExposeOptions) -> Self {
    (
      value.key,
      ExposeOptions {
        name: value.name,
        import: value.import,
      },
    )
  }
}

#[derive(Debug)]
#[napi(object)]
pub struct RawContainerReferencePluginOptions {
  pub remote_type: String,
  pub remotes: Vec<RawRemoteOptions>,
  pub share_scope: Option<Either<String, Vec<String>>>,
  pub enhanced: bool,
}

impl From<RawContainerReferencePluginOptions> for ContainerReferencePluginOptions {
  fn from(value: RawContainerReferencePluginOptions) -> Self {
    let share_scope = value.share_scope.map(into_share_scope);
    Self {
      remote_type: value.remote_type,
      remotes: value.remotes.into_iter().map(|e| e.into()).collect(),
      share_scope,
      enhanced: value.enhanced,
    }
  }
}

#[derive(Debug)]
#[napi(object)]
pub struct RawRemoteOptions {
  pub key: String,
  pub external: Vec<String>,
  pub share_scope: Either<String, Vec<String>>,
}

impl From<RawRemoteOptions> for (String, RemoteOptions) {
  fn from(value: RawRemoteOptions) -> Self {
    (
      value.key,
      RemoteOptions {
        external: value.external,
        share_scope: into_share_scope(value.share_scope),
      },
    )
  }
}

#[derive(Debug)]
#[napi(object)]
pub struct RawProvideOptions {
  pub key: String,
  pub share_key: String,
  pub share_scope: Either<String, Vec<String>>,
  #[napi(ts_type = "string | false | undefined")]
  pub version: Option<RawVersion>,
  pub eager: bool,
  pub singleton: Option<bool>,
  #[napi(ts_type = "string | false | undefined")]
  pub required_version: Option<RawVersion>,
  pub strict_version: Option<bool>,
  pub tree_shaking_mode: Option<String>,
}

impl From<RawProvideOptions> for (String, ProvideOptions) {
  fn from(value: RawProvideOptions) -> Self {
    (
      value.key,
      ProvideOptions {
        share_key: value.share_key,
        share_scope: into_share_scope(value.share_scope),
        version: value.version.map(|v| RawVersionWrapper(v).into()),
        eager: value.eager,
        singleton: value.singleton,
        required_version: value.required_version.map(|v| RawVersionWrapper(v).into()),
        strict_version: value.strict_version,
        tree_shaking_mode: value.tree_shaking_mode,
      },
    )
  }
}

#[derive(Debug)]
#[napi(object)]
pub struct RawCollectShareEntryPluginOptions {
  pub consumes: Vec<RawConsumeOptions>,
  pub filename: Option<String>,
}

impl From<RawCollectShareEntryPluginOptions> for CollectSharedEntryPluginOptions {
  fn from(value: RawCollectShareEntryPluginOptions) -> Self {
    Self {
      consumes: value
        .consumes
        .into_iter()
        .map(|provide| {
          let (key, consume_options): (String, ConsumeOptions) = provide.into();
          (key, std::sync::Arc::new(consume_options))
        })
        .collect(),
      filename: value.filename,
    }
  }
}

#[derive(Debug)]
#[napi(object)]
pub struct RawSharedContainerPluginOptions {
  pub name: String,
  pub request: String,
  pub version: String,
  pub file_name: Option<String>,
  pub library: JsLibraryOptions,
}

impl From<RawSharedContainerPluginOptions> for SharedContainerPluginOptions {
  fn from(value: RawSharedContainerPluginOptions) -> Self {
    SharedContainerPluginOptions {
      name: value.name,
      request: value.request,
      version: value.version,
      library: value.library.into(),
      file_name: value.file_name.map(Into::into),
    }
  }
}

#[derive(Debug)]
#[napi(object)]
pub struct RawConsumeSharedPluginOptions {
  pub consumes: Vec<RawConsumeOptions>,
  pub enhanced: bool,
}

impl From<RawConsumeSharedPluginOptions> for ConsumeSharedPluginOptions {
  fn from(value: RawConsumeSharedPluginOptions) -> Self {
    Self {
      consumes: value
        .consumes
        .into_iter()
        .map(|c| c.into())
        .map(|(k, v)| (k, Arc::new(v)))
        .collect(),
      enhanced: value.enhanced,
    }
  }
}

#[derive(Debug)]
#[napi(object)]
pub struct RawOptimizeSharedConfig {
  pub share_key: String,
  pub tree_shaking: bool,
  pub used_exports: Option<Vec<String>>,
}

impl From<RawOptimizeSharedConfig> for OptimizeSharedConfig {
  fn from(value: RawOptimizeSharedConfig) -> Self {
    Self {
      share_key: value.share_key,
      tree_shaking: value.tree_shaking,
      used_exports: value.used_exports.unwrap_or_default(),
    }
  }
}

#[derive(Debug)]
#[napi(object)]
pub struct RawSharedUsedExportsOptimizerPluginOptions {
  pub shared: Vec<RawOptimizeSharedConfig>,
  pub inject_tree_shaking_used_exports: Option<bool>,
  pub manifest_file_name: Option<String>,
  pub stats_file_name: Option<String>,
}

impl From<RawSharedUsedExportsOptimizerPluginOptions> for SharedUsedExportsOptimizerPluginOptions {
  fn from(value: RawSharedUsedExportsOptimizerPluginOptions) -> Self {
    Self {
      shared: value
        .shared
        .into_iter()
        .map(|config| config.into())
        .collect(),
      inject_tree_shaking_used_exports: value.inject_tree_shaking_used_exports.unwrap_or(true),
      manifest_file_name: value
        .manifest_file_name
        .and_then(|s| if s.trim().is_empty() { None } else { Some(s) }),
      stats_file_name: value
        .stats_file_name
        .and_then(|s| if s.trim().is_empty() { None } else { Some(s) }),
    }
  }
}

#[derive(Debug)]
#[napi(object)]
pub struct RawConsumeOptions {
  pub key: String,
  pub import: Option<String>,
  pub import_resolved: Option<String>,
  pub share_key: String,
  pub share_scope: Either<String, Vec<String>>,
  #[napi(ts_type = "string | false | undefined")]
  pub required_version: Option<RawVersion>,
  pub package_name: Option<String>,
  pub strict_version: bool,
  pub singleton: bool,
  pub eager: bool,
  pub tree_shaking_mode: Option<String>,
}

impl From<RawConsumeOptions> for (String, ConsumeOptions) {
  fn from(value: RawConsumeOptions) -> Self {
    (
      value.key,
      ConsumeOptions {
        import: value.import,
        import_resolved: value.import_resolved,
        share_key: value.share_key,
        share_scope: into_share_scope(value.share_scope),
        required_version: value.required_version.map(|v| RawVersionWrapper(v).into()),
        package_name: value.package_name,
        strict_version: value.strict_version,
        singleton: value.singleton,
        eager: value.eager,
        tree_shaking_mode: value.tree_shaking_mode,
      },
    )
  }
}

pub type RawVersion = Either<String, bool>;

struct RawVersionWrapper(RawVersion);

impl From<RawVersionWrapper> for ProvideVersion {
  fn from(value: RawVersionWrapper) -> Self {
    match value.0 {
      Either::A(s) => ProvideVersion::Version(s),
      Either::B(_) => ProvideVersion::False,
    }
  }
}

impl From<RawVersionWrapper> for ConsumeVersion {
  fn from(value: RawVersionWrapper) -> Self {
    match value.0 {
      Either::A(s) => ConsumeVersion::Version(s),
      Either::B(_) => ConsumeVersion::False,
    }
  }
}

#[derive(Debug)]
#[napi(object)]
pub struct RawModuleFederationRuntimePluginOptions {
  #[napi(ts_type = "string | undefined")]
  pub entry_runtime: Option<String>,
  pub experiments: Option<RawModuleFederationRuntimeExperimentsOptions>,
}

impl From<RawModuleFederationRuntimePluginOptions> for ModuleFederationRuntimePluginOptions {
  fn from(value: RawModuleFederationRuntimePluginOptions) -> Self {
    Self {
      entry_runtime: value.entry_runtime,
      experiments: value.experiments.map(Into::into).unwrap_or_default(),
    }
  }
}

#[derive(Debug, Default)]
#[napi(object)]
pub struct RawModuleFederationRuntimeExperimentsOptions {
  #[napi(js_name = "asyncStartup")]
  pub async_startup: Option<bool>,
}

impl From<RawModuleFederationRuntimeExperimentsOptions>
  for ModuleFederationRuntimeExperimentsOptions
{
  fn from(value: RawModuleFederationRuntimeExperimentsOptions) -> Self {
    Self {
      async_startup: value.async_startup.unwrap_or(false),
    }
  }
}

#[derive(Debug)]
#[napi(object)]
pub struct RawRemoteAliasTarget {
  pub name: String,
  pub entry: Option<String>,
}

#[derive(Debug)]
#[napi(object)]
pub struct RawManifestExposeOption {
  pub path: String,
  pub name: String,
}

#[derive(Debug)]
#[napi(object)]
pub struct RawManifestSharedOption {
  pub name: String,
  pub version: Option<String>,
  pub required_version: Option<String>,
  pub singleton: Option<bool>,
}

#[derive(Debug)]
#[napi(object)]
pub struct RawStatsBuildInfo {
  pub build_version: String,
  pub build_name: Option<String>,
  // only appear when enable tree_shaking
  pub target: Option<Vec<String>>,
  pub plugins: Option<Vec<String>>,
}

#[derive(Debug)]
#[napi(object)]
pub struct RawModuleFederationManifestPluginOptions {
  pub name: Option<String>,
  pub global_name: Option<String>,
  pub file_name: Option<String>,
  pub file_path: Option<String>,
  pub stats_file_name: Option<String>,
  pub manifest_file_name: Option<String>,
  pub disable_assets_analyze: Option<bool>,
  pub remote_alias_map: Option<HashMap<String, RawRemoteAliasTarget>>,
  pub exposes: Option<Vec<RawManifestExposeOption>>,
  pub shared: Option<Vec<RawManifestSharedOption>>,
  pub build_info: Option<RawStatsBuildInfo>,
}

impl From<RawModuleFederationManifestPluginOptions> for ModuleFederationManifestPluginOptions {
  fn from(value: RawModuleFederationManifestPluginOptions) -> Self {
    ModuleFederationManifestPluginOptions {
      name: value.name,
      global_name: value.global_name,
      stats_file_name: value.stats_file_name.unwrap_or_default(),
      manifest_file_name: value.manifest_file_name.unwrap_or_default(),
      disable_assets_analyze: value.disable_assets_analyze.unwrap_or(false),
      remote_alias_map: value
        .remote_alias_map
        .unwrap_or_default()
        .into_iter()
        .map(|(k, v)| {
          (
            k,
            RemoteAliasTarget {
              name: v.name,
              entry: v.entry,
            },
          )
        })
        .collect::<HashMap<String, RemoteAliasTarget>>(),
      exposes: value
        .exposes
        .unwrap_or_default()
        .into_iter()
        .map(|expose| ManifestExposeOption {
          path: expose.path,
          name: expose.name,
        })
        .collect(),
      shared: value
        .shared
        .unwrap_or_default()
        .into_iter()
        .map(|shared| ManifestSharedOption {
          name: shared.name,
          version: shared.version,
          required_version: shared.required_version,
          singleton: shared.singleton,
        })
        .collect(),
      build_info: value.build_info.map(|info| StatsBuildInfo {
        build_version: info.build_version,
        build_name: info.build_name,
        target: info.target,
        plugins: info.plugins,
      }),
    }
  }
}