Explorar o código

dev和pro双模支持了。

skyffire hai 1 ano
pai
achega
e6d5b0a7ff
Modificáronse 3 ficheiros con 35 adicións e 6 borrados
  1. 14 5
      example/main.js
  2. 19 0
      example/package-lock.json
  3. 2 1
      example/package.json

+ 14 - 5
example/main.js

@@ -2,6 +2,7 @@ const { app, BrowserWindow, Menu, session } = require('electron');
 const path = require('path');
 const fs = require('fs');
 const crypto = require('crypto');
+const isDev = process.env.NODE_ENV === 'development';
 
 const algorithm = 'aes-256-ctr';
 const password = 'skyfffire-password';
@@ -15,7 +16,8 @@ function decryptFile(filePath) {
   const encrypted = fileContent.slice(16); // Extract encrypted data
   const decipher = crypto.createDecipheriv(algorithm, key, iv);
   const decrypted = Buffer.concat([decipher.update(encrypted), decipher.final()]);
-  const relativePath = path.relative(process.resourcesPath, filePath).replace(/\\/g, '/');
+  let relativePath = path.relative(process.resourcesPath, filePath).replace(/\\/g, '/');
+  if (isDev) relativePath = relativePath.replace('../../../..', '')
   memoryCache[relativePath] = decrypted;
 }
 
@@ -33,7 +35,7 @@ function decryptDirectory(directoryPath) {
 }
 
 function createWindow() {
-  const directoryToDecrypt = path.join(process.resourcesPath, 'app.asar', 'static');
+  const directoryToDecrypt = isDev ? path.join(__dirname, 'build', 'static') : path.join(process.resourcesPath, 'app.asar', 'static');
   decryptDirectory(directoryToDecrypt);
 
   const win = new BrowserWindow({
@@ -47,10 +49,11 @@ function createWindow() {
   });
 
   // Load the index.html from disk
-  win.loadFile('index.html');
+  const index = isDev ? path.join(__dirname, 'build', 'index.html') : 'index.html';
+  win.loadFile(index); // 确保路径正确
 
   // 打开调试工具
-  // win.webContents.openDevTools();
+  if (isDev) win.webContents.openDevTools();
 
   // 创建菜单模板,只包含一个刷新按钮
   const menuTemplate = [
@@ -81,10 +84,16 @@ app.on('ready', () => {
   session.defaultSession.webRequest.onBeforeRequest((details, callback) => {
     const url = new URL(details.url);
     const filePath = path.normalize(decodeURIComponent(url.pathname));
-    const relativePath = filePath.replace(path.normalize(process.resourcesPath), '').replace(/\\/g, '/').replace('//', '');
+    let relativePath = filePath.replace(path.normalize(process.resourcesPath), '').replace(/\\/g, '/').replace('//', '');
+
+    if (isDev && relativePath.indexOf('react-stock-heatmap/example') !== -1) {
+      relativePath = relativePath.split('react-stock-heatmap/example')[1]
+    }
+    console.log(relativePath)
 
     if (memoryCache[relativePath]) {
       let sanitizedRelativePath = relativePath.replace('app.asar', 'app_asar'); // 避免路径中包含 app.asar
+      console.log(app.getPath('temp'), sanitizedRelativePath)
       const tempFilePath = path.join(app.getPath('temp'), sanitizedRelativePath);
       console.log(`Temp File Path: ${tempFilePath}`);
 

+ 19 - 0
example/package-lock.json

@@ -19,6 +19,7 @@
       },
       "devDependencies": {
         "@babel/plugin-syntax-object-rest-spread": "^7.8.3",
+        "cross-env": "^7.0.3",
         "electron": "^30.0.9",
         "electron-builder": "^24.13.3",
         "javascript-obfuscator": "^4.1.1",
@@ -2207,6 +2208,24 @@
         "node": ">= 10"
       }
     },
+    "node_modules/cross-env": {
+      "version": "7.0.3",
+      "resolved": "https://registry.npmmirror.com/cross-env/-/cross-env-7.0.3.tgz",
+      "integrity": "sha512-+/HKd6EgcQCJGh2PSjZuUitQBQynKor4wrFbRg4DtAgS1aWO+gU52xpH7M9ScGgXSYmAVS9bIJ8EzuaGw0oNAw==",
+      "dev": true,
+      "dependencies": {
+        "cross-spawn": "^7.0.1"
+      },
+      "bin": {
+        "cross-env": "src/bin/cross-env.js",
+        "cross-env-shell": "src/bin/cross-env-shell.js"
+      },
+      "engines": {
+        "node": ">=10.14",
+        "npm": ">=6",
+        "yarn": ">=1"
+      }
+    },
     "node_modules/cross-spawn": {
       "version": "7.0.3",
       "resolved": "https://registry.npmmirror.com/cross-spawn/-/cross-spawn-7.0.3.tgz",

+ 2 - 1
example/package.json

@@ -10,7 +10,7 @@
     "test": "node ../node_modules/react-scripts/bin/react-scripts.js test",
     "eject": "node ../node_modules/react-scripts/bin/react-scripts.js eject",
     "package": "node ../node_modules/react-scripts/bin/react-scripts.js build && node encrypt.js && node obfuscate-main.js && electron-builder",
-    "start": "electron ."
+    "start": "cross-env NODE_ENV=development electron ."
   },
   "dependencies": {
     "@rongmz/react-stock-heatmap": "file:..",
@@ -24,6 +24,7 @@
   },
   "devDependencies": {
     "@babel/plugin-syntax-object-rest-spread": "^7.8.3",
+    "cross-env": "^7.0.3",
     "electron": "^30.0.9",
     "electron-builder": "^24.13.3",
     "javascript-obfuscator": "^4.1.1",