Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make API for exporting JS content more explicit #4333

Merged
merged 1 commit into from
Dec 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 49 additions & 27 deletions crates/cli-support/src/js/mod.rs
Original file line number Diff line number Diff line change
@@ -120,6 +120,18 @@ struct FieldAccessor {
is_optional: bool,
}

/// Different JS constructs that can be exported.
enum ExportJs<'a> {
/// A class of the form `class Name {...}`.
Class(&'a str),
/// An anonymous function expression of the form `function(...) {...}`.
///
/// Note that the function name is not included in the string.
Function(&'a str),
/// An arbitrary JS expression.
Expression(&'a str),
}

const INITIAL_HEAP_VALUES: &[&str] = &["undefined", "null", "true", "false"];
// Must be kept in sync with `src/lib.rs` of the `wasm-bindgen` crate
const INITIAL_HEAP_OFFSET: usize = 128;
@@ -163,38 +175,46 @@ impl<'a> Context<'a> {
fn export(
&mut self,
export_name: &str,
contents: &str,
export: ExportJs,
comments: Option<&str>,
) -> Result<(), Error> {
let definition_name = self.generate_identifier(export_name);
if contents.starts_with("class") && definition_name != export_name {
if matches!(export, ExportJs::Class(_)) && definition_name != export_name {
bail!("cannot shadow already defined class `{}`", export_name);
}

let contents = contents.trim();
daxpedda marked this conversation as resolved.
Show resolved Hide resolved
// write out comments
if let Some(c) = comments {
self.globals.push_str(c);
}

let global = match self.config.mode {
OutputMode::Node { module: false } => {
if contents.starts_with("class") {
format!("{}\nmodule.exports.{1} = {1};\n", contents, export_name)
} else {
format!("module.exports.{} = {};\n", export_name, contents)
OutputMode::Node { module: false } => match export {
ExportJs::Class(class) => {
format!("{}\nmodule.exports.{1} = {1};\n", class, export_name)
}
}
OutputMode::NoModules { .. } => {
if contents.starts_with("class") {
format!("{}\n__exports.{1} = {1};\n", contents, export_name)
} else {
format!("__exports.{} = {};\n", export_name, contents)
ExportJs::Function(expr) | ExportJs::Expression(expr) => {
format!("module.exports.{} = {};\n", export_name, expr)
}
}
},
OutputMode::NoModules { .. } => match export {
ExportJs::Class(class) => {
format!("{}\n__exports.{1} = {1};\n", class, export_name)
}
ExportJs::Function(expr) | ExportJs::Expression(expr) => {
format!("__exports.{} = {};\n", export_name, expr)
}
},
OutputMode::Bundler { .. }
| OutputMode::Node { module: true }
| OutputMode::Web
| OutputMode::Deno => {
if let Some(body) = contents.strip_prefix("function") {
| OutputMode::Deno => match export {
ExportJs::Class(class) => {
assert_eq!(export_name, definition_name);
format!("export {}\n", class)
}
ExportJs::Function(function) => {
let body = function.strip_prefix("function").unwrap();
if export_name == definition_name {
format!("export function {}{}\n", export_name, body)
} else {
@@ -203,14 +223,12 @@ impl<'a> Context<'a> {
definition_name, body, definition_name, export_name,
)
}
} else if contents.starts_with("class") {
assert_eq!(export_name, definition_name);
format!("export {}\n", contents)
} else {
}
ExportJs::Expression(expr) => {
assert_eq!(export_name, definition_name);
format!("export const {} = {};\n", export_name, contents)
format!("export const {} = {};\n", export_name, expr)
}
}
},
};
self.global(&global);
Ok(())
@@ -1169,10 +1187,10 @@ __wbg_set_wasm(wasm);"

self.write_class_field_types(class, &mut ts_dst);

dst.push_str("}\n");
dst.push('}');
daxpedda marked this conversation as resolved.
Show resolved Hide resolved
ts_dst.push_str("}\n");

self.export(name, &dst, Some(&class.comments))?;
self.export(name, ExportJs::Class(&dst), Some(&class.comments))?;

if class.generate_typescript {
self.typescript.push_str(&class.comments);
@@ -2872,7 +2890,11 @@ __wbg_set_wasm(wasm);"
self.typescript.push_str(";\n");
}

self.export(name, &format!("function{}", code), Some(&js_docs))?;
self.export(
name,
ExportJs::Function(&format!("function{}", code)),
Some(&js_docs),
)?;
self.globals.push('\n');
}
AuxExportKind::Constructor(class) => {
@@ -3995,7 +4017,7 @@ __wbg_set_wasm(wasm);"

self.export(
&enum_.name,
&format!("Object.freeze({{\n{}}})", variants),
ExportJs::Expression(&format!("Object.freeze({{\n{}}})", variants)),
Some(&docs),
)?;