@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.
- package/dist/esbuild-cli-artifact.generated.d.ts +1 -1
- package/dist/esbuild-cli-artifact.generated.js +4 -4
- package/dist/esbuild-wasm-bundle.generated.d.ts +11 -10
- package/dist/esbuild-wasm-bundle.generated.d.ts.map +1 -1
- package/dist/esbuild-wasm-bundle.generated.js +11 -10
- package/dist/facets/build-facet.d.ts +53 -0
- package/dist/facets/build-facet.d.ts.map +1 -0
- package/dist/facets/build-facet.js +346 -0
- package/dist/facets/cirrus-real.d.ts +3 -0
- package/dist/facets/cirrus-real.d.ts.map +1 -1
- package/dist/facets/cirrus-real.js +1 -2
- package/dist/facets/compose.d.ts +1 -0
- package/dist/facets/compose.d.ts.map +1 -1
- package/dist/facets/compose.js +3 -0
- package/dist/facets/data-plan.d.ts +14 -30
- package/dist/facets/data-plan.d.ts.map +1 -1
- package/dist/facets/data-plan.js +153 -13
- package/dist/facets/dev-stylesheet.d.ts +27 -0
- package/dist/facets/dev-stylesheet.d.ts.map +1 -0
- package/dist/facets/dev-stylesheet.js +65 -0
- package/dist/facets/esbuild-transform.d.ts +23 -18
- package/dist/facets/esbuild-transform.d.ts.map +1 -1
- package/dist/facets/esbuild-transform.js +63 -109
- package/dist/facets/manager.d.ts +49 -9
- package/dist/facets/manager.d.ts.map +1 -1
- package/dist/facets/manager.js +238 -69
- package/dist/facets/oxc-transform.d.ts +29 -0
- package/dist/facets/oxc-transform.d.ts.map +1 -0
- package/dist/facets/oxc-transform.js +198 -0
- package/dist/facets/prebundle-pool.d.ts +23 -0
- package/dist/facets/prebundle-pool.d.ts.map +1 -0
- package/dist/facets/prebundle-pool.js +55 -0
- package/dist/facets/process.d.ts +13 -6
- package/dist/facets/process.d.ts.map +1 -1
- package/dist/facets/process.js +37 -31
- package/dist/facets/transform-store.d.ts +4 -4
- package/dist/facets/transform-store.js +4 -4
- package/dist/facets/vite-dev-server.d.ts +89 -12
- package/dist/facets/vite-dev-server.d.ts.map +1 -1
- package/dist/facets/vite-dev-server.js +66 -43
- package/dist/hosted/commands.d.ts.map +1 -1
- package/dist/hosted/commands.js +20 -4
- package/dist/hosted/runtime.d.ts.map +1 -1
- package/dist/hosted/runtime.js +7 -2
- package/dist/hosted/services.d.ts +3 -3
- package/dist/hosted/services.d.ts.map +1 -1
- package/dist/hosted/services.js +102 -95
- package/dist/hosted/session.d.ts +1 -0
- package/dist/hosted/session.d.ts.map +1 -1
- package/dist/loaders/generated-workers.d.ts +3 -0
- package/dist/loaders/generated-workers.d.ts.map +1 -1
- package/dist/loaders/generated-workers.js +3 -0
- package/dist/napi-wasm-artifacts.generated.js +2 -2
- package/dist/node-shims-artifact.generated.d.ts +14 -1
- package/dist/node-shims-artifact.generated.d.ts.map +1 -1
- package/dist/node-shims-artifact.generated.js +23 -10
- package/dist/npm/bin-links.d.ts +7 -4
- package/dist/npm/bin-links.d.ts.map +1 -1
- package/dist/npm/bin-links.js +14 -55
- package/dist/npm/cache.d.ts +6 -0
- package/dist/npm/cache.d.ts.map +1 -1
- package/dist/npm/cache.js +26 -4
- package/dist/npm/installer.d.ts +1 -1
- package/dist/npm/installer.d.ts.map +1 -1
- package/dist/npm/installer.js +26 -52
- package/dist/npm/pre-bundle-facet.d.ts +15 -117
- package/dist/npm/pre-bundle-facet.d.ts.map +1 -1
- package/dist/npm/pre-bundle-facet.js +13 -511
- package/dist/oxc-facet-artifact.generated.d.ts +18 -0
- package/dist/oxc-facet-artifact.generated.d.ts.map +1 -0
- package/dist/oxc-facet-artifact.generated.js +17 -0
- package/dist/oxc-wasm-artifact.generated.d.ts +20 -0
- package/dist/oxc-wasm-artifact.generated.d.ts.map +1 -0
- package/dist/oxc-wasm-artifact.generated.js +19 -0
- package/dist/rolldown-facet-artifact.generated.d.ts +18 -0
- package/dist/rolldown-facet-artifact.generated.d.ts.map +1 -0
- package/dist/rolldown-facet-artifact.generated.js +17 -0
- package/dist/router/remote-api.d.ts.map +1 -1
- package/dist/router/remote-api.js +9 -0
- package/dist/runtime/bash-repl.d.ts.map +1 -1
- package/dist/runtime/bash-repl.js +1 -4
- package/dist/runtime/cpython-resident.d.ts.map +1 -1
- package/dist/runtime/cpython-resident.js +1 -0
- package/dist/runtime/esbuild-wasm-bytes.d.ts +25 -23
- package/dist/runtime/esbuild-wasm-bytes.d.ts.map +1 -1
- package/dist/runtime/esbuild-wasm-bytes.js +30 -25
- package/dist/runtime/facet-loader-host.d.ts +5 -4
- package/dist/runtime/facet-loader-host.d.ts.map +1 -1
- package/dist/runtime/facet-loader-host.js +5 -4
- package/dist/runtime/node-runner.d.ts +2 -0
- package/dist/runtime/node-runner.d.ts.map +1 -1
- package/dist/runtime/node-runner.js +1 -0
- package/dist/runtime/node-shims-artifact.d.ts +8 -1
- package/dist/runtime/node-shims-artifact.d.ts.map +1 -1
- package/dist/runtime/node-shims-artifact.js +32 -4
- package/dist/runtime/node-shims.d.ts.map +1 -1
- package/dist/runtime/node-shims.js +145 -109
- package/dist/runtime/opencode-facet-runner.d.ts.map +1 -1
- package/dist/runtime/opencode-facet-runner.js +6 -2
- package/dist/runtime/oxc-wasm-bytes.d.ts +19 -0
- package/dist/runtime/oxc-wasm-bytes.d.ts.map +1 -0
- package/dist/runtime/oxc-wasm-bytes.js +39 -0
- package/dist/runtime/package-manager.js +1 -1
- package/dist/runtime/process-logs-api.d.ts.map +1 -1
- package/dist/runtime/process-logs-api.js +2 -0
- package/dist/runtime/project-fs.d.ts +8 -1
- package/dist/runtime/project-fs.d.ts.map +1 -1
- package/dist/runtime/project-fs.js +15 -1
- package/dist/runtime/python-repl.d.ts +40 -1
- package/dist/runtime/python-repl.d.ts.map +1 -1
- package/dist/runtime/python-repl.js +19 -11
- package/dist/runtime/ruby-repl.d.ts +38 -0
- package/dist/runtime/ruby-repl.d.ts.map +1 -1
- package/dist/runtime/ruby-repl.js +24 -5
- package/dist/runtime/ruby-resident.d.ts.map +1 -1
- package/dist/runtime/ruby-resident.js +2 -0
- package/dist/session/nimbus-session.d.ts +13 -8
- package/dist/session/nimbus-session.d.ts.map +1 -1
- package/dist/session/nimbus-session.js +11 -5
- package/dist/session/programmatic.d.ts +25 -30
- package/dist/session/programmatic.d.ts.map +1 -1
- package/dist/session/programmatic.js +65 -93
- package/dist/session/routes.d.ts.map +1 -1
- package/dist/session/routes.js +23 -7
- package/dist/session/rpc.d.ts +16 -6
- package/dist/session/rpc.d.ts.map +1 -1
- package/dist/session/rpc.js +53 -13
- package/dist/session/start-real-vite.d.ts +24 -9
- package/dist/session/start-real-vite.d.ts.map +1 -1
- package/dist/session/start-real-vite.js +40 -4
- package/dist/session/supervisor-op.d.ts +14 -0
- package/dist/session/supervisor-op.d.ts.map +1 -1
- package/dist/session/supervisor-op.js +49 -0
- package/dist/session/supervisor-rpc.d.ts +11 -0
- package/dist/session/supervisor-rpc.d.ts.map +1 -1
- package/dist/session/supervisor-rpc.js +18 -0
- package/dist/session/vite-command.d.ts.map +1 -1
- package/dist/session/vite-command.js +23 -19
- package/dist/session/ws-relay.d.ts.map +1 -1
- package/dist/session/ws-relay.js +13 -1
- package/dist/shell/npm-bin-entrypoints.d.ts +3 -1
- package/dist/shell/npm-bin-entrypoints.d.ts.map +1 -1
- package/dist/shell/npm-bin-entrypoints.js +53 -25
- package/dist/vfs/facet-resident-store.d.ts.map +1 -1
- package/dist/vfs/facet-resident-store.js +11 -5
- package/dist/wrangler/nimbus-wrangler.d.ts +8 -0
- package/dist/wrangler/nimbus-wrangler.d.ts.map +1 -1
- package/dist/wrangler/nimbus-wrangler.js +54 -57
- package/package.json +6 -4
- package/public/_assets/esbuild-0.24.2.wasm +0 -0
- package/public/_assets/napi-wasm/loader/{99efae93dd1dcc2b → ab7e57fe9e8750c2}/napi-wasm-loader.mjs +4 -4
- package/public/_assets/napi-wasm/loader/{99efae93dd1dcc2b → ab7e57fe9e8750c2}/provenance.json +2 -2
- package/public/_assets/oxc/nimbus-oxc-1aa8c4c7057c6c28.provenance.json +72 -0
- package/public/_assets/oxc/nimbus-oxc-1aa8c4c7057c6c28.wasm +0 -0
- package/public/_assets/runtime/esbuild-cli-f13e6a112b03ac07.js +1143 -0
- package/public/_assets/runtime/js-interpreter-3c37c48024f0f651.js +23 -0
- package/public/_assets/runtime/js-interpreter-ops-2f4dfec6798e70fc.js +290 -0
- package/public/_assets/runtime/js-interpreter-primordials-e620a61b7e9800fa.js +1 -0
- package/public/_assets/runtime/{node-shims-f92f16c7d7c532e9.js → node-shims-e3adc4576da31e6e.js} +651 -103
- package/public/_assets/runtime/{esbuild-cli-b7aecffd4c6a36fe.js → oxc-facet-fd86fefcdf16bf9c.js} +217 -1047
- package/public/_assets/runtime/{resident-store-92386265e8b306dc.js → resident-store-009f39539ac989fc.js} +11 -5
- package/public/_assets/runtime/rolldown-facet-09983434e2dbe93e.js +15165 -0
- package/public/_assets/runtime/{vfs-write-ledger-d33ee0034446c3e0.js → vfs-write-ledger-9e9594591425a9b8.js} +4 -1
- package/scripts/acorn-primordials.mjs +637 -0
- package/scripts/bundle-esbuild-wasm.mjs +42 -50
- package/scripts/bundle-facet-workers.mjs +247 -18
- package/scripts/bundle-node-shims.mjs +29 -2
- package/scripts/bundle-oxc-wasm.mjs +105 -0
- package/scripts/interpreter-bundle.mjs +105 -0
- package/scripts/napi-wasm/build.mjs +4 -1
- package/scripts/napi-wasm/loader/napi-wasm-loader.ts +38 -3
- package/scripts/oxc-wasm/Cargo.lock +1246 -0
- package/scripts/oxc-wasm/Cargo.toml +30 -0
- package/scripts/oxc-wasm/build.mjs +249 -0
- package/scripts/oxc-wasm/examples/memory.rs +51 -0
- package/scripts/oxc-wasm/examples/transform.rs +20 -0
- package/scripts/oxc-wasm/inputs.mjs +32 -0
- package/scripts/oxc-wasm/rust-toolchain.toml +5 -0
- package/scripts/oxc-wasm/src/abi.rs +136 -0
- package/scripts/oxc-wasm/src/diagnostics.rs +49 -0
- package/scripts/oxc-wasm/src/helpers.rs +139 -0
- package/scripts/oxc-wasm/src/lib.rs +247 -0
- package/scripts/oxc-wasm/src/module.rs +978 -0
- package/scripts/oxc-wasm/src/names.rs +175 -0
- package/scripts/oxc-wasm/src/options.rs +154 -0
- package/scripts/parser-audit.mjs +492 -0
- package/scripts/rolldown-facet/entry.mjs +21 -0
- package/scripts/rolldown-facet/shims.mjs +64 -0
- package/dist/facets/esbuild-bundle-pool.d.ts +0 -43
- package/dist/facets/esbuild-bundle-pool.d.ts.map +0 -1
- package/dist/facets/esbuild-bundle-pool.js +0 -80
- package/dist/loaders/pre-bundle-preamble.d.ts +0 -44
- package/dist/loaders/pre-bundle-preamble.d.ts.map +0 -1
- package/dist/loaders/pre-bundle-preamble.js +0 -116
- package/dist/runtime/host-wasm.d.ts +0 -8
- package/dist/runtime/host-wasm.d.ts.map +0 -1
- package/dist/runtime/host-wasm.js +0 -33
- /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
|
+
}
|