瀏覽代碼

Fix importing `@tauri-apps/api` in Node.js (#3752)

Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
Kasper 3 年之前
父節點
當前提交
d36c8e0322

+ 5 - 0
.changes/fix-nodejs-api-import.md

@@ -0,0 +1,5 @@
+---
+"api": patch
+---
+
+Fix importing `@tauri-apps/api` in Node.js

File diff suppressed because it is too large
+ 0 - 0
core/tauri/scripts/bundle.js


+ 35 - 0
tooling/api/src/helpers/env-check.ts

@@ -0,0 +1,35 @@
+// Copyright 2019-2021 Tauri Programme within The Commons Conservancy
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-License-Identifier: MIT
+
+/** @ignore */
+
+function isBrowser(): boolean {
+  return typeof window !== 'undefined'
+}
+
+function isLinux(): boolean {
+  if (isBrowser()) {
+    return navigator.appVersion.includes('Linux')
+  } else {
+    return process.platform === 'linux'
+  }
+}
+
+function isWindows(): boolean {
+  if (isBrowser()) {
+    return navigator.appVersion.includes('Win')
+  } else {
+    return process.platform === 'win32'
+  }
+}
+
+function isMacOS(): boolean {
+  if (isBrowser()) {
+    return navigator.appVersion.includes('Mac')
+  } else {
+    return process.platform === 'darwin'
+  }
+}
+
+export { isLinux, isWindows, isMacOS, isBrowser }

+ 0 - 19
tooling/api/src/helpers/os-check.ts

@@ -1,19 +0,0 @@
-// Copyright 2019-2021 Tauri Programme within The Commons Conservancy
-// SPDX-License-Identifier: Apache-2.0
-// SPDX-License-Identifier: MIT
-
-/** @ignore */
-
-function isLinux(): boolean {
-  return navigator.appVersion.includes('Linux')
-}
-
-function isWindows(): boolean {
-  return navigator.appVersion.includes('Win')
-}
-
-function isMacOS(): boolean {
-  return navigator.appVersion.includes('Mac')
-}
-
-export { isLinux, isWindows, isMacOS }

+ 1 - 1
tooling/api/src/os.ts

@@ -24,7 +24,7 @@
  */
 
 import { LiteralUnion } from 'type-fest'
-import { isWindows } from './helpers/os-check'
+import { isWindows } from './helpers/env-check'
 import { invokeTauriCommand } from './helpers/tauri'
 
 /**

+ 1 - 1
tooling/api/src/path.ts

@@ -25,7 +25,7 @@
 
 import { invokeTauriCommand } from './helpers/tauri'
 import { BaseDirectory } from './fs'
-import { isWindows } from './helpers/os-check'
+import { isWindows } from './helpers/env-check'
 
 /**
  * Returns the path to the suggested directory for your app config files.

+ 2 - 1
tooling/api/src/window.ts

@@ -108,6 +108,7 @@
 import { invokeTauriCommand } from './helpers/tauri'
 import type { EventName, EventCallback, UnlistenFn } from './event'
 import { emit, listen, once } from './helpers/event'
+import { isBrowser } from './helpers/env-check'
 
 /** Allows you to retrieve information about a given monitor. */
 interface Monitor {
@@ -1169,7 +1170,7 @@ class WebviewWindow extends WindowManager {
 
 /** The WebviewWindow for the current window. */
 let appWindow
-if ('__TAURI_METADATA__' in window) {
+if (isBrowser() && '__TAURI_METADATA__' in window) {
   appWindow = new WebviewWindow(
     window.__TAURI_METADATA__.__currentWindow.label,
     {

Some files were not shown because too many files changed in this diff