Kaynağa Gözat

chore: 新增编译前变量替换处理

zhangyuhan 1 yıl önce
ebeveyn
işleme
36e5aba475
3 değiştirilmiş dosya ile 40 ekleme ve 2 silme
  1. 2 1
      package.json
  2. 1 1
      project.config.json
  3. 37 0
      script/replaceEnv.js

+ 2 - 1
package.json

@@ -10,7 +10,8 @@
     "framework": "Vue"
     "framework": "Vue"
   },
   },
   "scripts": {
   "scripts": {
-    "build:weapp": "taro build --type weapp",
+    "pre:env": "node script/replaceEnv.js",
+    "build:weapp": "npm run pre:env && taro build --type weapp",
     "build:h5": "taro build --type h5",
     "build:h5": "taro build --type h5",
     "dev:pord-weapp": "NODE_ENV=production npm run build:weapp -- --watch",
     "dev:pord-weapp": "NODE_ENV=production npm run build:weapp -- --watch",
     "dev:weapp": "npm run build:weapp -- --watch",
     "dev:weapp": "npm run build:weapp -- --watch",

+ 1 - 1
project.config.json

@@ -2,7 +2,7 @@
   "miniprogramRoot": "dist/",
   "miniprogramRoot": "dist/",
   "projectname": "jy-wx-mini2",
   "projectname": "jy-wx-mini2",
   "description": "jy-wx-mini",
   "description": "jy-wx-mini",
-  "appid": "wx124df4877c2b761a",
+  "appid": "wx37f06c38292f7d82",
   "setting": {
   "setting": {
     "urlCheck": true,
     "urlCheck": true,
     "es6": false,
     "es6": false,

+ 37 - 0
script/replaceEnv.js

@@ -0,0 +1,37 @@
+const fs = require('fs');
+const path = require('path');
+
+// 同步读取文件内容
+function readFileSync(filePath) {
+  return fs.readFileSync(filePath, 'utf8');
+}
+
+// 同步写入文件内容
+function writeFileSync(filePath, data) {
+  fs.writeFileSync(filePath, data, 'utf8');
+}
+
+// 替换文件中的文本
+function replaceTextInFileSync(filePath, matchKey, str) {
+  try {
+    // 读取文件内容
+    let fileContent = readFileSync(filePath);
+
+    // 替换文本
+    const newContent = fileContent.replace(matchKey, str);
+
+    // 写入新内容到文件
+    writeFileSync(filePath, newContent);
+
+    console.log('文本替换完成。');
+  } catch (error) {
+    console.error('文本替换失败:', error);
+  }
+}
+
+// 使用示例
+const filePath = path.resolve('./src/assets/style/_variables.scss'); // 替换为你的文件路径
+const matchKey = '$main: #2ABED1;'; // 替换为需要查找并替换的文本
+const str = '$main: #111;'; // 替换为新的文本
+
+// replaceTextInFileSync(filePath, matchKey, str);