diff --git a/.changes/os-plugin-refactor.md b/.changes/os-plugin-refactor.md index 63cd5bdd..ff0f7384 100644 --- a/.changes/os-plugin-refactor.md +++ b/.changes/os-plugin-refactor.md @@ -9,3 +9,4 @@ The os plugin is recieving a few changes to improve consistency and add new feat - Added `family()`,`exe_extension()`, and `hostname()` functions and their equivalents for JS. - Removed `tempdir()` function and its equivalent on JS, use `std::env::temp_dir` instead of `temp_dir` from `tauri::path::PathResolver::temp_dir` and `path.tempDir` on JS. - Modified `platform()` implementation to return `windows` instead of `win32` and `macos` instead of `darwin` to align with Rust's `std::env::consts::OS` +- `EOL` const in JS has been modified into a function `eol()` fix import issues in frameworks like `next.js` diff --git a/plugins/os/guest-js/index.ts b/plugins/os/guest-js/index.ts index 2b94895d..7104c03a 100644 --- a/plugins/os/guest-js/index.ts +++ b/plugins/os/guest-js/index.ts @@ -11,6 +11,9 @@ declare global { interface Window { __TAURI_INVOKE__: (cmd: string, args?: unknown) => Promise; + __TAURI_OS__: { + EOL: string; + }; } } @@ -41,18 +44,16 @@ type Arch = | "s390x" | "sparc64"; -function isWindows(): boolean { - return navigator.appVersion.includes("Win"); -} - /** - * The operating system-specific end-of-line marker. + * Returns the operating system-specific end-of-line marker. * - `\n` on POSIX * - `\r\n` on Windows * * @since 2.0.0 * */ -const EOL = isWindows() ? "\r\n" : "\n"; +function eol() { + return window.__TAURI_OS__.EOL; +} /** * Returns a string describing the specific operating system in use. @@ -174,7 +175,7 @@ async function hostname(): Promise { } export { - EOL, + eol, platform, family, version, diff --git a/plugins/os/src/lib.rs b/plugins/os/src/lib.rs index ccbbdfef..efebb832 100644 --- a/plugins/os/src/lib.rs +++ b/plugins/os/src/lib.rs @@ -91,8 +91,16 @@ pub fn hostname() -> String { } pub fn init() -> TauriPlugin { + let mut init_script = String::new(); + init_script.push_str(include_str!("api-iife.js")); + #[cfg(windows)] + let eol = "\r\n"; + #[cfg(not(windows))] + let eol = "\n"; + init_script.push_str(&format!("window.__TAURI_OS__.EOL = {eol}")); + Builder::new("os") - .js_init_script(include_str!("api-iife.js").to_string()) + .js_init_script(init_script) .invoke_handler(tauri::generate_handler![ commands::platform, commands::version,