Ver código fonte

代码混淆功能重搞,不会再出现打包失败情况

刘洋 1 ano atrás
pai
commit
e818e8eefe
3 arquivos alterados com 155 adições e 71 exclusões
  1. 62 0
      obfuscator.js
  2. 5 6
      package.json
  3. 88 65
      vite.config.ts

+ 62 - 0
obfuscator.js

@@ -0,0 +1,62 @@
+const JavaScriptObfuscator = require("javascript-obfuscator");
+const fs = require("fs");
+
+// 配置
+const buildDir = "./dist/assets/";
+
+const getJsFileList = (dir) =>
+  new Promise((resolve, reject) => {
+    fs.readdir(dir, (err, files) => {
+      if (err) return reject(`待混淆文件目录不存在!`);
+      console.log("files:", files);
+      return resolve(
+        Promise.all(
+          files
+            .filter((fileName) => fileName.endsWith(".js"))
+            .map(
+              (fileName) =>
+                new Promise((resolveInner) => {
+                  fs.readFile(dir + fileName, (err, data) => {
+                    console.log("err:", err);
+                    return resolveInner({ fileName, content: data.toString() });
+                  });
+                })
+            )
+        )
+      );
+    });
+  });
+
+getJsFileList(buildDir).then((list) => {
+  console.log(
+    `开始代码混淆...`,
+    list.map((item) => item.fileName)
+  );
+  Promise.all(
+    list.map(
+      (it) =>
+        new Promise((resolve) => {
+          const obfuscationResult = JavaScriptObfuscator.obfuscate(it.content, {
+            /** 这些都是配置 */
+            // optionsPreset: "low-obfuscation",
+            // disableConsoleOutput: false,
+            // controlFlowFlattening: true,
+            selfDefending: true,
+          });
+          fs.writeFile(
+            buildDir + it.fileName,
+            obfuscationResult.getObfuscatedCode(),
+            (err) => {
+              if (err) {
+                return console.log(`混淆失败文件=>> ${it.fileName}`, err);
+              }
+              console.log(`已混淆完成文件=>> ${it.fileName}`);
+              resolve(1);
+            }
+          );
+        })
+    )
+  ).then(() => {
+    console.log(`代码混淆完成!`);
+  });
+});

+ 5 - 6
package.json

@@ -6,7 +6,7 @@
     "start": "vite --host 0.0.0.0",
     "dev": "vite",
     "prebuild": "node --experimental-json-modules prebuild.mjs",
-    "build": "vue-tsc --noEmit --skipLibCheck && vite build",
+    "build": "vue-tsc --noEmit --skipLibCheck && vite build && node obfuscator.js",
     "build-without-type-check": "vite build",
     "typecheck": "vue-tsc --noEmit --skipLibCheck",
     "serve": "vite preview",
@@ -14,9 +14,9 @@
     "format": "prettier --ignore-path .gitignore .  --write",
     "test": "vitest",
     "prebuild:test": "node --experimental-json-modules prebuild.mjs test",
-    "build:test": "vite build --mode test",
+    "build:test": "vite build --mode test && node obfuscator.js",
     "prebuild:prod": "node --experimental-json-modules prebuild.mjs production",
-    "build:prod": "vite build --mode production"
+    "build:prod": "vite build --mode production && node obfuscator.js"
   },
   "dependencies": {
     "@aliyun-sls/web-track-browser": "^0.0.3",
@@ -62,11 +62,10 @@
     "eslint-config-prettier": "^8.5.0",
     "eslint-plugin-vue": "^9.1.1",
     "happy-dom": "^5.3.1",
-    "javascript-obfuscator": "^4.0.0",
+    "javascript-obfuscator": "4.0.0",
     "postcss": "^8.4.14",
     "prettier": "^2.7.1",
     "rollup": "^2.75.6",
-    "rollup-plugin-obfuscator": "^0.2.2",
     "typescript": "^4.7.3",
     "unplugin-auto-import": "^0.8.8",
     "unplugin-vue-components": "^0.19.6",
@@ -75,4 +74,4 @@
     "vue-eslint-parser": "9.0.2",
     "vue-tsc": "^0.37.9"
   }
-}
+}

+ 88 - 65
vite.config.ts

@@ -5,7 +5,7 @@ import { NaiveUiResolver } from "unplugin-vue-components/resolvers";
 import AutoImport from "unplugin-auto-import/vite";
 import legacy from "@vitejs/plugin-legacy";
 import vueJsx from "@vitejs/plugin-vue-jsx";
-import obfuscator from "rollup-plugin-obfuscator";
+// import obfuscator from "rollup-plugin-obfuscator";
 
 const SERVER_URL = "https://192.168.10.39";
 const path = require("path");
@@ -47,70 +47,93 @@ export default defineConfig({
       targets: ["chrome >= 58"],
       additionalLegacyPolyfills: ["regenerator-runtime/runtime"],
     }),
-    process.env.NODE_ENV === "development"
-      ? {}
-      : obfuscator({
-          // 不同的构建可能成功运行也可能不成功?
-          // 看看是不是单个文件做transform,然后对最终文件做renderChunk导致的构建结果不一致
-          fileOptions: false,
-          // fileOptions: {
-          //   optionsPreset: "low-obfuscation",
-          //   debugProtection: false,
-          //   transformObjectKeys: false,
-          //   disableConsoleOutput: false,
-          //   // controlFlowFlattening: true,
-          //   controlFlowFlatteningThreshold: 0.9,
-          //   // per-file:
-          //   deadCodeInjection: true,
-          //   deadCodeInjectionThreshold: 1,
-          //   // stringArrayThreshold: 1,
-          //   // numbersToExpressions: true,
-          //   // controlFlowFlatteningThreshold: 1,
-          //   // stringArrayCallsTransform: true,
-          //   // stringArrayCallsTransformThreshold: 1,
-          //   // stringArrayEncoding: ["rc4"],
-          // },
-          globalOptions: {
-            optionsPreset: "low-obfuscation",
-            // debugProtection: true,
-            // debugProtectionInterval: 2000,
-            // domainLock: [
-            //   ".exam-cloud.cn",
-            //   ".test41v3.qmth.com.cn",
-            //   ".ea100.com.cn",
-            //   ".ecs.qmth.com.cn",
-            //   ".qmth.com.cn",
-            //   ".dev39.qmth.com.cn",
-            //   ".test41.qmth.com.cn",
-            // ],
-            domainLock: [],
-            // selfDefending: true,
-            // sourceMap: false,
-            // splitStrings: true,
-            disableConsoleOutput: false,
-            // // stringArrayCallsTransform: true,
-            // deadCodeInjection: true,
-            // numbersToExpressions: true,
-            controlFlowFlattening: true,
-            // stringArrayEncoding: ["none", "base64", "rc4"],
-            // stringArrayRotate: true,
-            // transformObjectKeys: true,
-          },
-          include: [
-            // "src/**/**.ts",
-            // "src/**/**.js",
-            "src/utils/**.ts",
-            // "**/UserLogin/**.ts",
-            // "**/Examing/**/**.ts",
-          ],
-          exclude: ["node_modules/**"],
-          ...(disableObfuse
-            ? {
-                fileOptions: false,
-                globalOptions: false,
-              }
-            : {}),
-        }),
+    // process.env.NODE_ENV === "development"
+    //   ? {}
+    //   : // : obfuscator({
+    //     // 不同的构建可能成功运行也可能不成功?
+    //     // 看看是不是单个文件做transform,然后对最终文件做renderChunk导致的构建结果不一致
+    //     fileOptions: false,
+    //     // fileOptions: {
+    //     //   optionsPreset: "low-obfuscation",
+    //     //   debugProtection: false,
+    //     //   transformObjectKeys: false,
+    //     //   disableConsoleOutput: false,
+    //     //   // controlFlowFlattening: true,
+    //     //   controlFlowFlatteningThreshold: 0.9,
+    //     //   // per-file:
+    //     //   deadCodeInjection: true,
+    //     //   deadCodeInjectionThreshold: 1,
+    //     //   // stringArrayThreshold: 1,
+    //     //   // numbersToExpressions: true,
+    //     //   // controlFlowFlatteningThreshold: 1,
+    //     //   // stringArrayCallsTransform: true,
+    //     //   // stringArrayCallsTransformThreshold: 1,
+    //     //   // stringArrayEncoding: ["rc4"],
+    //     // },
+    //     globalOptions: {
+    //       optionsPreset: "low-obfuscation",
+    //       // debugProtection: true,
+    //       // debugProtectionInterval: 2000,
+    //       // domainLock: [
+    //       //   ".exam-cloud.cn",
+    //       //   ".test41v3.qmth.com.cn",
+    //       //   ".ea100.com.cn",
+    //       //   ".ecs.qmth.com.cn",
+    //       //   ".qmth.com.cn",
+    //       //   ".dev39.qmth.com.cn",
+    //       //   ".test41.qmth.com.cn",
+    //       // ],
+    //       domainLock: [],
+    //       // selfDefending: true,
+    //       // sourceMap: false,
+    //       // splitStrings: true,
+    //       disableConsoleOutput: false,
+    //       // // stringArrayCallsTransform: true,
+    //       // deadCodeInjection: true,
+    //       // numbersToExpressions: true,
+    //       controlFlowFlattening: true,
+    //       // stringArrayEncoding: ["none", "base64", "rc4"],
+    //       // stringArrayRotate: true,
+    //       // transformObjectKeys: true,
+    //     },
+    //     include: [
+    //       // "src/**/**.ts",
+    //       // "src/**/**.js",
+    //       "src/utils/**.ts",
+    //       // "**/UserLogin/**.ts",
+    //       // "**/Examing/**/**.ts",
+    //     ],
+    //     exclude: ["node_modules/**"],
+    //     ...(disableObfuse
+    //       ? {
+    //           fileOptions: false,
+    //           globalOptions: false,
+    //         }
+    //       : {}),
+    //   }),
+    // javascriptObfuscator({
+    //   compact: true,
+    //   controlFlowFlattening: true,
+    //   controlFlowFlatteningThreshold: 1,
+    //   deadCodeInjection: true,
+    //   deadCodeInjectionThreshold: 1,
+    //   debugProtection: true,
+    //   debugProtectionInterval: 0,
+    //   disableConsoleOutput: true,
+    //   identifierNamesGenerator: "hexadecimal",
+    //   log: false,
+    //   renameGlobals: false,
+    //   rotateStringArray: true,
+    //   selfDefending: true,
+    //   shuffleStringArray: true,
+    //   splitStrings: true,
+    //   splitStringsChunkLength: 10,
+    //   stringArray: true,
+    //   stringArrayEncoding: ["rc4"],
+    //   stringArrayThreshold: 1,
+    //   transformObjectKeys: true,
+    //   unicodeEscapeSequence: false,
+    // }),
   ],
   server: {
     port: 3000,