Преглед изворни кода

feat(tauri) read config overriden by the node CLI (#258)

* feat(tauri) read config overriden by the node CLI

* chore(tauri) use option_env macro
Lucas Fernandes Nogueira пре 5 година
родитељ
комит
e17c5f84b1
4 измењених фајлова са 16 додато и 24 уклоњено
  1. 2 2
      cli/tauri.js/src/helpers/tauri-config.ts
  2. 6 18
      cli/tauri.js/src/runner.ts
  3. 2 2
      tauri/src/app/runner.rs
  4. 6 2
      tauri/src/config.rs

+ 2 - 2
cli/tauri.js/src/helpers/tauri-config.ts

@@ -41,8 +41,7 @@ module.exports = (cfg: Partial<TauriConfig>): TauriConfig => {
           title: pkg.productName
         },
         security: {
-          csp:
-            "default-src data: filesystem: ws: http: https: 'unsafe-eval' 'unsafe-inline'"
+          csp: "default-src blob: data: filesystem: ws: http: https: 'unsafe-eval' 'unsafe-inline'"
         },
         edge: {
           active: true
@@ -63,6 +62,7 @@ module.exports = (cfg: Partial<TauriConfig>): TauriConfig => {
 
   process.env.TAURI_DIST_DIR = appPaths.resolve.app(config.build.distDir)
   process.env.TAURI_DIR = appPaths.tauriDir
+  process.env.TAURI_CONFIG = JSON.stringify(config)
 
   return config
 }

+ 6 - 18
cli/tauri.js/src/runner.ts

@@ -1,7 +1,7 @@
 import Inliner from '@tauri-apps/tauri-inliner'
 import toml from '@tauri-apps/toml'
 import chokidar, { FSWatcher } from 'chokidar'
-import { existsSync, readFileSync, writeFileSync, mkdirSync } from 'fs-extra'
+import { existsSync, readFileSync, writeFileSync } from 'fs-extra'
 import { JSDOM } from 'jsdom'
 import { debounce } from 'lodash'
 import path from 'path'
@@ -82,13 +82,7 @@ class Runner {
           path.join(appDir, 'tauri.conf.js')
         ],
         {
-          // TODO: incorrect options?
-          // @ts-ignore
-          watchers: {
-            chokidar: {
-              ignoreInitial: true
-            }
-          }
+          ignoreInitial: true
         }
       )
       .on(
@@ -156,17 +150,16 @@ class Runner {
 
   async __parseHtml(cfg: TauriConfig, indexDir: string): Promise<string[]> {
     const inlinedAssets: string[] = []
-    const distDir = cfg.build.distDir
 
     return new Promise((resolve, reject) => {
-      const distIndexPath = path.join(indexDir, 'index.html')
-      if (!existsSync(distIndexPath)) {
+      const indexPath = path.join(indexDir, 'index.html')
+      if (!existsSync(indexPath)) {
         warn(
           `Error: cannot find index.html in "${indexDir}". Did you forget to build your web code or update the build.distDir in tauri.conf.json?`
         )
         reject(new Error('Could not find index.html in dist dir.'))
       }
-      new Inliner(distIndexPath, (err: Error, html: string) => {
+      new Inliner(indexPath, (err: Error, html: string) => {
         if (err) {
           reject(err)
         } else {
@@ -190,12 +183,8 @@ class Runner {
             document.head.appendChild(cspTag)
           }
 
-          if (!existsSync(distDir)) {
-            mkdirSync(distDir, { recursive: true })
-          }
-
           writeFileSync(
-            path.join(distDir, 'index.tauri.html'),
+            path.join(indexDir, 'index.tauri.html'),
             dom.serialize()
           )
           resolve(inlinedAssets)
@@ -277,7 +266,6 @@ class Runner {
 
   __manipulateToml(callback: (tomlContents: object) => void): void {
     const tomlPath = path.join(tauriDir, 'Cargo.toml')
-    // TODO: should this be read as buffer or string?
     const tomlFile = readFileSync(tomlPath)
     // @ts-ignore
     const tomlContents = toml.parse(tomlFile)

+ 2 - 2
tauri/src/app/runner.rs

@@ -8,9 +8,9 @@ pub(crate) fn run(application: &mut crate::App) {
     content = if config.build.dev_path.starts_with("http") {
       web_view::Content::Url(config.build.dev_path)
     } else {
-      let dev_path = std::path::Path::new(env!("TAURI_DIST_DIR")).join("index.tauri.html");
+      let dev_path = std::path::Path::new(&config.build.dev_path).join("index.tauri.html");
       web_view::Content::Html(
-        std::fs::read_to_string(dev_path).expect("failed to build index.tauri.html"),
+        std::fs::read_to_string(dev_path).expect("failed to read index.tauri.html"),
       )
     };
   }

+ 6 - 2
tauri/src/config.rs

@@ -105,6 +105,10 @@ fn default_build() -> BuildConfig {
 }
 
 pub fn get() -> Config {
-  serde_json::from_str(include_str!(concat!(env!("TAURI_DIR"), "/tauri.conf.json")))
-    .expect("failed to read tauri.conf.json")
+  match option_env!("TAURI_CONFIG") {
+    Some(config) => serde_json::from_str(config)
+      .expect("failed to parse TAURI_CONFIG env"),
+    None => serde_json::from_str(include_str!(concat!(env!("TAURI_DIR"), "/tauri.conf.json")))
+      .expect("failed to read tauri.conf.json")
+  }
 }