-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): allow defining global API script on plugin build (#9156)
* feat(core): allow defining global API script on plugin build Adds `tauri_plugin::Builder::global_api_script_path` so plugin authors can define the JavaScript global API bindings (supposed to be injected to `window.__TAURI__`) at compile time, so the string is only part of the binary when withGlobalTauri is true. Currently this needs to be done manually at runtime (and it's always added to the binary via include_str). * prefix variable * use list of scripts instead of combining them * static str * header [skip ci] * slice
- Loading branch information
1 parent
3f039c1
commit e227fe0
Showing
15 changed files
with
151 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
--- | ||
"tauri": patch:feat | ||
"tauri-codegen": patch:feat | ||
"tauri-build": patch:feat | ||
"tauri-plugin": patch:feat | ||
--- | ||
|
||
Allow plugins to define (at compile time) JavaScript that are initialized when `withGlobalTauri` is true. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"tauri-plugin": patch:feat | ||
--- | ||
|
||
Added `Builder::global_api_script_path` to define a JavaScript file containing the initialization script for the plugin API bindings when `withGlobalTauri` is used. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"tauri-utils": patch:feat | ||
--- | ||
|
||
Added the `plugin` module. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Copyright 2019-2024 Tauri Programme within The Commons Conservancy | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// SPDX-License-Identifier: MIT | ||
|
||
//! Compile-time and runtime types for Tauri plugins. | ||
#[cfg(feature = "build")] | ||
pub use build::*; | ||
|
||
#[cfg(feature = "build")] | ||
mod build { | ||
use std::{ | ||
env::vars_os, | ||
path::{Path, PathBuf}, | ||
}; | ||
|
||
const GLOBAL_API_SCRIPT_PATH_KEY: &str = "GLOBAL_API_SCRIPT_PATH"; | ||
/// Known file name of the file that contains an array with the path of all API scripts defined with [`define_global_api_script_path`]. | ||
pub const GLOBAL_API_SCRIPT_FILE_LIST_PATH: &str = "__global-api-script.js"; | ||
|
||
/// Defines the path to the global API script using Cargo instructions. | ||
pub fn define_global_api_script_path(path: PathBuf) { | ||
println!( | ||
"cargo:{GLOBAL_API_SCRIPT_PATH_KEY}={}", | ||
path | ||
.canonicalize() | ||
.expect("failed to canonicalize global API script path") | ||
.display() | ||
) | ||
} | ||
|
||
/// Collects the path of all the global API scripts defined with [`define_global_api_script_path`] | ||
/// and saves them to the out dir with filename [`GLOBAL_API_SCRIPT_FILE_LIST_PATH`]. | ||
pub fn load_global_api_scripts(out_dir: &Path) { | ||
let mut scripts = Vec::new(); | ||
|
||
for (key, value) in vars_os() { | ||
let key = key.to_string_lossy(); | ||
|
||
if key.starts_with("DEP_") && key.ends_with(GLOBAL_API_SCRIPT_PATH_KEY) { | ||
let script_path = PathBuf::from(value); | ||
scripts.push(script_path); | ||
} | ||
} | ||
|
||
std::fs::write( | ||
out_dir.join(GLOBAL_API_SCRIPT_FILE_LIST_PATH), | ||
serde_json::to_string(&scripts).expect("failed to serialize global API script paths"), | ||
) | ||
.expect("failed to write global API script"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// Copyright 2019-2024 Tauri Programme within The Commons Conservancy | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// SPDX-License-Identifier: MIT | ||
|
||
if ('__TAURI__' in window) { | ||
window.__TAURI__.sample = {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters