@nimbus-sh/worker 0.12.0 → 0.13.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (198) hide show
  1. package/dist/esbuild-cli-artifact.generated.d.ts +1 -1
  2. package/dist/esbuild-cli-artifact.generated.js +4 -4
  3. package/dist/esbuild-wasm-bundle.generated.d.ts +11 -10
  4. package/dist/esbuild-wasm-bundle.generated.d.ts.map +1 -1
  5. package/dist/esbuild-wasm-bundle.generated.js +11 -10
  6. package/dist/facets/build-facet.d.ts +53 -0
  7. package/dist/facets/build-facet.d.ts.map +1 -0
  8. package/dist/facets/build-facet.js +346 -0
  9. package/dist/facets/cirrus-real.d.ts +3 -0
  10. package/dist/facets/cirrus-real.d.ts.map +1 -1
  11. package/dist/facets/cirrus-real.js +1 -2
  12. package/dist/facets/compose.d.ts +1 -0
  13. package/dist/facets/compose.d.ts.map +1 -1
  14. package/dist/facets/compose.js +3 -0
  15. package/dist/facets/data-plan.d.ts +14 -30
  16. package/dist/facets/data-plan.d.ts.map +1 -1
  17. package/dist/facets/data-plan.js +153 -13
  18. package/dist/facets/dev-stylesheet.d.ts +27 -0
  19. package/dist/facets/dev-stylesheet.d.ts.map +1 -0
  20. package/dist/facets/dev-stylesheet.js +65 -0
  21. package/dist/facets/esbuild-transform.d.ts +23 -18
  22. package/dist/facets/esbuild-transform.d.ts.map +1 -1
  23. package/dist/facets/esbuild-transform.js +63 -109
  24. package/dist/facets/manager.d.ts +49 -9
  25. package/dist/facets/manager.d.ts.map +1 -1
  26. package/dist/facets/manager.js +238 -69
  27. package/dist/facets/oxc-transform.d.ts +29 -0
  28. package/dist/facets/oxc-transform.d.ts.map +1 -0
  29. package/dist/facets/oxc-transform.js +198 -0
  30. package/dist/facets/prebundle-pool.d.ts +23 -0
  31. package/dist/facets/prebundle-pool.d.ts.map +1 -0
  32. package/dist/facets/prebundle-pool.js +55 -0
  33. package/dist/facets/process.d.ts +13 -6
  34. package/dist/facets/process.d.ts.map +1 -1
  35. package/dist/facets/process.js +37 -31
  36. package/dist/facets/transform-store.d.ts +4 -4
  37. package/dist/facets/transform-store.js +4 -4
  38. package/dist/facets/vite-dev-server.d.ts +89 -12
  39. package/dist/facets/vite-dev-server.d.ts.map +1 -1
  40. package/dist/facets/vite-dev-server.js +66 -43
  41. package/dist/hosted/commands.d.ts.map +1 -1
  42. package/dist/hosted/commands.js +20 -4
  43. package/dist/hosted/runtime.d.ts.map +1 -1
  44. package/dist/hosted/runtime.js +7 -2
  45. package/dist/hosted/services.d.ts +3 -3
  46. package/dist/hosted/services.d.ts.map +1 -1
  47. package/dist/hosted/services.js +102 -95
  48. package/dist/hosted/session.d.ts +1 -0
  49. package/dist/hosted/session.d.ts.map +1 -1
  50. package/dist/loaders/generated-workers.d.ts +3 -0
  51. package/dist/loaders/generated-workers.d.ts.map +1 -1
  52. package/dist/loaders/generated-workers.js +3 -0
  53. package/dist/napi-wasm-artifacts.generated.js +2 -2
  54. package/dist/node-shims-artifact.generated.d.ts +14 -1
  55. package/dist/node-shims-artifact.generated.d.ts.map +1 -1
  56. package/dist/node-shims-artifact.generated.js +23 -10
  57. package/dist/npm/bin-links.d.ts +7 -4
  58. package/dist/npm/bin-links.d.ts.map +1 -1
  59. package/dist/npm/bin-links.js +14 -55
  60. package/dist/npm/cache.d.ts +6 -0
  61. package/dist/npm/cache.d.ts.map +1 -1
  62. package/dist/npm/cache.js +26 -4
  63. package/dist/npm/installer.d.ts +1 -1
  64. package/dist/npm/installer.d.ts.map +1 -1
  65. package/dist/npm/installer.js +26 -52
  66. package/dist/npm/pre-bundle-facet.d.ts +15 -117
  67. package/dist/npm/pre-bundle-facet.d.ts.map +1 -1
  68. package/dist/npm/pre-bundle-facet.js +13 -511
  69. package/dist/oxc-facet-artifact.generated.d.ts +18 -0
  70. package/dist/oxc-facet-artifact.generated.d.ts.map +1 -0
  71. package/dist/oxc-facet-artifact.generated.js +17 -0
  72. package/dist/oxc-wasm-artifact.generated.d.ts +20 -0
  73. package/dist/oxc-wasm-artifact.generated.d.ts.map +1 -0
  74. package/dist/oxc-wasm-artifact.generated.js +19 -0
  75. package/dist/rolldown-facet-artifact.generated.d.ts +18 -0
  76. package/dist/rolldown-facet-artifact.generated.d.ts.map +1 -0
  77. package/dist/rolldown-facet-artifact.generated.js +17 -0
  78. package/dist/router/remote-api.d.ts.map +1 -1
  79. package/dist/router/remote-api.js +9 -0
  80. package/dist/runtime/bash-repl.d.ts.map +1 -1
  81. package/dist/runtime/bash-repl.js +1 -4
  82. package/dist/runtime/cpython-resident.d.ts.map +1 -1
  83. package/dist/runtime/cpython-resident.js +1 -0
  84. package/dist/runtime/esbuild-wasm-bytes.d.ts +25 -23
  85. package/dist/runtime/esbuild-wasm-bytes.d.ts.map +1 -1
  86. package/dist/runtime/esbuild-wasm-bytes.js +30 -25
  87. package/dist/runtime/facet-loader-host.d.ts +5 -4
  88. package/dist/runtime/facet-loader-host.d.ts.map +1 -1
  89. package/dist/runtime/facet-loader-host.js +5 -4
  90. package/dist/runtime/node-runner.d.ts +2 -0
  91. package/dist/runtime/node-runner.d.ts.map +1 -1
  92. package/dist/runtime/node-runner.js +1 -0
  93. package/dist/runtime/node-shims-artifact.d.ts +8 -1
  94. package/dist/runtime/node-shims-artifact.d.ts.map +1 -1
  95. package/dist/runtime/node-shims-artifact.js +32 -4
  96. package/dist/runtime/node-shims.d.ts.map +1 -1
  97. package/dist/runtime/node-shims.js +145 -109
  98. package/dist/runtime/opencode-facet-runner.d.ts.map +1 -1
  99. package/dist/runtime/opencode-facet-runner.js +6 -2
  100. package/dist/runtime/oxc-wasm-bytes.d.ts +19 -0
  101. package/dist/runtime/oxc-wasm-bytes.d.ts.map +1 -0
  102. package/dist/runtime/oxc-wasm-bytes.js +39 -0
  103. package/dist/runtime/package-manager.js +1 -1
  104. package/dist/runtime/process-logs-api.d.ts.map +1 -1
  105. package/dist/runtime/process-logs-api.js +2 -0
  106. package/dist/runtime/project-fs.d.ts +8 -1
  107. package/dist/runtime/project-fs.d.ts.map +1 -1
  108. package/dist/runtime/project-fs.js +15 -1
  109. package/dist/runtime/python-repl.d.ts +40 -1
  110. package/dist/runtime/python-repl.d.ts.map +1 -1
  111. package/dist/runtime/python-repl.js +19 -11
  112. package/dist/runtime/ruby-repl.d.ts +38 -0
  113. package/dist/runtime/ruby-repl.d.ts.map +1 -1
  114. package/dist/runtime/ruby-repl.js +24 -5
  115. package/dist/runtime/ruby-resident.d.ts.map +1 -1
  116. package/dist/runtime/ruby-resident.js +2 -0
  117. package/dist/session/nimbus-session.d.ts +13 -8
  118. package/dist/session/nimbus-session.d.ts.map +1 -1
  119. package/dist/session/nimbus-session.js +11 -5
  120. package/dist/session/programmatic.d.ts +25 -30
  121. package/dist/session/programmatic.d.ts.map +1 -1
  122. package/dist/session/programmatic.js +65 -93
  123. package/dist/session/routes.d.ts.map +1 -1
  124. package/dist/session/routes.js +23 -7
  125. package/dist/session/rpc.d.ts +16 -6
  126. package/dist/session/rpc.d.ts.map +1 -1
  127. package/dist/session/rpc.js +53 -13
  128. package/dist/session/start-real-vite.d.ts +24 -9
  129. package/dist/session/start-real-vite.d.ts.map +1 -1
  130. package/dist/session/start-real-vite.js +40 -4
  131. package/dist/session/supervisor-op.d.ts +14 -0
  132. package/dist/session/supervisor-op.d.ts.map +1 -1
  133. package/dist/session/supervisor-op.js +49 -0
  134. package/dist/session/supervisor-rpc.d.ts +11 -0
  135. package/dist/session/supervisor-rpc.d.ts.map +1 -1
  136. package/dist/session/supervisor-rpc.js +18 -0
  137. package/dist/session/vite-command.d.ts.map +1 -1
  138. package/dist/session/vite-command.js +23 -19
  139. package/dist/session/ws-relay.d.ts.map +1 -1
  140. package/dist/session/ws-relay.js +13 -1
  141. package/dist/shell/npm-bin-entrypoints.d.ts +3 -1
  142. package/dist/shell/npm-bin-entrypoints.d.ts.map +1 -1
  143. package/dist/shell/npm-bin-entrypoints.js +53 -25
  144. package/dist/vfs/facet-resident-store.d.ts.map +1 -1
  145. package/dist/vfs/facet-resident-store.js +11 -5
  146. package/dist/wrangler/nimbus-wrangler.d.ts +8 -0
  147. package/dist/wrangler/nimbus-wrangler.d.ts.map +1 -1
  148. package/dist/wrangler/nimbus-wrangler.js +54 -57
  149. package/package.json +6 -4
  150. package/public/_assets/esbuild-0.24.2.wasm +0 -0
  151. package/public/_assets/napi-wasm/loader/{99efae93dd1dcc2b → ab7e57fe9e8750c2}/napi-wasm-loader.mjs +4 -4
  152. package/public/_assets/napi-wasm/loader/{99efae93dd1dcc2b → ab7e57fe9e8750c2}/provenance.json +2 -2
  153. package/public/_assets/oxc/nimbus-oxc-1aa8c4c7057c6c28.provenance.json +72 -0
  154. package/public/_assets/oxc/nimbus-oxc-1aa8c4c7057c6c28.wasm +0 -0
  155. package/public/_assets/runtime/esbuild-cli-f13e6a112b03ac07.js +1143 -0
  156. package/public/_assets/runtime/js-interpreter-3c37c48024f0f651.js +23 -0
  157. package/public/_assets/runtime/js-interpreter-ops-2f4dfec6798e70fc.js +290 -0
  158. package/public/_assets/runtime/js-interpreter-primordials-e620a61b7e9800fa.js +1 -0
  159. package/public/_assets/runtime/{node-shims-f92f16c7d7c532e9.js → node-shims-e3adc4576da31e6e.js} +651 -103
  160. package/public/_assets/runtime/{esbuild-cli-b7aecffd4c6a36fe.js → oxc-facet-fd86fefcdf16bf9c.js} +217 -1047
  161. package/public/_assets/runtime/{resident-store-92386265e8b306dc.js → resident-store-009f39539ac989fc.js} +11 -5
  162. package/public/_assets/runtime/rolldown-facet-09983434e2dbe93e.js +15165 -0
  163. package/public/_assets/runtime/{vfs-write-ledger-d33ee0034446c3e0.js → vfs-write-ledger-9e9594591425a9b8.js} +4 -1
  164. package/scripts/acorn-primordials.mjs +637 -0
  165. package/scripts/bundle-esbuild-wasm.mjs +42 -50
  166. package/scripts/bundle-facet-workers.mjs +247 -18
  167. package/scripts/bundle-node-shims.mjs +29 -2
  168. package/scripts/bundle-oxc-wasm.mjs +105 -0
  169. package/scripts/interpreter-bundle.mjs +105 -0
  170. package/scripts/napi-wasm/build.mjs +4 -1
  171. package/scripts/napi-wasm/loader/napi-wasm-loader.ts +38 -3
  172. package/scripts/oxc-wasm/Cargo.lock +1246 -0
  173. package/scripts/oxc-wasm/Cargo.toml +30 -0
  174. package/scripts/oxc-wasm/build.mjs +249 -0
  175. package/scripts/oxc-wasm/examples/memory.rs +51 -0
  176. package/scripts/oxc-wasm/examples/transform.rs +20 -0
  177. package/scripts/oxc-wasm/inputs.mjs +32 -0
  178. package/scripts/oxc-wasm/rust-toolchain.toml +5 -0
  179. package/scripts/oxc-wasm/src/abi.rs +136 -0
  180. package/scripts/oxc-wasm/src/diagnostics.rs +49 -0
  181. package/scripts/oxc-wasm/src/helpers.rs +139 -0
  182. package/scripts/oxc-wasm/src/lib.rs +247 -0
  183. package/scripts/oxc-wasm/src/module.rs +978 -0
  184. package/scripts/oxc-wasm/src/names.rs +175 -0
  185. package/scripts/oxc-wasm/src/options.rs +154 -0
  186. package/scripts/parser-audit.mjs +492 -0
  187. package/scripts/rolldown-facet/entry.mjs +21 -0
  188. package/scripts/rolldown-facet/shims.mjs +64 -0
  189. package/dist/facets/esbuild-bundle-pool.d.ts +0 -43
  190. package/dist/facets/esbuild-bundle-pool.d.ts.map +0 -1
  191. package/dist/facets/esbuild-bundle-pool.js +0 -80
  192. package/dist/loaders/pre-bundle-preamble.d.ts +0 -44
  193. package/dist/loaders/pre-bundle-preamble.d.ts.map +0 -1
  194. package/dist/loaders/pre-bundle-preamble.js +0 -116
  195. package/dist/runtime/host-wasm.d.ts +0 -8
  196. package/dist/runtime/host-wasm.d.ts.map +0 -1
  197. package/dist/runtime/host-wasm.js +0 -33
  198. /package/public/_assets/napi-wasm/loader/{99efae93dd1dcc2b → ab7e57fe9e8750c2}/wasi-trampoline.wasm +0 -0
@@ -0,0 +1,175 @@
1
+ //! Names for the bindings module conversion introduces, chosen as esbuild
2
+ //! chooses them: `import_<file>` from the import's path, `stdin_exports` and
3
+ //! `stdin_default`, the helpers' own names. A generated name keeps its
4
+ //! spelling; a user symbol that would collide with it is renamed (`require`
5
+ //! becomes `require2`), and a generated name that is an unbound global in the
6
+ //! module takes the next free suffix instead, since a global cannot be renamed.
7
+
8
+ use std::hash::{Hash, Hasher};
9
+
10
+ use rustc_hash::{FxHashSet, FxHasher};
11
+
12
+ use oxc::allocator::Allocator;
13
+ use oxc::semantic::{Scoping, SymbolId};
14
+ use oxc::str::Ident;
15
+ use oxc::syntax::keyword::is_reserved_keyword;
16
+
17
+ /// esbuild's `GenerateNonUniqueNameFromPath`: the file name without its
18
+ /// extension, or the directory's name for an `index` file, as an identifier.
19
+ pub fn name_from_path(path: &str) -> String {
20
+ let (dir, base) = split_dir_base(path);
21
+ let mut base = strip_ext(base);
22
+ if base == "index" {
23
+ let dir_base = strip_ext(split_dir_base(dir).1);
24
+ if !dir_base.is_empty() {
25
+ base = dir_base;
26
+ }
27
+ }
28
+ ensure_valid_identifier(base)
29
+ }
30
+
31
+ fn split_dir_base(path: &str) -> (&str, &str) {
32
+ match path.rfind(['/', '\\']) {
33
+ Some(i) => (&path[..i], &path[i + 1..]),
34
+ None => ("", path),
35
+ }
36
+ }
37
+
38
+ fn strip_ext(base: &str) -> &str {
39
+ match base.rfind('.') {
40
+ Some(i) => &base[..i],
41
+ None => base,
42
+ }
43
+ }
44
+
45
+ /// esbuild's `EnsureValidIdentifier`: ASCII letters and digits, runs of
46
+ /// anything else collapsed to one `_`, no leading digit, never empty.
47
+ pub fn ensure_valid_identifier(base: &str) -> String {
48
+ let mut out = String::with_capacity(base.len());
49
+ let mut needs_gap = false;
50
+ for c in base.chars() {
51
+ if c.is_ascii_alphabetic() || (!out.is_empty() && c.is_ascii_digit()) {
52
+ if needs_gap {
53
+ out.push('_');
54
+ needs_gap = false;
55
+ }
56
+ out.push(c);
57
+ } else if !out.is_empty() {
58
+ needs_gap = true;
59
+ }
60
+ }
61
+ if out.is_empty() {
62
+ out.push('_');
63
+ }
64
+ out
65
+ }
66
+
67
+ /// Globals the generated code reads (the helpers' names are reserved as they
68
+ /// are used). A generated binding never takes one of these names.
69
+ const READ_BY_OUTPUT: [&str; 5] = ["Object", "Promise", "require", "module", "exports"];
70
+
71
+ /// The module's names, and the renames that keep generated names free.
72
+ ///
73
+ /// What is in use is kept as hashes: a module of a hundred thousand symbols
74
+ /// would otherwise copy every name. A hash that collides only makes a free
75
+ /// name look taken, which costs a suffix or a rename, never a clash.
76
+ pub struct Names {
77
+ /// Every name in use: symbols (any scope), unbound globals, generated names.
78
+ taken: FxHashSet<u64>,
79
+ unresolved: FxHashSet<String>,
80
+ generated: FxHashSet<String>,
81
+ }
82
+
83
+ fn hash(name: &str) -> u64 {
84
+ let mut hasher = FxHasher::default();
85
+ name.hash(&mut hasher);
86
+ hasher.finish()
87
+ }
88
+
89
+ impl Names {
90
+ pub fn new(scoping: &Scoping) -> Self {
91
+ let mut taken: FxHashSet<u64> = scoping.symbol_names().map(hash).collect();
92
+ let unresolved: FxHashSet<String> =
93
+ scoping.root_unresolved_references().keys().map(|k| k.to_string()).collect();
94
+ taken.extend(unresolved.iter().map(|n| hash(n)));
95
+ Self { taken, unresolved, generated: FxHashSet::default() }
96
+ }
97
+
98
+ /// Claim `name` for a generated binding and return the spelling to use.
99
+ pub fn generate(&mut self, name: &str, scoping: &mut Scoping, allocator: &Allocator) -> String {
100
+ // `export * as default from "x"` names its record after a reserved
101
+ // word, `export * as Object from "x"` after a global the output reads:
102
+ // `default2`, `Object2`.
103
+ let name = if self.unresolved.contains(name)
104
+ || self.generated.contains(name)
105
+ || is_reserved_keyword(name)
106
+ || READ_BY_OUTPUT.contains(&name)
107
+ {
108
+ self.fresh(name)
109
+ } else {
110
+ self.evict(name, scoping, allocator);
111
+ name.to_string()
112
+ };
113
+ self.taken.insert(hash(&name));
114
+ self.generated.insert(name.clone());
115
+ name
116
+ }
117
+
118
+ /// Generated code refers to the global (or wrapper parameter) `name`:
119
+ /// rename every symbol that would shadow it.
120
+ pub fn reserve_global(&mut self, name: &str, scoping: &mut Scoping, allocator: &Allocator) {
121
+ if self.generated.insert(name.to_string()) {
122
+ self.evict(name, scoping, allocator);
123
+ }
124
+ }
125
+
126
+ fn evict(&mut self, name: &str, scoping: &mut Scoping, allocator: &Allocator) {
127
+ if !self.taken.contains(&hash(name)) {
128
+ return;
129
+ }
130
+ let symbols: Vec<SymbolId> = scoping.symbol_ids().filter(|&s| scoping.symbol_name(s) == name).collect();
131
+ if symbols.is_empty() {
132
+ return;
133
+ }
134
+ let renamed = self.fresh(name);
135
+ self.taken.insert(hash(&renamed));
136
+ let ident = Ident::from_str_in(&renamed, &allocator);
137
+ for symbol in symbols {
138
+ scoping.set_symbol_name(symbol, ident);
139
+ }
140
+ }
141
+
142
+ /// A name no symbol of the module has, for a binding only generated code
143
+ /// sees (a wrapper's parameter): `name` itself, or `name2`, ….
144
+ pub fn claim_fresh(&mut self, name: &str) -> String {
145
+ let name = if self.taken.contains(&hash(name)) { self.fresh(name) } else { name.to_string() };
146
+ self.taken.insert(hash(&name));
147
+ self.generated.insert(name.clone());
148
+ name
149
+ }
150
+
151
+ /// `name2`, `name3`, … : the first not in use.
152
+ fn fresh(&self, name: &str) -> String {
153
+ (2u32..)
154
+ .map(|n| format!("{name}{n}"))
155
+ .find(|candidate| !self.taken.contains(&hash(candidate)))
156
+ .expect("an unused name")
157
+ }
158
+ }
159
+
160
+ #[cfg(test)]
161
+ mod tests {
162
+ use super::name_from_path;
163
+
164
+ #[test]
165
+ fn names_follow_esbuild() {
166
+ assert_eq!(name_from_path("@scope/pkg"), "pkg");
167
+ assert_eq!(name_from_path("node:fs"), "node_fs");
168
+ assert_eq!(name_from_path("./foo/bar-baz.js"), "bar_baz");
169
+ assert_eq!(name_from_path("../x/index.mjs"), "x");
170
+ assert_eq!(name_from_path("lodash.get"), "lodash");
171
+ assert_eq!(name_from_path("1abc"), "abc");
172
+ assert_eq!(name_from_path("."), "_");
173
+ assert_eq!(name_from_path("a"), "a");
174
+ }
175
+ }
@@ -0,0 +1,154 @@
1
+ //! Transform options, and their wire form.
2
+ //!
3
+ //! The JavaScript side sends options as NUL-separated fields: a key, then its
4
+ //! value. `define` repeats, carrying `key\0value` pairs as two fields after
5
+ //! its own key. Unknown keys are an error rather than ignored, so a caller
6
+ //! that asks for something this engine does not do hears about it.
7
+
8
+ #[derive(Clone, Copy, Debug, PartialEq, Eq)]
9
+ pub enum Loader {
10
+ Js,
11
+ Jsx,
12
+ Ts,
13
+ Tsx,
14
+ }
15
+
16
+ /// The module format of the output. `Preserve` keeps the module's own syntax;
17
+ /// `Esm` additionally wraps a CommonJS module as an ES module's default
18
+ /// export; `Cjs` lowers ES module syntax to CommonJS.
19
+ #[derive(Clone, Copy, Debug, PartialEq, Eq)]
20
+ pub enum Format {
21
+ Preserve,
22
+ Esm,
23
+ Cjs,
24
+ }
25
+
26
+ #[derive(Clone, Copy, Debug, PartialEq, Eq)]
27
+ pub enum JsxMode {
28
+ /// `React.createElement`, or `jsx_factory` / `jsx_fragment`.
29
+ Transform,
30
+ /// `react/jsx-runtime`.
31
+ Automatic,
32
+ Preserve,
33
+ }
34
+
35
+ #[derive(Clone, Copy, Debug, PartialEq, Eq)]
36
+ pub enum SourceMapMode {
37
+ None,
38
+ /// Returned beside the code.
39
+ External,
40
+ /// Appended to the code as a data URL.
41
+ Inline,
42
+ }
43
+
44
+ #[derive(Clone, Debug)]
45
+ pub struct Options {
46
+ pub loader: Loader,
47
+ pub format: Format,
48
+ pub jsx: JsxMode,
49
+ pub jsx_factory: Option<String>,
50
+ pub jsx_fragment: Option<String>,
51
+ /// `(expression, replacement)`, applied to unbound globals.
52
+ pub define: Vec<(String, String)>,
53
+ pub sourcemap: SourceMapMode,
54
+ /// The name diagnostics and source maps give the input (`<stdin>` when unset).
55
+ pub sourcefile: Option<String>,
56
+ /// Whether the output keeps `import()`; otherwise it becomes a `require`.
57
+ pub supported_dynamic_import: bool,
58
+ /// Whether the output keeps `import.meta`; otherwise it reads an empty object.
59
+ pub supported_import_meta: bool,
60
+ }
61
+
62
+ impl Default for Options {
63
+ fn default() -> Self {
64
+ Self {
65
+ loader: Loader::Js,
66
+ format: Format::Preserve,
67
+ jsx: JsxMode::Transform,
68
+ jsx_factory: None,
69
+ jsx_fragment: None,
70
+ define: Vec::new(),
71
+ sourcemap: SourceMapMode::None,
72
+ sourcefile: None,
73
+ supported_dynamic_import: true,
74
+ supported_import_meta: true,
75
+ }
76
+ }
77
+ }
78
+
79
+ impl Options {
80
+ /// Parse the wire form described in the module documentation.
81
+ pub fn decode(wire: &str) -> Result<Self, String> {
82
+ let mut options = Self::default();
83
+ if wire.is_empty() {
84
+ return Ok(options);
85
+ }
86
+ let mut fields = wire.split('\0');
87
+ while let Some(key) = fields.next() {
88
+ let mut value = || fields.next().ok_or_else(|| format!("option {key:?} has no value"));
89
+ match key {
90
+ "loader" => {
91
+ options.loader = match value()? {
92
+ "js" => Loader::Js,
93
+ "jsx" => Loader::Jsx,
94
+ "ts" => Loader::Ts,
95
+ "tsx" => Loader::Tsx,
96
+ other => return Err(format!("unsupported loader {other:?}")),
97
+ }
98
+ }
99
+ "format" => {
100
+ options.format = match value()? {
101
+ "preserve" => Format::Preserve,
102
+ "esm" => Format::Esm,
103
+ "cjs" => Format::Cjs,
104
+ other => return Err(format!("unsupported format {other:?}")),
105
+ }
106
+ }
107
+ "jsx" => {
108
+ options.jsx = match value()? {
109
+ "transform" => JsxMode::Transform,
110
+ "automatic" => JsxMode::Automatic,
111
+ "preserve" => JsxMode::Preserve,
112
+ other => return Err(format!("unsupported jsx mode {other:?}")),
113
+ }
114
+ }
115
+ "jsxFactory" => options.jsx_factory = Some(value()?.to_string()),
116
+ "jsxFragment" => options.jsx_fragment = Some(value()?.to_string()),
117
+ "define" => {
118
+ let name = value()?.to_string();
119
+ let replacement = value()?.to_string();
120
+ options.define.push((name, replacement));
121
+ }
122
+ "sourcemap" => {
123
+ options.sourcemap = match value()? {
124
+ "none" => SourceMapMode::None,
125
+ "external" => SourceMapMode::External,
126
+ "inline" => SourceMapMode::Inline,
127
+ other => return Err(format!("unsupported sourcemap mode {other:?}")),
128
+ }
129
+ }
130
+ "sourcefile" => options.sourcefile = Some(value()?.to_string()),
131
+ "dynamicImport" => options.supported_dynamic_import = flag(value()?)?,
132
+ "importMeta" => options.supported_import_meta = flag(value()?)?,
133
+ other => return Err(format!("unknown option {other:?}")),
134
+ }
135
+ }
136
+ if options.format == Format::Cjs
137
+ && options.jsx == JsxMode::Preserve
138
+ && matches!(options.loader, Loader::Jsx | Loader::Tsx)
139
+ {
140
+ // A preserved `<C />` names an import that CommonJS output moves
141
+ // onto a record; module.rs does not rewrite JSX names.
142
+ return Err("jsx \"preserve\" is not supported with format \"cjs\"".into());
143
+ }
144
+ Ok(options)
145
+ }
146
+ }
147
+
148
+ fn flag(value: &str) -> Result<bool, String> {
149
+ match value {
150
+ "1" => Ok(true),
151
+ "0" => Ok(false),
152
+ other => Err(format!("expected 0 or 1, got {other:?}")),
153
+ }
154
+ }