Tauri does not get .env.production when running tauri build
Asked Answered
K

1

6

I've got a react app and implemented tauri to it so I can make it a desktop application.

I have two .env files: .env.development and .env.production. When i run tauri dev everything works correctly, but when I run tauri build is running without the enviroment variables in the production or the development env files.

I'm attaching a snap of my tauri.conf.json as I hope it helps.

{
  "$schema": "../node_modules/@tauri-apps/cli/schema.json",
  "build": {
    "beforeBuildCommand": "npm run build",
    "beforeDevCommand": "npm start",
    "devPath": "http://localhost:3000",
    "distDir": "../build"
  },
  "package": {
    "productName": "AppName",
    "version": "1.2.3"
  },
  "tauri": {
    "allowlist": {
      "all": true
    },
    "bundle": {
      "active": true,
      "category": "DeveloperTool",
      "copyright": "",
      "deb": {
        "depends": []
      },
      "externalBin": [],
      "icon": [
        "icons/32x32.png",
        "icons/128x128.png",
        "icons/[email protected]",
        "icons/icon.icns",
        "icons/icon.ico"
      ],
      "identifier": "com.myappname.app",
      "longDescription": "",
      "macOS": {
        "entitlements": null,
        "exceptionDomain": "",
        "frameworks": [],
        "providerShortName": null,
        "signingIdentity": null
      },
      "resources": [],
      "shortDescription": "",
      "targets": "all",
      "windows": {
        "certificateThumbprint": null,
        "digestAlgorithm": "sha256",
        "timestampUrl": ""
      }
    },
    "security": {
      "csp": null
    },
    "updater": {
      "active": false
    },
    "windows": [
      {
        "fullscreen": false,
        "height": 1080,
        "resizable": true,
        "title": "AppName",
        "width": 1920
      }
    ]
  }
}

Thanks in advance!

Kohn answered 5/10, 2022 at 12:36 Comment(0)
R
1

Here're few ways to get it in Rust:

1. Using std::env

#[tauri::command]
fn get_env(name: &str) -> String {
    std::env::var(String::from(name)).unwrap_or(String::from(""))
}

Then in typescript:

import { invoke } from "@tauri-apps/api/core";

function get_env(name: string) {
  return await invoke("get_env", { name });
}

2. Using Vite

Only with VITE_ prefixes:

import.meta.env.VITE_DEVELOPER_MINDSET

3. Using SvelteKit

In SSG mode only and only starting with PUBLIC_ prefixes.

import * as env from "$env/static/public";
console.log(import.meta.env.PUBLIC_DEVELOPER_MINDSET); // works

4. Pure Rust

Install crate: cargo add dotenv

Then in your rust code, load from .env using:

dotenv::dotenv().ok();

Or using macro:

#[macro_use]
extern crate dotenv_codegen;

And then in rust you can use dotenv macro:

println!("{}", dotenv!("DEVELOPER_MINDSET"));

There are couple more options on how to connect pulling and embedded environment variables inside backend and frontend of the Tauri app in my tutorial.

Rowdy answered 18/1 at 0:54 Comment(1)
Here's a youtube video about tauri environment variables – youtu.be/eFRl38t-D1URowdy

© 2022 - 2024 — McMap. All rights reserved.