Prechádzať zdrojové kódy

fix(cli): invalid plugin template (#8161)

Lucas Fernandes Nogueira 1 rok pred
rodič
commit
bfbbefdb9e

+ 6 - 0
.changes/fix-plugin-template.md

@@ -0,0 +1,6 @@
+---
+"@tauri-apps/cli": patch:bug
+"tauri-cli": patch:bug
+---
+
+Fix invalid plugin template.

+ 26 - 29
examples/api/vite.config.js

@@ -5,38 +5,35 @@
 import { defineConfig } from 'vite'
 import Unocss from 'unocss/vite'
 import { svelte } from '@sveltejs/vite-plugin-svelte'
-import { internalIpV4 } from 'internal-ip'
+import { internalIpV4Sync } from 'internal-ip'
+
+const mobile = !!/android|ios/.exec(process.env.TAURI_ENV_PLATFORM)
 
 // https://vitejs.dev/config/
-export default defineConfig(async ({ command, mode }) => {
-  const host =
-    process.env.TAURI_ENV_PLATFORM === 'android' ||
-    process.env.TAURI_ENV_PLATFORM === 'ios'
-      ? await internalIpV4()
-      : 'localhost'
-  return {
-    plugins: [Unocss(), svelte()],
-    build: {
-      rollupOptions: {
-        output: {
-          entryFileNames: `assets/[name].js`,
-          chunkFileNames: `assets/[name].js`,
-          assetFileNames: `assets/[name].[ext]`
-        }
-      }
-    },
-    server: {
-      host: '0.0.0.0',
-      port: 5173,
-      strictPort: true,
-      hmr: {
-        protocol: 'ws',
-        host,
-        port: 5183
-      },
-      fs: {
-        allow: ['.', '../../tooling/api/dist']
+export default defineConfig({
+  plugins: [Unocss(), svelte()],
+  build: {
+    rollupOptions: {
+      output: {
+        entryFileNames: `assets/[name].js`,
+        chunkFileNames: `assets/[name].js`,
+        assetFileNames: `assets/[name].[ext]`
       }
     }
+  },
+  server: {
+    host: mobile ? '0.0.0.0' : false,
+    port: 5173,
+    strictPort: true,
+    hmr: mobile
+      ? {
+          protocol: 'ws',
+          host: internalIpV4Sync(),
+          port: 5183
+        }
+      : undefined,
+    fs: {
+      allow: ['.', '../../tooling/api/dist']
+    }
   }
 })

+ 1 - 0
tooling/cli/src/plugin/android.rs

@@ -68,6 +68,7 @@ pub fn command(cli: Cli) -> Result<()> {
 
       let mut data = BTreeMap::new();
       super::init::plugin_name_data(&mut data, &plugin_name);
+      data.insert("android_package_id", handlebars::to_json(&plugin_id));
 
       let mut created_dirs = Vec::new();
       template::render_with_generator(

+ 4 - 3
tooling/cli/templates/plugin/__example-api/tauri-app/package.json

@@ -10,13 +10,14 @@
     "tauri": "tauri"
   },
   "dependencies": {
-    "@tauri-apps/api": "^1.1.0",
+    "@tauri-apps/api": "^2.0.0-alpha.11",
     "tauri-plugin-{{ plugin_name }}-api": "link:../../"
   },
   "devDependencies": {
     "@sveltejs/vite-plugin-svelte": "^1.0.1",
+    "internal-ip": "^7.0.0",
     "svelte": "^3.49.0",
     "vite": "^3.0.2",
-    "@tauri-apps/cli": "^1.1.0"
+    "@tauri-apps/cli": "^2.0.0-alpha.17"
   }
-}
+}

+ 4 - 0
tooling/cli/templates/plugin/__example-api/tauri-app/src-tauri/Cargo.crate-manifest

@@ -8,6 +8,10 @@ repository = ""
 edition = "2021"
 rust-version = "1.70"
 
+[lib]
+name = "tauri_app_lib"
+crate-type = ["staticlib", "cdylib", "rlib"]
+
 # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
 
 [build-dependencies]

+ 14 - 0
tooling/cli/templates/plugin/__example-api/tauri-app/src-tauri/src/lib.rs

@@ -0,0 +1,14 @@
+// Learn more about Tauri commands at https://tauri.app/v1/guides/features/command
+#[tauri::command]
+fn greet(name: &str) -> String {
+    format!("Hello, {}! You've been greeted from Rust!", name)
+}
+
+#[cfg_attr(mobile, tauri::mobile_entry_point)]
+pub fn run() {
+    tauri::Builder::default()
+        .invoke_handler(tauri::generate_handler![greet])
+        .plugin(tauri_plugin_{{ plugin_name_snake_case }}::init())
+        .run(tauri::generate_context!())
+        .expect("error while running tauri application");
+}

+ 1 - 11
tooling/cli/templates/plugin/__example-api/tauri-app/src-tauri/src/main.rs

@@ -1,16 +1,6 @@
 // Prevents additional console window on Windows in release, DO NOT REMOVE!!
 #![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
 
-// Learn more about Tauri commands at https://tauri.app/v1/guides/features/command
-#[tauri::command]
-fn greet(name: &str) -> String {
-    format!("Hello, {}! You've been greeted from Rust!", name)
-}
-
 fn main() {
-    tauri::Builder::default()
-        .invoke_handler(tauri::generate_handler![greet])
-        .plugin(tauri_plugin_{{ plugin_name_snake_case }}::init())
-        .run(tauri::generate_context!())
-        .expect("error while running tauri application");
+  tauri_app_lib::run();
 }

+ 10 - 12
tooling/cli/templates/plugin/__example-api/tauri-app/vite.config.js

@@ -1,5 +1,8 @@
 import { defineConfig } from "vite";
 import { svelte } from "@sveltejs/vite-plugin-svelte";
+import { internalIpV4Sync } from 'internal-ip'
+
+const mobile = !!/android|ios/.exec(process.env.TAURI_ENV_PLATFORM);
 
 // https://vitejs.dev/config/
 export default defineConfig({
@@ -10,18 +13,13 @@ export default defineConfig({
   clearScreen: false,
   // tauri expects a fixed port, fail if that port is not available
   server: {
+    host: mobile ? "0.0.0.0" : false,
     port: 1420,
     strictPort: true,
+    hmr: mobile ? {
+      protocol: 'ws',
+      host: internalIpV4Sync(),
+      port: 1421
+    } : undefined,
   },
-  // to make use of `TAURI_ENV_DEBUG` and other env variables
-  // https://tauri.studio/v1/api/config#buildconfig.beforedevcommand
-  envPrefix: ["VITE_", "TAURI_ENV_"],
-  build: {
-    // Tauri supports es2021
-    target: ["es2021", "chrome100", "safari13"],
-    // don't minify for debug builds
-    minify: !process.env.TAURI_ENV_DEBUG ? "esbuild" : false,
-    // produce sourcemaps for debug builds
-    sourcemap: !!process.env.TAURI_ENV_DEBUG,
-  },
-});
+})

+ 1 - 1
tooling/cli/templates/plugin/__example-basic/vanilla/package.json

@@ -9,6 +9,6 @@
   "author": "",
   "license": "MIT",
   "dependencies": {
-    "@tauri-apps/cli": "^1.0.0"
+    "@tauri-apps/cli": "^2.0.0-alpha.17"
   }
 }

+ 4 - 0
tooling/cli/templates/plugin/__example-basic/vanilla/src-tauri/Cargo.crate-manifest

@@ -8,6 +8,10 @@ repository = ""
 edition = "2021"
 rust-version = "1.70"
 
+[lib]
+name = "tauri_app_lib"
+crate-type = ["staticlib", "cdylib", "rlib"]
+
 # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
 
 [build-dependencies]

+ 7 - 0
tooling/cli/templates/plugin/__example-basic/vanilla/src-tauri/src/lib.rs

@@ -0,0 +1,7 @@
+#[cfg_attr(mobile, tauri::mobile_entry_point)]
+pub fn run() {
+  tauri::Builder::default()
+    .plugin(tauri_plugin_{{ plugin_name_snake_case }}::init())
+    .run(tauri::generate_context!())
+    .expect("error while running tauri application");
+}

+ 1 - 4
tooling/cli/templates/plugin/__example-basic/vanilla/src-tauri/src/main.rs

@@ -2,8 +2,5 @@
 #![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
 
 fn main() {
-  tauri::Builder::default()
-    .plugin(tauri_plugin_{{ plugin_name_snake_case }}::init())
-    .run(tauri::generate_context!())
-    .expect("error while running tauri application");
+  tauri_app_lib::run();
 }

+ 1 - 1
tooling/cli/templates/plugin/package.json

@@ -19,7 +19,7 @@
     "typescript": "4.7.3"
   },
   "dependencies": {
-    "@tauri-apps/api": "^1.0.0",
+    "@tauri-apps/api": "^2.0.0-alpha.11",
     "tslib": "^2.1.0"
   }
 }

+ 1 - 1
tooling/cli/templates/plugin/webview-src/index.ts

@@ -1,4 +1,4 @@
-{#if license_header}}
+{{#if license_header}}
 {{ license_header }}
 {{/if}}
 import { invoke } from '@tauri-apps/api/primitives'