gaoluyang
2026-06-24 712aa51536236d43e87273e4ce45ac5691dffad8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#!/usr/bin/env node
import { readFileSync, writeFileSync, readdirSync, statSync } from 'node:fs';
import { join, relative, dirname } from 'node:path';
 
const projectRoot = 'D:/beforeProject/yudao-ui-admin-vben/web-antd-standalone';
 
// 递归获取所有文件
function getAllFiles(dir, files = [], extensions = ['.ts', '.tsx', '.vue', '.js', '.mjs', '.cjs', '.scss', '.css']) {
  const entries = readdirSync(dir);
  for (const entry of entries) {
    const fullPath = join(dir, entry);
    if (statSync(fullPath).isDirectory()) {
      if (!['node_modules', 'dist', '.git'].includes(entry)) {
        getAllFiles(fullPath, files, extensions);
      }
    } else if (extensions.some(ext => entry.endsWith(ext))) {
      files.push(fullPath);
    }
  }
  return files;
}
 
// 处理子路径引用
function processSubPathImport(fromFile, packageName, subPath) {
  const fromDir = dirname(fromFile);
 
  const packageBasePaths = {
    '@vben/plugins': 'src/packages/effects/plugins/src',
    '@vben/styles': 'src/packages/styles/src',
    '@vben/stores': 'src/packages/stores/src',
    '@vben/types': 'src/packages/types/src',
    '@vben/icons': 'src/packages/icons/src',
    '@vben/locales': 'src/packages/locales/src',
    '@vben/utils': 'src/packages/utils/src',
    '@vben/common-ui': 'src/packages/effects/common-ui/src',
    '@vben/hooks': 'src/packages/effects/hooks/src',
    '@vben/layouts': 'src/packages/effects/layouts/src',
    '@vben/request': 'src/packages/effects/request/src',
    '@vben/access': 'src/packages/effects/access/src',
    '@vben/constants': 'src/packages/constants/src',
    '@vben/preferences': 'src/packages/preferences/src',
    '@vben/tailwind-config': 'src/internal/tailwind-config/src',
    '@vben/node-utils': 'src/internal/node-utils/src',
    '@vben/vite-config': 'src/internal/vite-config/src',
    '@vben-core/composables': 'src/packages/@core/composables/src',
    '@vben-core/design': 'src/packages/@core/base/design/src',
    '@vben-core/form-ui': 'src/packages/@core/ui-kit/form-ui/src',
    '@vben-core/icons': 'src/packages/@core/base/icons/src',
    '@vben-core/layout-ui': 'src/packages/@core/ui-kit/layout-ui/src',
    '@vben-core/menu-ui': 'src/packages/@core/ui-kit/menu-ui/src',
    '@vben-core/popup-ui': 'src/packages/@core/ui-kit/popup-ui/src',
    '@vben-core/preferences': 'src/packages/@core/preferences/src',
    '@vben-core/shadcn-ui': 'src/packages/@core/ui-kit/shadcn-ui/src',
    '@vben-core/shared': 'src/packages/@core/base/shared/src',
    '@vben-core/tabs-ui': 'src/packages/@core/ui-kit/tabs-ui/src',
    '@vben-core/typings': 'src/packages/@core/base/typings/src',
  };
 
  const basePath = packageBasePaths[packageName];
  if (!basePath) return null;
 
  // 处理特殊子路径
  if (packageName === '@vben/plugins') {
    const pluginSubPaths = {
      'echarts': 'echarts',
      'vxe-table': 'vxe-table',
      'markmap': 'markmap',
      'tinyflow': 'tinyflow',
      'motion': 'motion',
      'code-editor': 'code-editor',
      'tiptap': 'tiptap',
    };
    const subDir = pluginSubPaths[subPath];
    if (subDir) {
      const targetPath = join(projectRoot, basePath, subDir);
      const relPath = relative(fromDir, targetPath);
      return relPath.startsWith('.') ? relPath : './' + relPath;
    }
  }
 
  if (packageName === '@vben/styles') {
    const stylesSubPaths = {
      'global': 'global',
      'antd': 'antd',
    };
    const subDir = stylesSubPaths[subPath];
    if (subDir) {
      const targetPath = join(projectRoot, basePath, subDir);
      const relPath = relative(fromDir, targetPath);
      return relPath.startsWith('.') ? relPath : './' + relPath;
    }
  }
 
  if (packageName === '@vben/common-ui') {
    // 处理 es 路径
    if (subPath.startsWith('es/')) {
      const esPath = subPath.replace('es/', '');
      const targetPath = join(projectRoot, basePath, esPath);
      const relPath = relative(fromDir, targetPath);
      return relPath.startsWith('.') ? relPath : './' + relPath;
    }
  }
 
  if (packageName === '@vben/types') {
    if (subPath === 'global') {
      const targetPath = join(projectRoot, basePath, 'global.d.ts');
      const relPath = relative(fromDir, targetPath);
      return relPath.startsWith('.') ? relPath : './' + relPath;
    }
  }
 
  if (packageName === '@vben/tailwind-config') {
    if (subPath === 'theme') {
      const targetPath = join(projectRoot, basePath, 'theme.ts');
      const relPath = relative(fromDir, targetPath);
      return relPath.startsWith('.') ? relPath : './' + relPath;
    }
  }
 
  // 默认:直接拼接子路径
  const targetPath = join(projectRoot, basePath, subPath);
  const relPath = relative(fromDir, targetPath);
  return relPath.startsWith('.') ? relPath : './' + relPath;
}
 
// 处理单个文件
function processFile(filePath) {
  let content = readFileSync(filePath, 'utf-8');
  let modified = false;
 
  // 处理 @vben/plugins/echarts 等子路径引用
  const subPathRegex = /from\s+['"](@vben(?:-core)?\/[^/]+\/[^'"]+)['"]/g;
  const subPathRegex2 = /import\s*\(\s*['"](@vben(?:-core)?\/[^/]+\/[^'"]+)['"]\s*\)/g;
  const scssImportRegex = /@use\s+['"](@vben\/[^'"]+)['"]/g;
  const scssImportRegex2 = /@import\s+['"](@vben\/[^'"]+)['"]/g;
 
  content = content.replace(subPathRegex, (match, fullImport) => {
    const parts = fullImport.split('/');
    const packageName = parts.slice(0, 2).join('/');
    const subPath = parts.slice(2).join('/');
 
    const relativePath = processSubPathImport(filePath, packageName, subPath);
    if (relativePath) {
      modified = true;
      return `from '${relativePath}'`;
    }
    return match;
  });
 
  content = content.replace(subPathRegex2, (match, fullImport) => {
    const parts = fullImport.split('/');
    const packageName = parts.slice(0, 2).join('/');
    const subPath = parts.slice(2).join('/');
 
    const relativePath = processSubPathImport(filePath, packageName, subPath);
    if (relativePath) {
      modified = true;
      return `import('${relativePath}')`;
    }
    return match;
  });
 
  // 处理 SCSS @use 和 @import
  if (filePath.endsWith('.scss') || filePath.endsWith('.vue')) {
    content = content.replace(scssImportRegex, (match, fullImport) => {
      const parts = fullImport.split('/');
      const packageName = parts.slice(0, 2).join('/');
      const subPath = parts.slice(2).join('/');
 
      const relativePath = processSubPathImport(filePath, packageName, subPath);
      if (relativePath) {
        modified = true;
        return `@use '${relativePath}'`;
      }
      return match;
    });
 
    content = content.replace(scssImportRegex2, (match, fullImport) => {
      const parts = fullImport.split('/');
      const packageName = parts.slice(0, 2).join('/');
      const subPath = parts.slice(2).join('/');
 
      const relativePath = processSubPathImport(filePath, packageName, subPath);
      if (relativePath) {
        modified = true;
        return `@import '${relativePath}'`;
      }
      return match;
    });
  }
 
  if (modified) {
    writeFileSync(filePath, content);
    console.log(`Updated: ${filePath}`);
  }
 
  return modified;
}
 
// 主函数
const srcDir = join(projectRoot, 'src');
const files = getAllFiles(srcDir);
 
let updatedCount = 0;
for (const file of files) {
  if (processFile(file)) {
    updatedCount++;
  }
}
 
console.log(`\nTotal files updated: ${updatedCount}`);