Skip to content

Commit 4412b7c

Browse files
authored
refactor(tauri): inject script with webview init API (#1186)
1 parent b9ce7b9 commit 4412b7c

File tree

17 files changed

+547
-508
lines changed

17 files changed

+547
-508
lines changed

.changes/refactor-tauri-entry.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"tauri": minor
3+
---
4+
5+
The Tauri script is now injected with the webview `init` API, so it is available after page changes.
6+
It will no longer be injected on `${distDir}/index.tauri.html`, but we will add a `${distDir}/__tauri.js` file to read it at app compile time.
7+
You should add that to your `.gitignore` file.

cli/core/Cargo.lock

-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cli/core/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ tiny_http = "0.8"
2525
attohttpc = "0.16"
2626
url = "2.2"
2727
http = "0.2"
28-
handlebars = "3.5"
2928
notify = "4.0"
3029
shared_child = "0.3"
3130
toml_edit = "0.2"

cli/core/src/build.rs

+11-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::helpers::{
88
config::get as get_config,
99
execute_with_output,
1010
manifest::rewrite_manifest,
11-
TauriHtml,
11+
TauriHtml, TauriScript,
1212
};
1313
use std::env::{set_current_dir, set_var};
1414
use std::fs::read_to_string;
@@ -97,15 +97,23 @@ impl Build {
9797
let config_guard = config.lock().unwrap();
9898
let config_ = config_guard.as_ref().unwrap();
9999

100+
// index.tauri.html
100101
let index_html_path = PathBuf::from(&config_.build.dist_dir).join("index.html");
101102
let tauri_html = TauriHtml::new(&config_.build.dist_dir, read_to_string(index_html_path)?)
102103
.inliner_enabled(config_.tauri.inliner.active && !config_.tauri.embedded_server.active)
103-
.global_tauri(config_.build.with_global_tauri)
104-
.generate()?;
104+
.get()?;
105105
let tauri_index_html_path = PathBuf::from(&config_.build.dist_dir).join("index.tauri.html");
106106
let mut tauri_index_html_file = File::create(tauri_index_html_path)?;
107107
tauri_index_html_file.write_all(tauri_html.as_bytes())?;
108108

109+
// __tauri.js
110+
let tauri_script = TauriScript::new()
111+
.global_tauri(config_.build.with_global_tauri)
112+
.get();
113+
let tauri_script_path = PathBuf::from(&config_.build.dist_dir).join("__tauri.js");
114+
let mut tauri_script_file = File::create(tauri_script_path)?;
115+
tauri_script_file.write_all(tauri_script.as_bytes())?;
116+
109117
let settings = settings_builder.build()?;
110118

111119
if let Some(before_build) = &config_.build.before_build_command {

cli/core/src/dev.rs

+17-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::helpers::{
22
app_paths::{app_dir, tauri_dir},
33
config::{get as get_config, reload as reload_config, ConfigHandle},
44
manifest::rewrite_manifest,
5-
Logger, TauriHtml,
5+
Logger, TauriHtml, TauriScript,
66
};
77

88
use attohttpc::{Method, RequestBuilder};
@@ -14,6 +14,9 @@ use url::Url;
1414

1515
use std::env::set_var;
1616
use std::ffi::OsStr;
17+
use std::fs::File;
18+
use std::io::Write;
19+
use std::path::PathBuf;
1720
use std::process::{exit, Command};
1821
use std::sync::mpsc::{channel, Receiver};
1922
use std::sync::{Arc, Mutex};
@@ -138,6 +141,18 @@ impl Dev {
138141

139142
rewrite_manifest(config.clone())?;
140143

144+
// __tauri.js
145+
{
146+
let config_guard = config.lock().unwrap();
147+
let config_ = config_guard.as_ref().unwrap();
148+
let tauri_script = TauriScript::new()
149+
.global_tauri(config_.build.with_global_tauri)
150+
.get();
151+
let tauri_script_path = PathBuf::from(&config_.build.dist_dir).join("__tauri.js");
152+
let mut tauri_script_file = File::create(tauri_script_path)?;
153+
tauri_script_file.write_all(tauri_script.as_bytes())?;
154+
}
155+
141156
let (child_wait_tx, child_wait_rx) = channel();
142157
let child_wait_rx = Arc::new(Mutex::new(child_wait_rx));
143158

@@ -250,9 +265,7 @@ fn proxy_dev_server(config: ConfigHandle, dev_path: &Url, dev_port: u16) -> crat
250265
let config_guard = config.lock().unwrap();
251266
let config = config_guard.as_ref().unwrap();
252267
let response = request_builder.send()?.text()?;
253-
let tauri_html = TauriHtml::new(&config.build.dist_dir, response)
254-
.global_tauri(config.build.with_global_tauri)
255-
.generate()?;
268+
let tauri_html = TauriHtml::new(&config.build.dist_dir, response).get()?;
256269
request.respond(Response::from_data(tauri_html))?;
257270
} else {
258271
let response = request_builder.send()?.bytes()?;

cli/core/src/helpers/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ pub mod app_paths;
22
pub mod config;
33
mod logger;
44
pub mod manifest;
5-
mod tauri_html;
5+
mod tauri_entry;
66

77
pub use logger::Logger;
8-
pub use tauri_html::TauriHtml;
8+
pub use tauri_entry::{TauriHtml, TauriScript};
99

1010
use std::io::{BufRead, BufReader};
1111
use std::process::{Command, Stdio};

cli/core/src/helpers/tauri_entry.rs

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
use kuchiki::traits::*;
2+
use tauri_inliner::inline_html_string;
3+
4+
use std::path::PathBuf;
5+
6+
pub struct TauriHtml {
7+
original: String,
8+
html_dir: PathBuf,
9+
inliner_enabled: bool,
10+
}
11+
12+
impl TauriHtml {
13+
pub fn new(html_dir: impl Into<PathBuf>, html: String) -> Self {
14+
Self {
15+
original: html,
16+
html_dir: html_dir.into(),
17+
inliner_enabled: false,
18+
}
19+
}
20+
21+
pub fn inliner_enabled(mut self, enabled: bool) -> Self {
22+
self.inliner_enabled = enabled;
23+
self
24+
}
25+
26+
pub fn get(self) -> crate::Result<String> {
27+
let html = if self.inliner_enabled {
28+
inline_html_string(&self.original, self.html_dir, Default::default())?
29+
} else {
30+
self.original
31+
};
32+
33+
let new_html = kuchiki::parse_html().one(html).to_string();
34+
35+
Ok(new_html)
36+
}
37+
}
38+
39+
#[derive(Default)]
40+
pub struct TauriScript {
41+
global_tauri: bool,
42+
}
43+
44+
impl TauriScript {
45+
pub fn new() -> Self {
46+
Default::default()
47+
}
48+
49+
pub fn global_tauri(mut self, global_tauri: bool) -> Self {
50+
self.global_tauri = global_tauri;
51+
self
52+
}
53+
54+
pub fn get(self) -> String {
55+
let mut scripts = Vec::new();
56+
scripts.push(include_str!("../templates/tauri.js"));
57+
scripts.push(include_str!("../templates/mutation-observer.js"));
58+
59+
if self.global_tauri {
60+
scripts.insert(
61+
0,
62+
include_str!(concat!(env!("OUT_DIR"), "/tauri.bundle.umd.js")),
63+
);
64+
}
65+
66+
scripts.join("\n\n")
67+
}
68+
}

cli/core/src/helpers/tauri_html.rs

-107
This file was deleted.

cli/core/src/templates/mutation-observer.js

+19-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
(function () {
1+
function __tauri_mutation_observer (target) {
22
function loadAsset(path, type) {
33
if (path) {
44
window.__TAURI__.loadAsset(path, type)
@@ -23,14 +23,25 @@
2323
})
2424
})
2525

26-
{{#if (eq target "body")}}
27-
var target = document.documentElement
28-
{{ else }}
29-
var target = document.head
30-
{{/if}}
31-
3226
observer.observe(target, {
3327
childList: true,
3428
subtree: true
3529
})
36-
})()
30+
}
31+
32+
__tauri_mutation_observer(document.documentElement)
33+
if (
34+
document.readyState === 'complete' ||
35+
document.readyState === 'interactive'
36+
) {
37+
__tauri_mutation_observer(document.head)
38+
} else {
39+
window.addEventListener(
40+
'DOMContentLoaded',
41+
function () {
42+
__tauri_mutation_observer(document.head)
43+
},
44+
true
45+
)
46+
}
47+

cli/core/src/templates/tauri.js

+22-8
Original file line numberDiff line numberDiff line change
@@ -116,15 +116,29 @@ if (!String.prototype.startsWith) {
116116
delete window[callback]
117117
}, true)
118118

119-
window.__TAURI_INVOKE_HANDLER__(
120-
_objectSpread(
121-
{
122-
callback: callback,
123-
error: error
124-
},
125-
args
119+
if (window.__TAURI_INVOKE_HANDLER__) {
120+
window.__TAURI_INVOKE_HANDLER__(
121+
_objectSpread(
122+
{
123+
callback: callback,
124+
error: error
125+
},
126+
args
127+
)
126128
)
127-
)
129+
} else {
130+
window.addEventListener('DOMContentLoaded', function () {
131+
window.__TAURI_INVOKE_HANDLER__(
132+
_objectSpread(
133+
{
134+
callback: callback,
135+
error: error
136+
},
137+
args
138+
)
139+
)
140+
})
141+
}
128142
})
129143
}
130144

Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
dist/index.tauri.html
1+
dist/index.tauri.html
2+
dist/__tauri.js
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
src-tauri
22
dist/index.tauri.html
3+
dist/__tauri.js

0 commit comments

Comments
 (0)