Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "uniffi-bindgen-node"
version = "0.1.3"
version = "0.1.4"
edition = "2024"

[dependencies]
Expand Down
22 changes: 1 addition & 21 deletions src/bindings/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ use crate::bindings::{filters, utils::{DirnameApi, ImportExtension, LibPath, Lib
pub struct Bindings {
pub package_json_contents: String,
pub sys_ts_template_contents: String,
pub commonjs_shim_cts_template_contents: String,
pub node_ts_file_contents: String,
pub index_ts_file_contents: String,
}
Expand Down Expand Up @@ -41,7 +40,6 @@ struct SysTemplate<'ci> {
out_lib_disable_auto_loading: bool,
out_verbose_logs: bool,
out_lib_path: LibPath,
commonjs_shim_cjs_main_file_name: String,
}

impl<'ci> SysTemplate<'ci> {
Expand All @@ -51,32 +49,17 @@ impl<'ci> SysTemplate<'ci> {
out_lib_disable_auto_loading: bool,
out_verbose_logs: bool,
out_lib_path: LibPath,
commonjs_shim_cjs_main_file_name: &str,
) -> Self {
Self {
ci,
out_dirname_api,
out_lib_disable_auto_loading,
out_verbose_logs,
out_lib_path,
commonjs_shim_cjs_main_file_name: commonjs_shim_cjs_main_file_name.into(),
}
}
}

#[derive(Template)]
#[template(escape = "none", path = "commonjs-shim.cts")]
struct CommonJsShimTemplate {
out_lib_path: LibPath,
}

impl CommonJsShimTemplate {
pub fn new(out_lib_path: LibPath) -> Self {
Self { out_lib_path }
}
}



#[derive(Template)]
#[template(escape = "none", path = "node.ts")]
Expand Down Expand Up @@ -133,7 +116,6 @@ impl IndexTsTemplate {
pub struct GenerateNodeBindingsOptions<'a> {
pub sys_ts_main_file_name: &'a str,
pub node_ts_main_file_name: &'a str,
pub commonjs_shim_cts_main_file_name: &'a str,
pub out_dirname_api: DirnameApi,
pub out_lib_disable_auto_loading: bool,
pub out_import_extension: ImportExtension,
Expand All @@ -147,15 +129,13 @@ pub fn generate_node_bindings(
options: GenerateNodeBindingsOptions<'_>,
) -> Result<Bindings> {
let package_json_contents = PackageJsonTemplate::new(ci, options.out_node_version, options.out_lib_path.clone()).render().context("failed to render package.json template")?;
let sys_ts_template_contents = SysTemplate::new(ci, options.out_dirname_api, options.out_lib_disable_auto_loading, options.out_verbose_logs, options.out_lib_path.clone(), options.commonjs_shim_cts_main_file_name).render().context("failed to render sys.ts template")?;
let commonjs_shim_cts_template_contents = CommonJsShimTemplate::new(options.out_lib_path.clone()).render().context("failed to render commonjs_shim.cjs template")?;
let sys_ts_template_contents = SysTemplate::new(ci, options.out_dirname_api, options.out_lib_disable_auto_loading, options.out_verbose_logs, options.out_lib_path.clone()).render().context("failed to render sys.ts template")?;
let node_ts_file_contents = NodeTsTemplate::new(ci, options.sys_ts_main_file_name, options.out_import_extension.clone(), options.out_verbose_logs).render().context("failed to render node.ts template")?;
let index_ts_file_contents = IndexTsTemplate::new(options.node_ts_main_file_name, options.sys_ts_main_file_name, options.out_import_extension, options.out_lib_disable_auto_loading).render().context("failed to render index.ts template")?;

Ok(Bindings {
package_json_contents,
sys_ts_template_contents,
commonjs_shim_cts_template_contents,
node_ts_file_contents,
index_ts_file_contents,
})
Expand Down
8 changes: 0 additions & 8 deletions src/bindings/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,20 +73,17 @@ impl BindingGenerator for NodeBindingGenerator {
for uniffi_bindgen::Component { ci, config: _, .. } in components {
let sys_ts_main_file_name = format!("{}-sys", ci.namespace().to_kebab_case());
let node_ts_main_file_name = format!("{}-node", ci.namespace().to_kebab_case());
let commonjs_shim_cts_main_file_name = format!("{}-commonjs-shim", ci.namespace().to_kebab_case());

let Bindings {
package_json_contents,
sys_ts_template_contents,
commonjs_shim_cts_template_contents,
node_ts_file_contents,
index_ts_file_contents,
} = generate_node_bindings(
&ci,
GenerateNodeBindingsOptions {
sys_ts_main_file_name: sys_ts_main_file_name.as_str(),
node_ts_main_file_name: node_ts_main_file_name.as_str(),
commonjs_shim_cts_main_file_name: commonjs_shim_cts_main_file_name.as_str(),
out_dirname_api: self.out_dirname_api.clone(),
out_lib_disable_auto_loading: self.out_lib_disable_auto_loading,
out_import_extension: self.out_import_extension.clone(),
Expand All @@ -105,11 +102,6 @@ impl BindingGenerator for NodeBindingGenerator {
let sys_template_path = settings.out_dir.join(format!("{sys_ts_main_file_name}.ts"));
write_with_dirs(&sys_template_path, sys_ts_template_contents)?;

if !commonjs_shim_cts_template_contents.is_empty() {
let commonjs_shim_template_path = settings.out_dir.join(format!("{commonjs_shim_cts_main_file_name}.cts"));
write_with_dirs(&commonjs_shim_template_path, commonjs_shim_cts_template_contents)?;
}

let index_template_path = settings.out_dir.join("index.ts");
write_with_dirs(&index_template_path, index_ts_file_contents)?;
}
Expand Down
53 changes: 0 additions & 53 deletions templates/commonjs-shim.cts

This file was deleted.

53 changes: 46 additions & 7 deletions templates/sys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import { join, dirname } from "path";
{% if out_dirname_api == DirnameApi::ImportMetaUrl %}
import { fileURLToPath } from 'url';
{% endif %}
{% if let LibPath::Modules(_) = out_lib_path %}
import { createRequire } from "module";
{% endif %}
import {
DataType,
JsExternal,
Expand All @@ -23,9 +26,6 @@ import {
uniffiCreateFfiConverterString,
UniffiError,
} from 'uniffi-bindgen-react-native';
{% if let LibPath::Modules(_) = out_lib_path %}
import * as commonjsShim from './{{ commonjs_shim_cjs_main_file_name }}.cts';
{% endif %}


const CALL_SUCCESS = 0, CALL_ERROR = 1, CALL_UNEXPECTED_ERROR = 2, CALL_CANCELLED = 3;
Expand All @@ -47,9 +47,11 @@ function _uniffiLoad() {

{% match out_dirname_api -%}
{%- when DirnameApi::Dirname -%}
const filePath = __filename;
const libraryDirectory = __dirname;
{%- when DirnameApi::ImportMetaUrl -%}
const libraryDirectory = dirname(fileURLToPath(import.meta.url));
const filePath = fileURLToPath(import.meta.url);
const libraryDirectory = dirname(filePath);
{%- endmatch %}

// Get the path to the lib to load
Expand All @@ -64,9 +66,46 @@ function _uniffiLoad() {
const libraryPath = join(libraryDirectory, "{{ literal }}");
{%- endif -%}

{% when LibPath::Modules(_) %}
// @ts-ignore
const libraryPath = (commonjsShim.getLibPathModule() as { triple: string, path: string }).path;
{% when LibPath::Modules(mods) %}
let libPathModule;
let libPathModuleLastResolutionError: Error | null = null;
let libPathModuleLoadAttemptStack: Array<string> = [];

const commonjsRequire = createRequire(filePath);
{%- for switch_token in mods.as_switch_tokens() -%}
{% match switch_token -%}
{% when LibPathSwitchToken::Switch(value) -%}
switch ({{ value }}) {
{% when LibPathSwitchToken::Case(value) -%}
case "{{value}}":
{% when LibPathSwitchToken::EndCase -%}
break;
{% when LibPathSwitchToken::EndSwitch(_value) -%}
}
{% when LibPathSwitchToken::Value(value) -%}
if (!libPathModule) {
try {
libPathModule = commonjsRequire("{{ value }}");
} catch (e) {
libPathModuleLastResolutionError = e as Error;
libPathModuleLoadAttemptStack.push("{{ value }}");
}
}
{%- endmatch -%}
{%- endfor -%}

if (!libPathModule) {
const messageFragments = [
`Failed to load a native binding library!`,
`Attempted loading from the following modules in order: ${libPathModuleLoadAttemptStack.join(", ")}.`,
];
if (libPathModuleLastResolutionError) {
messageFragments.push(`The error message from the final load attempt is: ${libPathModuleLastResolutionError?.stack ?? libPathModuleLastResolutionError}`);
}
throw new Error(messageFragments.join('\n'));
}

const libraryPath = libPathModule.default().path;

{% endmatch %}

Expand Down