|
@@ -0,0 +1,53 @@
|
|
|
+// replace-use-with-import.js
|
|
|
+const fs = require('fs');
|
|
|
+const path = require('path');
|
|
|
+
|
|
|
+const targetDir = path.resolve(process.argv[2] || '.'); // 传入目录,默认当前目录
|
|
|
+
|
|
|
+// 读取目录,递归遍历文件
|
|
|
+function walk(dir) {
|
|
|
+ let results = [];
|
|
|
+ const list = fs.readdirSync(dir, { withFileTypes: true });
|
|
|
+ for (const file of list) {
|
|
|
+ const fullPath = path.join(dir, file.name);
|
|
|
+ if (file.isDirectory()) {
|
|
|
+ results = results.concat(walk(fullPath));
|
|
|
+ } else if (file.isFile()) {
|
|
|
+ if (/\.(vue|scss)$/.test(file.name)) {
|
|
|
+ results.push(fullPath);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return results;
|
|
|
+}
|
|
|
+
|
|
|
+// 替换函数
|
|
|
+function replaceUseToImport(content) {
|
|
|
+ // 正则匹配 @use "xxx" as *; 或 @use 'xxx' as *;
|
|
|
+ const regex = /@use\s+(['"])([^'"]+)\1\s+as\s+\*;/g;
|
|
|
+ return content.replace(regex, (match, quote, path) => {
|
|
|
+ return `@import ${quote}${path}${quote};`;
|
|
|
+ });
|
|
|
+}
|
|
|
+
|
|
|
+function main() {
|
|
|
+ const files = walk(targetDir);
|
|
|
+ console.log(`找到 ${files.length} 个文件,开始替换...`);
|
|
|
+
|
|
|
+ let totalReplacements = 0;
|
|
|
+
|
|
|
+ for (const filePath of files) {
|
|
|
+ let content = fs.readFileSync(filePath, 'utf-8');
|
|
|
+ const newContent = replaceUseToImport(content);
|
|
|
+
|
|
|
+ if (newContent !== content) {
|
|
|
+ fs.writeFileSync(filePath, newContent, 'utf-8');
|
|
|
+ totalReplacements++;
|
|
|
+ console.log(`替换完成: ${filePath}`);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ console.log(`总共替换了 ${totalReplacements} 个文件中的 @use -> @import`);
|
|
|
+}
|
|
|
+
|
|
|
+main();
|