|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +/** |
| 4 | + * Plasmo + Tailwind v4 Compatibility Patch |
| 5 | + * |
| 6 | + * Automatically fixes "node:" import issues in jiti and @tailwindcss/oxide |
| 7 | + * that cause Plasmo builds to fail when using Tailwind CSS v4. |
| 8 | + * |
| 9 | + * @see https://github.com/PlasmoHQ/plasmo/issues/1188 |
| 10 | + */ |
| 11 | + |
| 12 | +const fs = require('fs'); |
| 13 | +const path = require('path'); |
| 14 | + |
| 15 | +// Console colors |
| 16 | +const c = { |
| 17 | + reset: '\x1b[0m', |
| 18 | + red: '\x1b[31m', |
| 19 | + green: '\x1b[32m', |
| 20 | + yellow: '\x1b[33m', |
| 21 | + blue: '\x1b[34m', |
| 22 | + cyan: '\x1b[36m', |
| 23 | +}; |
| 24 | + |
| 25 | +const log = (msg, color = 'reset') => |
| 26 | + console.log(`${c[color]}${msg}${c.reset}`); |
| 27 | + |
| 28 | +/** |
| 29 | + * Find package files that need patching |
| 30 | + */ |
| 31 | +const findPackageFiles = () => { |
| 32 | + const nodeModules = path.resolve('node_modules'); |
| 33 | + if (!fs.existsSync(nodeModules)) return []; |
| 34 | + |
| 35 | + const files = []; |
| 36 | + const pnpmPath = path.join(nodeModules, '.pnpm'); |
| 37 | + |
| 38 | + // Helper to safely read directory |
| 39 | + const readDir = (dir) => { |
| 40 | + try { |
| 41 | + return fs.existsSync(dir) ? fs.readdirSync(dir) : []; |
| 42 | + } catch { |
| 43 | + return []; |
| 44 | + } |
| 45 | + }; |
| 46 | + |
| 47 | + // Helper to check file exists |
| 48 | + const fileExists = (filePath) => { |
| 49 | + try { |
| 50 | + return fs.existsSync(filePath); |
| 51 | + } catch { |
| 52 | + return false; |
| 53 | + } |
| 54 | + }; |
| 55 | + |
| 56 | + // Find jiti files |
| 57 | + const findJitiFiles = (basePath) => { |
| 58 | + const jitiPath = path.join(basePath, 'jiti'); |
| 59 | + if (!fileExists(jitiPath)) return []; |
| 60 | + |
| 61 | + const targets = ['dist/jiti.cjs', 'dist/babel.cjs', 'lib/jiti.cjs']; |
| 62 | + |
| 63 | + return targets |
| 64 | + .map((target) => path.join(jitiPath, target)) |
| 65 | + .filter(fileExists); |
| 66 | + }; |
| 67 | + |
| 68 | + // Find oxide files |
| 69 | + const findOxideFiles = (basePath) => { |
| 70 | + const oxidePath = path.join(basePath, '@tailwindcss', 'oxide', 'index.js'); |
| 71 | + return fileExists(oxidePath) ? [oxidePath] : []; |
| 72 | + }; |
| 73 | + |
| 74 | + // Search pnpm structure |
| 75 | + if (fileExists(pnpmPath)) { |
| 76 | + readDir(pnpmPath).forEach((entry) => { |
| 77 | + if (entry.startsWith('jiti@')) { |
| 78 | + const packagePath = path.join(pnpmPath, entry, 'node_modules'); |
| 79 | + files.push(...findJitiFiles(packagePath)); |
| 80 | + } |
| 81 | + |
| 82 | + if ( |
| 83 | + entry.startsWith('@tailwindcss+oxide@') || |
| 84 | + entry.startsWith('%40tailwindcss+oxide@') |
| 85 | + ) { |
| 86 | + const packagePath = path.join(pnpmPath, entry, 'node_modules'); |
| 87 | + files.push(...findOxideFiles(packagePath)); |
| 88 | + } |
| 89 | + }); |
| 90 | + } |
| 91 | + |
| 92 | + // Search regular node_modules |
| 93 | + files.push(...findJitiFiles(nodeModules)); |
| 94 | + files.push(...findOxideFiles(nodeModules)); |
| 95 | + |
| 96 | + return [...new Set(files)]; // Remove duplicates |
| 97 | +}; |
| 98 | + |
| 99 | +/** |
| 100 | + * Patch a single file |
| 101 | + */ |
| 102 | +const patchFile = (filePath) => { |
| 103 | + try { |
| 104 | + if (!fs.existsSync(filePath)) { |
| 105 | + log(`⚠️ File not found: ${path.basename(filePath)}`, 'yellow'); |
| 106 | + return false; |
| 107 | + } |
| 108 | + |
| 109 | + const content = fs.readFileSync(filePath, 'utf8'); |
| 110 | + const hasNodeImports = |
| 111 | + content.includes('require("node:') || content.includes("require('node:"); |
| 112 | + |
| 113 | + if (!hasNodeImports) { |
| 114 | + log(`✅ ${path.basename(filePath)} - already patched`, 'green'); |
| 115 | + return true; |
| 116 | + } |
| 117 | + |
| 118 | + // Apply patches for both quote styles |
| 119 | + const patched = content |
| 120 | + .replace(/require\("node:([^"]+)"\)/g, 'require("$1")') |
| 121 | + .replace(/require\('node:([^']+)'\)/g, "require('$1')"); |
| 122 | + |
| 123 | + fs.writeFileSync(filePath, patched, 'utf8'); |
| 124 | + log(`✅ ${path.basename(filePath)} - patched successfully`, 'green'); |
| 125 | + return true; |
| 126 | + } catch (error) { |
| 127 | + log(`❌ ${path.basename(filePath)} - error: ${error.message}`, 'red'); |
| 128 | + return false; |
| 129 | + } |
| 130 | +}; |
| 131 | + |
| 132 | +/** |
| 133 | + * Main execution |
| 134 | + */ |
| 135 | +const main = () => { |
| 136 | + log('🔧 Plasmo + Tailwind v4 compatibility patch', 'cyan'); |
| 137 | + |
| 138 | + const files = findPackageFiles(); |
| 139 | + |
| 140 | + if (files.length === 0) { |
| 141 | + log('⚠️ No files found to patch', 'yellow'); |
| 142 | + log( |
| 143 | + ' This might mean packages are not installed or using different structure', |
| 144 | + 'yellow', |
| 145 | + ); |
| 146 | + return; |
| 147 | + } |
| 148 | + |
| 149 | + log(` Found ${files.length} files to check`, 'blue'); |
| 150 | + |
| 151 | + const results = files.map(patchFile); |
| 152 | + const successful = results.filter(Boolean).length; |
| 153 | + |
| 154 | + log(''); // Empty line |
| 155 | + |
| 156 | + if (successful === files.length) { |
| 157 | + log('🎉 All files patched successfully!', 'green'); |
| 158 | + log(' Tailwind v4 should now work with Plasmo', 'green'); |
| 159 | + } else { |
| 160 | + log(`⚠️ ${successful}/${files.length} files patched`, 'yellow'); |
| 161 | + } |
| 162 | + |
| 163 | + if (successful === 0) { |
| 164 | + log('💡 Try running: pnpm install && node scripts/patch-jiti.js', 'blue'); |
| 165 | + } |
| 166 | +}; |
| 167 | + |
| 168 | +// Execute if run directly |
| 169 | +if (require.main === module) { |
| 170 | + main(); |
| 171 | +} |
| 172 | + |
| 173 | +module.exports = { main, patchFile, findPackageFiles }; |
0 commit comments