Skip to content

Commit 8bdd894

Browse files
authored
refactor(core): move bundle script to /tauri crate (#1377)
* refactor(core): move bundle script to /tauri crate * fix(cli): clippy * fix(core): tests
1 parent 52c2baf commit 8bdd894

File tree

20 files changed

+23
-1094
lines changed

20 files changed

+23
-1094
lines changed

.changes/refactor-tauri-entry.md

-2
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,3 @@
33
---
44

55
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.

api/rollup.config.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ export default [
7070
output: [
7171
{
7272
name: '__TAURI__',
73-
dir: 'dist/', // if it needs to run in the browser
74-
entryFileNames: 'tauri.bundle.umd.js',
73+
dir: '../tauri/scripts',
74+
entryFileNames: 'bundle.js',
7575
format: 'umd',
7676
plugins: [
7777
getBabelOutputPlugin({

cli/core/build.rs

+1-19
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,13 @@
11
use std::{
2-
env,
32
env::current_dir,
43
error::Error,
5-
fs::{read_to_string, File},
4+
fs::File,
65
io::{BufWriter, Write},
7-
path::Path,
86
};
97

108
mod config_definition;
119

1210
pub fn main() -> Result<(), Box<dyn Error>> {
13-
let out_dir = env::var("OUT_DIR")?;
14-
15-
let dest_bundle_umd_path = Path::new(&out_dir).join("tauri.bundle.umd.js");
16-
let mut bundle_umd_file = BufWriter::new(File::create(&dest_bundle_umd_path)?);
17-
18-
let bundle_umd_path = current_dir()?.join("../../api/dist/tauri.bundle.umd.js");
19-
println!("cargo:rerun-if-changed={:?}", bundle_umd_path);
20-
if let Ok(bundle_umd_js) = read_to_string(bundle_umd_path) {
21-
write!(bundle_umd_file, "{}", bundle_umd_js)?;
22-
} else {
23-
write!(
24-
bundle_umd_file,
25-
r#"throw new Error("you are trying to use the global Tauri script but the @tauri-apps/api package wasn't compiled; run `yarn build` first")"#
26-
)?;
27-
}
28-
2911
let schema = schemars::schema_for!(config_definition::Config);
3012
let schema_file_path = current_dir()?.join("schema.json");
3113
let mut schema_file = BufWriter::new(File::create(&schema_file_path)?);

cli/core/src/build.rs

+2-15
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,10 @@ use crate::helpers::{
55
config::get as get_config,
66
execute_with_output,
77
manifest::rewrite_manifest,
8-
Logger, TauriScript,
8+
Logger,
99
};
1010

11-
use std::{
12-
env::set_current_dir,
13-
fs::{rename, File},
14-
io::Write,
15-
path::PathBuf,
16-
process::Command,
17-
};
11+
use std::{env::set_current_dir, fs::rename, path::PathBuf, process::Command};
1812

1913
mod rust;
2014

@@ -63,20 +57,13 @@ impl Build {
6357
let config_guard = config.lock().unwrap();
6458
let config_ = config_guard.as_ref().unwrap();
6559

66-
// __tauri.js
67-
let tauri_script = TauriScript::new()
68-
.global_tauri(config_.build.with_global_tauri)
69-
.get();
7060
let web_asset_path = PathBuf::from(&config_.build.dist_dir);
7161
if !web_asset_path.exists() {
7262
return Err(anyhow::anyhow!(
7363
"Unable to find your web assets, did you forget to build your web app? Your distDir is set to \"{:?}\".",
7464
web_asset_path
7565
));
7666
}
77-
let tauri_script_path = web_asset_path.join("__tauri.js");
78-
let mut tauri_script_file = File::create(tauri_script_path)?;
79-
tauri_script_file.write_all(tauri_script.as_bytes())?;
8067

8168
if let Some(before_build) = &config_.build.before_build_command {
8269
let mut cmd: Option<&str> = None;

cli/core/src/dev.rs

+1-18
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},
44
manifest::rewrite_manifest,
5-
Logger, TauriScript,
5+
Logger,
66
};
77

88
use notify::{watcher, DebouncedEvent, RecursiveMode, Watcher};
@@ -12,9 +12,6 @@ use shared_child::SharedChild;
1212
use std::{
1313
env::set_current_dir,
1414
ffi::OsStr,
15-
fs::{create_dir_all, File},
16-
io::Write,
17-
path::PathBuf,
1815
process::{exit, Child, Command},
1916
sync::{
2017
mpsc::{channel, Receiver},
@@ -102,20 +99,6 @@ impl Dev {
10299

103100
rewrite_manifest(config.clone())?;
104101

105-
// __tauri.js
106-
{
107-
let config_guard = config.lock().unwrap();
108-
let config_ = config_guard.as_ref().unwrap();
109-
let tauri_script = TauriScript::new()
110-
.global_tauri(config_.build.with_global_tauri)
111-
.get();
112-
let tauri_dir_path = PathBuf::from(&config_.build.dist_dir);
113-
let tauri_script_path = tauri_dir_path.join("__tauri.js");
114-
create_dir_all(tauri_dir_path)?;
115-
let mut tauri_script_file = File::create(tauri_script_path)?;
116-
tauri_script_file.write_all(tauri_script.as_bytes())?;
117-
}
118-
119102
let (child_wait_tx, child_wait_rx) = channel();
120103
let child_wait_rx = Arc::new(Mutex::new(child_wait_rx));
121104

cli/core/src/helpers/mod.rs

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

76
pub use logger::Logger;
8-
pub use tauri_entry::TauriScript;
97

108
use std::{
119
io::{BufRead, BufReader},

cli/core/src/helpers/tauri_entry.rs

-29
This file was deleted.

cli/tauri.js/test/jest/fixtures/app/.gitignore

-1
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
src-tauri
2-
dist/__tauri.js

core/tauri-codegen/src/context.rs

-7
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ pub fn context_codegen(data: ContextData) -> Result<TokenStream, EmbeddedAssetsE
3131
quote!(None)
3232
};
3333

34-
let tauri_script_path = dist_dir.join("__tauri.js").display().to_string();
35-
3634
// double braces are purposeful to force the code into a block expression
3735
Ok(quote! {{
3836
use ::tauri::api::private::{OnceCell, AsTauriContext};
@@ -53,11 +51,6 @@ pub fn context_codegen(data: ContextData) -> Result<TokenStream, EmbeddedAssetsE
5351
#assets
5452
}
5553

56-
/// Make the __tauri.js a dependency for the compiler
57-
fn raw_tauri_script() -> &'static str {
58-
include_str!(#tauri_script_path)
59-
}
60-
6154
/// Default window icon to set automatically if exists
6255
fn default_window_icon() -> Option<&'static [u8]> {
6356
#default_window_icon

examples/api/.gitignore

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
/node_modules/
22
/public/build/
3-
/public/__tauri.js
43

54
.DS_Store

0 commit comments

Comments
 (0)