Просмотр исходного кода

fix: extend postinstall-fix.js to also patch HBuilderX bundled remove-scoped.js

liaoxg 3 месяцев назад
Родитель
Сommit
f21f0c0122
1 измененных файлов с 62 добавлено и 31 удалено
  1. 62 31
      cfc-frontend/postinstall-fix.js

+ 62 - 31
cfc-frontend/postinstall-fix.js

@@ -9,43 +9,74 @@
  *   "TypeError: Cannot read properties of undefined (reading 'spaces')"
  *
  * The fix adds a guard before accessing .spaces on selector.first.
+ *
+ * Patches BOTH:
+ *   1. Project local node_modules (for vue-cli-service / npm build)
+ *   2. HBuilderX bundled uniapp-cli (for HBuilderX build)
  */
 
 const fs = require('fs');
 const path = require('path');
 
-const filePath = path.join(
-  __dirname,
-  'node_modules',
-  '@dcloudio',
-  'vue-cli-plugin-uni',
-  'packages',
-  '@vue',
-  'component-compiler-utils',
-  'dist',
-  'stylePlugins',
-  'remove-scoped.js'
-);
-
-if (!fs.existsSync(filePath)) {
-  console.log('remove-scoped.js not found, skipping patch');
+/** Find all remove-scoped.js files to patch */
+function findRemoveScopedFiles() {
+  const files = [];
+
+  // 1. Project local node_modules
+  const localPath = path.join(__dirname, 'node_modules', '@dcloudio',
+    'vue-cli-plugin-uni', 'packages', '@vue', 'component-compiler-utils',
+    'dist', 'stylePlugins', 'remove-scoped.js');
+  if (fs.existsSync(localPath)) files.push(localPath);
+
+  // 2. HBuilderX bundled uniapp-cli (common install paths)
+  const hbxPaths = [
+    'E:\\develop\\HBuilderX\\plugins\\uniapp-cli',
+    'C:\\Program Files\\HBuilderX\\plugins\\uniapp-cli',
+    'C:\\Program Files (x86)\\HBuilderX\\plugins\\uniapp-cli',
+    path.join(process.env.LOCALAPPDATA || '', 'Programs', 'HBuilderX', 'plugins', 'uniapp-cli'),
+  ];
+  for (const base of hbxPaths) {
+    const hbxPath = path.join(base, 'node_modules', '@dcloudio',
+      'vue-cli-plugin-uni', 'packages', '@vue', 'component-compiler-utils',
+      'dist', 'stylePlugins', 'remove-scoped.js');
+    if (fs.existsSync(hbxPath)) files.push(hbxPath);
+  }
+
+  return files;
+}
+
+/** Patch a single remove-scoped.js file */
+function patchFile(filePath) {
+  let content = fs.readFileSync(filePath, 'utf8');
+
+  // Fix: selector.first.spaces.before = ''; → if (selector.first) { ... }
+  if (content.includes("selector.first.spaces.before = '';")) {
+    content = content.replace(
+      /selector\.first\.spaces\.before = '';/,
+      "if (selector.first) { selector.first.spaces.before = ''; }"
+    );
+    fs.writeFileSync(filePath, content, 'utf8');
+    console.log(`  ✅ ${filePath}`);
+    return true;
+  } else if (content.includes("if (selector.first) { selector.first.spaces.before = ''; }")) {
+    console.log(`  ⏭️  ${filePath} (already patched)`);
+    return true;
+  } else {
+    console.log(`  ❌ ${filePath} (unexpected format)`);
+    return false;
+  }
+}
+
+// Main
+const files = findRemoveScopedFiles();
+if (files.length === 0) {
+  console.log('remove-scoped.js not found in any location, skipping patch');
   process.exit(0);
 }
 
-let content = fs.readFileSync(filePath, 'utf8');
-
-// Fix line 56: selector.first.spaces.before = '';
-// Guard: if (selector.first && selector.first.spaces)
-if (content.includes("selector.first.spaces.before = '';")) {
-  content = content.replace(
-    /selector\.first\.spaces\.before = '';/,
-    "if (selector.first) { selector.first.spaces.before = ''; }"
-  );
-  fs.writeFileSync(filePath, content, 'utf8');
-  console.log('✅ remove-scoped.js: patched selector.first guard');
-} else if (content.includes("if (selector.first) { selector.first.spaces.before = ''; }")) {
-  console.log('remove-scoped.js: already patched, skipping');
-} else {
-  console.log('remove-scoped.js: unexpected format, manual check needed');
-  console.log('Look for: selector.first.spaces.before');
+console.log(`Found ${files.length} remove-scoped.js to patch:`);
+let patched = 0;
+for (const f of files) {
+  if (patchFile(f)) patched++;
 }
+console.log(`Done: ${patched}/${files.length} patched`);