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
use std::path::Path;
/// Web target specific options
#[derive(Default)]
pub struct WebTargetOptions {
_priv: (),
}
/// Bundler target specific options
#[derive(Default)]
pub struct BundlerTargetOptions {
pub(crate) _source_imports: bool,
}
/// Target specific options
#[non_exhaustive]
pub enum OutputTarget {
Web(WebTargetOptions),
Bundler(BundlerTargetOptions),
}
impl Default for OutputTarget {
fn default() -> Self {
Self::Web(Default::default())
}
}
impl OutputTarget {
/// Change the target to "web".
///
/// This target supports javascript modules, but instantiates
/// wasm modules with manually configured imports.
pub fn web(&mut self) -> &mut WebTargetOptions {
loop {
if let Self::Web(web) = self {
break web;
}
*self = Self::Web(Default::default());
}
}
/// Change the target to "bundler".
///
/// This target is geared towards webpack. Wasm modules derive
/// their imports from the declared import paths.
pub fn bundler(&mut self) -> &mut BundlerTargetOptions {
loop {
if let Self::Bundler(bundler) = self {
break bundler;
}
*self = Self::Bundler(Default::default())
}
}
}
#[non_exhaustive]
pub struct Options<'a> {
/// The input wasm to split
pub input_wasm: &'a [u8],
/// Where to put javascript wrappers, split wasm modules.
///
/// Default: `Path::new("wasm_split")`
pub output_dir: &'a Path,
/// Where to put the main module that has to be post-processed by wasm-bindgen.
/// Usually a path in `output_dir`.
///
/// Default: `Path::new("wasm_split/main.wasm")`
pub main_out_path: &'a Path,
/// Module path of the created link file, relative to the output dir.
/// The wasm will use this path to import the loader functions for the split chunks.
///
/// Default: `"./__wasm_split.js"`
pub link_name: &'a str,
/// Output target
///
/// Default: Web
pub target: OutputTarget,
/// The meaning of this depends on the target:
/// - for web, this is that module path from where `initSync` will be imported from
/// - for bundler this is the module path to the wasm module
///
/// Default: `"./main.js"`
pub main_module: &'a str,
/// Verbosely output additional information about processing.
///
/// Default: false
pub verbose: bool,
/// Switch to transform and emit `.debug_` sections.
///
/// This option is experimental.
/// Default: `true` if the `WASM_SPLIT_CLI_ENABLE_DWARF` environment variable is non-empty.
pub emit_dwarf: bool,
/// Enables explicit tests for assumptions we make about the input wasm file during integration testing.
#[doc(hidden)]
pub strict_tests: bool,
}
impl<'wasm> Options<'wasm> {
/// New default options of the specified input wasm.
pub fn new(input_wasm: &'wasm [u8]) -> Self {
Self {
input_wasm,
output_dir: Path::new("wasm_split"),
main_out_path: Path::new("wasm_split/main.wasm"),
link_name: "./__wasm_split.js",
main_module: "./main.js",
target: OutputTarget::default(),
verbose: false,
emit_dwarf: std::env::var_os("WASM_SPLIT_CLI_ENABLE_DWARF")
.is_some_and(|v| !v.is_empty()),
strict_tests: false,
}
}
}