Kaynağa Gözat

修复打包路径问题

skyffire 1 yıl önce
ebeveyn
işleme
7f014c6b63
3 değiştirilmiş dosya ile 29 ekleme ve 18 silme
  1. 27 15
      example/electron.js
  2. 1 1
      example/obfuscate-main.js
  3. 1 2
      example/package.json

+ 27 - 15
example/electron.js

@@ -84,22 +84,34 @@ app.on('ready', () => {
     const relativePath = filePath.replace(path.normalize(process.resourcesPath), '').replace(/\\/g, '/').replace('//', '');
 
     if (memoryCache[relativePath]) {
-      const tempFilePath = path.join(app.getPath('temp'), relativePath);
-      fs.mkdirSync(path.dirname(tempFilePath), { recursive: true });
-      fs.writeFileSync(tempFilePath, memoryCache[relativePath]);
-
-      // console.log(`Intercepting request for: ${relativePath}, redirectURL: ${tempFilePath}`);
-
-      let mimeType = 'text/plain';
-      if (relativePath.endsWith('.js')) {
-        mimeType = 'application/javascript';
-      } else if (relativePath.endsWith('.css')) {
-        mimeType = 'text/css';
-      } else if (relativePath.endsWith('.html')) {
-        mimeType = 'text/html';
+      let sanitizedRelativePath = relativePath.replace('app.asar', 'app_asar'); // 避免路径中包含 app.asar
+      const tempFilePath = path.join(app.getPath('temp'), sanitizedRelativePath);
+      console.log(`Temp File Path: ${tempFilePath}`);
+
+      const tempDir = path.dirname(tempFilePath);
+      console.log(`Temp Directory Path: ${tempDir}`);
+
+      // 确保临时目录路径存在
+      if (!fs.existsSync(tempDir)) {
+        console.log(`Creating directory: ${tempDir}`);
+        fs.mkdirSync(tempDir, { recursive: true });
+        console.log(`Directory created: ${tempDir}`);
+      } else {
+        console.log(`Directory already exists: ${tempDir}`);
+      }
+
+      // 确保目录存在后再写入文件
+      if (fs.existsSync(tempDir)) {
+        console.log(`Writing file: ${tempFilePath}`);
+        fs.writeFileSync(tempFilePath, memoryCache[relativePath]);
+        console.log(`File written: ${tempFilePath}`);
+
+        console.log(`Intercepting request for: ${relativePath}, redirectURL: ${tempFilePath}`);
+        callback({ cancel: false, redirectURL: tempFilePath });
+      } else {
+        console.error(`Directory does not exist after creation attempt: ${tempDir}`);
+        callback({ cancel: false });
       }
-      // console.log(`Serving from memory: ${relativePath} as ${mimeType}`);
-      callback({ cancel: false, redirectURL: tempFilePath });
     } else {
       callback({ cancel: false });
     }

+ 1 - 1
example/obfuscate-main.js

@@ -18,7 +18,7 @@ const obfuscationResult = JavaScriptObfuscator.obfuscate(fileContent, {
   deadCodeInjectionThreshold: 0.4,
   debugProtection: true,
   debugProtectionInterval: 3000,
-  disableConsoleOutput: true,
+  disableConsoleOutput: false,
   identifierNamesGenerator: 'hexadecimal',
   renameGlobals: false,
   selfDefending: true,

+ 1 - 2
example/package.json

@@ -7,10 +7,9 @@
   "scripts": {
     "start-web": "node ../node_modules/react-scripts/bin/react-scripts.js start",
     "build-web": "node ../node_modules/react-scripts/bin/react-scripts.js build",
-    "total-build": "node ../node_modules/react-scripts/bin/react-scripts.js build && node encrypt.js && node obfuscate-main.js",
     "test": "node ../node_modules/react-scripts/bin/react-scripts.js test",
     "eject": "node ../node_modules/react-scripts/bin/react-scripts.js eject",
-    "package": "electron-builder",
+    "package": "node ../node_modules/react-scripts/bin/react-scripts.js build && node encrypt.js && node obfuscate-main.js && electron-builder",
     "start": "electron ."
   },
   "dependencies": {