@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,139 @@
1
+ //! The runtime helpers module conversion emits, verbatim from esbuild 0.24.2
2
+ //! (MIT, Copyright (c) 2020 Evan Wallace), in the order esbuild prints them.
3
+ //! Their names are part of the contract: Nimbus's server-launch analysis
4
+ //! recognizes `__toESM(require(...))` and `__toCommonJS(...)` by name.
5
+
6
+ #[derive(Clone, Copy, Debug, PartialEq, Eq)]
7
+ #[repr(u16)]
8
+ pub enum Helper {
9
+ Create = 1 << 0,
10
+ DefProp = 1 << 1,
11
+ GetOwnPropDesc = 1 << 2,
12
+ GetOwnPropNames = 1 << 3,
13
+ GetProtoOf = 1 << 4,
14
+ HasOwnProp = 1 << 5,
15
+ CommonJs = 1 << 6,
16
+ Export = 1 << 7,
17
+ CopyProps = 1 << 8,
18
+ ReExport = 1 << 9,
19
+ ToEsm = 1 << 10,
20
+ ToCommonJs = 1 << 11,
21
+ }
22
+
23
+ const ALL: [Helper; 12] = [
24
+ Helper::Create,
25
+ Helper::DefProp,
26
+ Helper::GetOwnPropDesc,
27
+ Helper::GetOwnPropNames,
28
+ Helper::GetProtoOf,
29
+ Helper::HasOwnProp,
30
+ Helper::CommonJs,
31
+ Helper::Export,
32
+ Helper::CopyProps,
33
+ Helper::ReExport,
34
+ Helper::ToEsm,
35
+ Helper::ToCommonJs,
36
+ ];
37
+
38
+ impl Helper {
39
+ pub fn name(self) -> &'static str {
40
+ match self {
41
+ Self::Create => "__create",
42
+ Self::DefProp => "__defProp",
43
+ Self::GetOwnPropDesc => "__getOwnPropDesc",
44
+ Self::GetOwnPropNames => "__getOwnPropNames",
45
+ Self::GetProtoOf => "__getProtoOf",
46
+ Self::HasOwnProp => "__hasOwnProp",
47
+ Self::CommonJs => "__commonJS",
48
+ Self::Export => "__export",
49
+ Self::CopyProps => "__copyProps",
50
+ Self::ReExport => "__reExport",
51
+ Self::ToEsm => "__toESM",
52
+ Self::ToCommonJs => "__toCommonJS",
53
+ }
54
+ }
55
+
56
+ fn source(self) -> &'static str {
57
+ match self {
58
+ Self::Create => "var __create = Object.create;\n",
59
+ Self::DefProp => "var __defProp = Object.defineProperty;\n",
60
+ Self::GetOwnPropDesc => "var __getOwnPropDesc = Object.getOwnPropertyDescriptor;\n",
61
+ Self::GetOwnPropNames => "var __getOwnPropNames = Object.getOwnPropertyNames;\n",
62
+ Self::GetProtoOf => "var __getProtoOf = Object.getPrototypeOf;\n",
63
+ Self::HasOwnProp => "var __hasOwnProp = Object.prototype.hasOwnProperty;\n",
64
+ Self::CommonJs => concat!(
65
+ "var __commonJS = (cb, mod) => function __require() {\n",
66
+ " return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;\n",
67
+ "};\n",
68
+ ),
69
+ Self::Export => concat!(
70
+ "var __export = (target, all) => {\n",
71
+ " for (var name in all)\n",
72
+ " __defProp(target, name, { get: all[name], enumerable: true });\n",
73
+ "};\n",
74
+ ),
75
+ Self::CopyProps => concat!(
76
+ "var __copyProps = (to, from, except, desc) => {\n",
77
+ " if (from && typeof from === \"object\" || typeof from === \"function\") {\n",
78
+ " for (let key of __getOwnPropNames(from))\n",
79
+ " if (!__hasOwnProp.call(to, key) && key !== except)\n",
80
+ " __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });\n",
81
+ " }\n",
82
+ " return to;\n",
83
+ "};\n",
84
+ ),
85
+ Self::ReExport => "var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, \"default\"), secondTarget && __copyProps(secondTarget, mod, \"default\"));\n",
86
+ Self::ToEsm => concat!(
87
+ "var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(\n",
88
+ " isNodeMode || !mod || !mod.__esModule ? __defProp(target, \"default\", { value: mod, enumerable: true }) : target,\n",
89
+ " mod\n",
90
+ "));\n",
91
+ ),
92
+ Self::ToCommonJs => "var __toCommonJS = (mod) => __copyProps(__defProp({}, \"__esModule\", { value: true }), mod);\n",
93
+ }
94
+ }
95
+
96
+ fn requires(self) -> u16 {
97
+ use Helper::*;
98
+ match self {
99
+ CommonJs => GetOwnPropNames as u16,
100
+ Export => DefProp as u16,
101
+ CopyProps => DefProp as u16 | GetOwnPropNames as u16 | HasOwnProp as u16 | GetOwnPropDesc as u16,
102
+ ReExport => CopyProps as u16,
103
+ ToEsm => Create as u16 | GetProtoOf as u16 | DefProp as u16 | CopyProps as u16,
104
+ ToCommonJs => CopyProps as u16 | DefProp as u16,
105
+ _ => 0,
106
+ }
107
+ }
108
+ }
109
+
110
+ /// A set of helpers, closed over what each one calls.
111
+ #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
112
+ pub struct Helpers(u16);
113
+
114
+ impl Helpers {
115
+ pub fn add(&mut self, helper: Helper) {
116
+ if self.0 & helper as u16 != 0 {
117
+ return;
118
+ }
119
+ self.0 |= helper as u16;
120
+ for dependency in ALL {
121
+ if helper.requires() & dependency as u16 != 0 {
122
+ self.add(dependency);
123
+ }
124
+ }
125
+ }
126
+
127
+ pub fn is_empty(self) -> bool {
128
+ self.0 == 0
129
+ }
130
+
131
+ pub fn iter(self) -> impl Iterator<Item = Helper> {
132
+ ALL.into_iter().filter(move |h| self.0 & *h as u16 != 0)
133
+ }
134
+
135
+ /// The helpers' source, in esbuild's order.
136
+ pub fn source(self) -> String {
137
+ self.iter().map(Helper::source).collect()
138
+ }
139
+ }
@@ -0,0 +1,247 @@
1
+ //! Nimbus's JavaScript/TypeScript transform: Oxc's parser, semantic analysis,
2
+ //! TypeScript/JSX transformer, global defines and code generator, plus the one
3
+ //! pass Oxc does not have, `module.rs`: ES module to CommonJS (and the
4
+ //! `import.meta`, dynamic `import()` and top-level `this` handling that goes
5
+ //! with a module format), shaped like esbuild 0.24's transform output, which
6
+ //! is the contract the rest of Nimbus was built against.
7
+ //!
8
+ //! The crate is a pure function from (source, options) to (code, map,
9
+ //! diagnostics). `abi.rs` exposes it to JavaScript as a wasm32 module with no
10
+ //! imports.
11
+
12
+ mod diagnostics;
13
+ mod helpers;
14
+ mod module;
15
+ mod names;
16
+ pub mod options;
17
+
18
+ #[cfg(target_arch = "wasm32")]
19
+ mod abi;
20
+
21
+ use oxc::allocator::Allocator;
22
+ use oxc::ast::ast::{Program, Statement};
23
+ use oxc::codegen::{Codegen, CodegenOptions, CommentOptions, LegalComment};
24
+ use oxc::parser::{ParseOptions, Parser};
25
+ use oxc::semantic::SemanticBuilder;
26
+ use oxc::diagnostics::OxcDiagnostic;
27
+ use oxc::span::{SourceType, Span};
28
+ use oxc::transformer::{
29
+ EnvOptions, JsxOptions, JsxRuntime, TransformOptions, Transformer, TypeScriptOptions,
30
+ };
31
+ use oxc::transformer_plugins::{ReplaceGlobalDefines, ReplaceGlobalDefinesConfig};
32
+
33
+ pub use diagnostics::Diagnostic;
34
+ use options::{JsxMode, Loader, Options, SourceMapMode};
35
+
36
+ /// What one transform produced. `code` is empty when `diagnostics` holds an error.
37
+ pub struct Output {
38
+ pub code: String,
39
+ /// The source map JSON, for `SourceMapMode::External`.
40
+ pub map: Option<String>,
41
+ pub diagnostics: Vec<Diagnostic>,
42
+ }
43
+
44
+ impl Output {
45
+ fn failed(diagnostics: Vec<Diagnostic>) -> Self {
46
+ Self { code: String::new(), map: None, diagnostics }
47
+ }
48
+
49
+ pub fn has_errors(&self) -> bool {
50
+ self.diagnostics.iter().any(|d| d.error)
51
+ }
52
+ }
53
+
54
+ /// Transform `source` as `options` describe, allocating the AST in `allocator`.
55
+ pub fn transform(allocator: &Allocator, source: &str, options: &Options) -> Output {
56
+ let source_type = match options.loader {
57
+ Loader::Js => SourceType::unambiguous(),
58
+ Loader::Jsx => SourceType::unambiguous().with_jsx(true),
59
+ Loader::Ts => SourceType::ts().with_unambiguous(true),
60
+ Loader::Tsx => SourceType::tsx().with_unambiguous(true),
61
+ };
62
+ let sourcefile = options.sourcefile.as_deref().unwrap_or("<stdin>");
63
+ let parse = |source_type, allow_return_outside_function| {
64
+ Parser::new(allocator, source, source_type)
65
+ .with_options(ParseOptions {
66
+ preserve_parens: false,
67
+ allow_return_outside_function,
68
+ ..ParseOptions::default()
69
+ })
70
+ .parse()
71
+ };
72
+ let failed = |parsed: &oxc::parser::ParserReturn<'_>| parsed.diagnostics.has_errors() || parsed.fatal_error;
73
+ let mut parsed = parse(source_type, false);
74
+ let mut parsed_as_module = false;
75
+ // Where a top-level `return` is, in a source that only parses with one.
76
+ let mut top_level_return = None;
77
+ if failed(&parsed) {
78
+ // Unambiguous parsing reads top-level `await` as a module's, but not
79
+ // `for await`: a source only a module can be is parsed as one. A
80
+ // CommonJS module may `return` at its top level (its wrapper is a
81
+ // function), which an ES module may not: that is decided below.
82
+ let as_module = parse(source_type.with_module(true), false);
83
+ if !failed(&as_module) {
84
+ parsed = as_module;
85
+ parsed_as_module = true;
86
+ } else {
87
+ let with_return = parse(source_type, true);
88
+ if failed(&with_return) {
89
+ return Output::failed(diagnostics::convert(source, sourcefile, parsed.diagnostics.into_vec()));
90
+ }
91
+ top_level_return = parsed.diagnostics.iter().find_map(|d| d.labels.first()).map(|label| {
92
+ Span::sized(label.offset() as u32, label.span().size())
93
+ });
94
+ parsed = with_return;
95
+ }
96
+ }
97
+ let has_dynamic_import = !parsed.module_record.dynamic_imports.is_empty();
98
+ let has_import_meta = !parsed.module_record.import_metas.is_empty();
99
+ let mut program = parsed.program;
100
+ // Export syntax decides whether the module gets an exports object, even
101
+ // when all of it is TypeScript types the transformer is about to erase.
102
+ let has_export_syntax = program.body.iter().any(is_export_statement);
103
+
104
+ let semantic = SemanticBuilder::new().with_check_syntax_error(true).with_enum_eval(true).build(&program);
105
+ if semantic.diagnostics.has_errors() {
106
+ return Output::failed(diagnostics::convert(source, sourcefile, semantic.diagnostics.into_vec()));
107
+ }
108
+ let mut scoping = semantic.semantic.into_scoping();
109
+ let mut warnings = Vec::new();
110
+
111
+ if source_type.is_typescript() || source_type.is_jsx() {
112
+ let transform_options = transform_options(options);
113
+ let transformed = Transformer::new(allocator, std::path::Path::new(sourcefile), &transform_options)
114
+ .build_with_scoping(scoping, &mut program);
115
+ if transformed.diagnostics.has_errors() {
116
+ return Output::failed(diagnostics::convert(source, sourcefile, transformed.diagnostics.into_vec()));
117
+ }
118
+ scoping = transformed.scoping;
119
+ }
120
+
121
+ if !options.define.is_empty() {
122
+ let config = match ReplaceGlobalDefinesConfig::new(&options.define) {
123
+ Ok(config) => config,
124
+ Err(errors) => return Output::failed(diagnostics::convert(source, sourcefile, errors.into_vec())),
125
+ };
126
+ scoping = ReplaceGlobalDefines::new(allocator, config).build(scoping, &mut program).scoping;
127
+ }
128
+
129
+ // An ES module, as esbuild decides it: export syntax (types included), an
130
+ // import that survived TypeScript's elision, `import.meta`, or top-level
131
+ // await (what made a parse as a module the only one that succeeds).
132
+ let mut is_esm = has_export_syntax
133
+ || has_import_meta
134
+ || parsed_as_module
135
+ || program.body.iter().any(|s| matches!(s, Statement::ImportDeclaration(_)));
136
+ // Unambiguous parsing accepts `await` at the top of a script; only `this`
137
+ // reads the difference, so only a source with both is walked for it.
138
+ if !is_esm && memchr::memmem::find(source.as_bytes(), b"await").is_some() && memchr::memmem::find(source.as_bytes(), b"this").is_some() {
139
+ is_esm = module::has_top_level_await(&program);
140
+ }
141
+ if is_esm && let Some(span) = top_level_return {
142
+ let error = OxcDiagnostic::error("Top-level return cannot be used inside an ECMAScript module").with_label(span);
143
+ return Output::failed(diagnostics::convert(source, sourcefile, vec![error]));
144
+ }
145
+ let pass = module::ModulePass::new(module::ModuleOptions {
146
+ format: options.format,
147
+ dynamic_import: options.supported_dynamic_import,
148
+ import_meta: options.supported_import_meta,
149
+ is_esm,
150
+ has_export_syntax,
151
+ has_import_meta,
152
+ has_dynamic_import,
153
+ });
154
+ let (scoping, outcome) = pass.run(allocator, &mut program, scoping);
155
+ match outcome {
156
+ Err(errors) => return Output::failed(diagnostics::convert(source, sourcefile, errors)),
157
+ Ok(pass_warnings) => warnings.extend(pass_warnings),
158
+ }
159
+
160
+ let (code, map) = generate(&program, scoping, options, sourcefile);
161
+ Output { code, map, diagnostics: diagnostics::convert(source, sourcefile, warnings) }
162
+ }
163
+
164
+ fn generate(
165
+ program: &Program<'_>,
166
+ scoping: oxc::semantic::Scoping,
167
+ options: &Options,
168
+ sourcefile: &str,
169
+ ) -> (String, Option<String>) {
170
+ let codegen_options = CodegenOptions {
171
+ // esbuild's default charset is ASCII: everything else is escaped.
172
+ ascii_only: true,
173
+ indent_char: oxc::codegen::IndentChar::Space,
174
+ indent_width: 2,
175
+ comments: CommentOptions {
176
+ normal: false,
177
+ jsdoc: false,
178
+ annotation: true,
179
+ legal: LegalComment::Inline,
180
+ ..CommentOptions::default()
181
+ },
182
+ source_map_path: (options.sourcemap != SourceMapMode::None).then(|| sourcefile.into()),
183
+ ..CodegenOptions::default()
184
+ };
185
+ let generated = Codegen::new().with_options(codegen_options).with_scoping(Some(scoping)).build(program);
186
+ let mut code = generated.code;
187
+ let map = generated.map.map(|mut map| {
188
+ map.set_source_contents(vec![Some(program.source_text)]);
189
+ map
190
+ });
191
+ match (options.sourcemap, map) {
192
+ (SourceMapMode::Inline, Some(map)) => {
193
+ if !code.ends_with('\n') {
194
+ code.push('\n');
195
+ }
196
+ code.push_str("//# sourceMappingURL=");
197
+ code.push_str(&map.to_data_url());
198
+ code.push('\n');
199
+ (code, None)
200
+ }
201
+ (SourceMapMode::External, Some(map)) => (code, Some(map.to_json_string())),
202
+ _ => (code, None),
203
+ }
204
+ }
205
+
206
+ fn transform_options(options: &Options) -> TransformOptions {
207
+ let jsx = match options.jsx {
208
+ JsxMode::Preserve => JsxOptions { jsx_plugin: false, display_name_plugin: false, ..JsxOptions::disable() },
209
+ JsxMode::Automatic => JsxOptions {
210
+ runtime: JsxRuntime::Automatic,
211
+ display_name_plugin: false,
212
+ ..JsxOptions::default()
213
+ },
214
+ JsxMode::Transform => JsxOptions {
215
+ runtime: JsxRuntime::Classic,
216
+ display_name_plugin: false,
217
+ pragma: options.jsx_factory.clone(),
218
+ pragma_frag: options.jsx_fragment.clone(),
219
+ ..JsxOptions::default()
220
+ },
221
+ };
222
+ TransformOptions {
223
+ typescript: TypeScriptOptions {
224
+ jsx_pragma: options.jsx_factory.clone().map(Into::into).unwrap_or_else(|| "React.createElement".into()),
225
+ jsx_pragma_frag: options.jsx_fragment.clone().map(Into::into).unwrap_or_else(|| "React.Fragment".into()),
226
+ ..TypeScriptOptions::default()
227
+ },
228
+ jsx,
229
+ env: EnvOptions::default(),
230
+ ..TransformOptions::default()
231
+ }
232
+ }
233
+
234
+ fn is_export_statement(statement: &Statement<'_>) -> bool {
235
+ matches!(
236
+ statement,
237
+ Statement::ExportAllDeclaration(_)
238
+ | Statement::ExportDefaultDeclaration(_)
239
+ | Statement::ExportNamedDeclaration(_)
240
+ | Statement::ExportFromDeclaration(_)
241
+ | Statement::ExportDeclaration(_)
242
+ )
243
+ }
244
+
245
+ /// What `Format::Cjs` reports for top-level await, word for word esbuild's
246
+ /// message: the caller recognizes it and lowers the module itself.
247
+ pub const TOP_LEVEL_AWAIT_CJS: &str = "Top-level await is currently not supported with the \"cjs\" output format";