diff --git a/.changes/os-plugin-refactor.md b/.changes/os-plugin-refactor.md index 63cd5bddd..ff0f73840 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/.eslintignore b/.eslintignore index efef8f77c..329feacba 100644 --- a/.eslintignore +++ b/.eslintignore @@ -2,4 +2,5 @@ target node_modules dist dist-js -api-iife.js \ No newline at end of file +api-iife.js +init.js \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock index d5c3a5404..4664da113 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5298,6 +5298,7 @@ dependencies = [ "os_info", "serde", "serde_json", + "serialize-to-javascript", "sys-locale", "tauri", "thiserror", diff --git a/plugins/os/Cargo.toml b/plugins/os/Cargo.toml index ef2f8a6d7..e22804105 100644 --- a/plugins/os/Cargo.toml +++ b/plugins/os/Cargo.toml @@ -15,3 +15,4 @@ thiserror = { workspace = true } os_info = "3" sys-locale = "0.3" gethostname = "0.4" +serialize-to-javascript = "=0.1.1" diff --git a/plugins/os/guest-js/index.ts b/plugins/os/guest-js/index.ts index 2b94895df..5c85b579a 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/api-iife.js b/plugins/os/src/api-iife.js index 382e6d153..98ed20a75 100644 --- a/plugins/os/src/api-iife.js +++ b/plugins/os/src/api-iife.js @@ -1 +1 @@ -if("__TAURI__"in window){var __TAURI_OS__=function(n){"use strict";const _=navigator.appVersion.includes("Win")?"\r\n":"\n";return n.EOL=_,n.arch=async function(){return window.__TAURI_INVOKE__("plugin:os|arch")},n.exeExtension=async function(){return window.__TAURI_INVOKE__("plugin:os|exe_extension")},n.family=async function(){return window.__TAURI_INVOKE__("plugin:os|family")},n.hostname=async function(){return window.__TAURI_INVOKE__("plugin:os|hostname")},n.locale=async function(){return window.__TAURI_INVOKE__("plugin:os|locale")},n.platform=async function(){return window.__TAURI_INVOKE__("plugin:os|platform")},n.type=async function(){return window.__TAURI_INVOKE__("plugin:os|os_type")},n.version=async function(){return window.__TAURI_INVOKE__("plugin:os|version")},n}({});Object.defineProperty(window.__TAURI__,"os",{value:__TAURI_OS__})} +if("__TAURI__"in window){var __TAURI_OS__=function(n){"use strict";return n.arch=async function(){return window.__TAURI_INVOKE__("plugin:os|arch")},n.eol=function(){return window.__TAURI__.os.__eol},n.exeExtension=async function(){return window.__TAURI_INVOKE__("plugin:os|exe_extension")},n.family=async function(){return window.__TAURI_INVOKE__("plugin:os|family")},n.hostname=async function(){return window.__TAURI_INVOKE__("plugin:os|hostname")},n.locale=async function(){return window.__TAURI_INVOKE__("plugin:os|locale")},n.platform=async function(){return window.__TAURI_INVOKE__("plugin:os|platform")},n.type=async function(){return window.__TAURI_INVOKE__("plugin:os|os_type")},n.version=async function(){return window.__TAURI_INVOKE__("plugin:os|version")},n}({});Object.defineProperty(window.__TAURI__,"os",{value:__TAURI_OS__})} diff --git a/plugins/os/src/init.js b/plugins/os/src/init.js new file mode 100644 index 000000000..33e42748c --- /dev/null +++ b/plugins/os/src/init.js @@ -0,0 +1,8 @@ +// Copyright 2019-2023 Tauri Programme within The Commons Conservancy +// SPDX-License-Identifier: Apache-2.0 +// SPDX-License-Identifier: MIT + +__RAW_global_os_api__; + +// eslint-disable-next-line +window.__TAURI__.os.__eol = __TEMPLATE_eol__; diff --git a/plugins/os/src/lib.rs b/plugins/os/src/lib.rs index ccbbdfef4..c66580d12 100644 --- a/plugins/os/src/lib.rs +++ b/plugins/os/src/lib.rs @@ -5,6 +5,7 @@ use std::fmt::Display; pub use os_info::Version; +use serialize_to_javascript::{default_template, DefaultTemplate, Template}; use tauri::{ plugin::{Builder, TauriPlugin}, Runtime, @@ -90,9 +91,28 @@ pub fn hostname() -> String { gethostname::gethostname().to_string_lossy().to_string() } +#[derive(Template)] +#[default_template("./init.js")] +struct InitJavascript { + #[raw] + global_os_api: &'static str, + eol: &'static str, +} + pub fn init() -> TauriPlugin { + let init_js = InitJavascript { + global_os_api: include_str!("api-iife.js"), + #[cfg(windows)] + eol: "\r\n", + #[cfg(not(windows))] + eol: "\n", + } + .render_default(&Default::default()) + // this will never fail with the above global_os_api eol values + .unwrap(); + Builder::new("os") - .js_init_script(include_str!("api-iife.js").to_string()) + .js_init_script(init_js.to_string()) .invoke_handler(tauri::generate_handler![ commands::platform, commands::version,