Skip to content

Commit ecdf4fd

Browse files
committed
feat(compiler): add app ID validation to prevent duplicate IDs during batch compilation
1 parent 7e85f4a commit ecdf4fd

1 file changed

Lines changed: 45 additions & 0 deletions

File tree

fe/packages/compiler/src/bin/compile.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,49 @@ function isCompilerModified(lastCompileTime) {
4343
return isModified(COMPILER_SRC_DIR, lastCompileTime)
4444
}
4545

46+
function readAppId(workPath) {
47+
const projectConfigPath = path.join(workPath, 'project.config.json')
48+
if (!fs.existsSync(projectConfigPath)) {
49+
return null
50+
}
51+
52+
try {
53+
const projectConfig = JSON.parse(fs.readFileSync(projectConfigPath, 'utf8'))
54+
return projectConfig.appid || null
55+
}
56+
catch (error) {
57+
console.warn(`读取 ${projectConfigPath} 失败:`, error.message)
58+
return null
59+
}
60+
}
61+
62+
function assertUniqueAppIds(exampleRoot, directories) {
63+
const appIdMap = new Map()
64+
65+
for (const directory of directories) {
66+
const workPath = path.join(exampleRoot, directory)
67+
const appId = readAppId(workPath)
68+
if (!appId) {
69+
continue
70+
}
71+
72+
const appNames = appIdMap.get(appId) || []
73+
appNames.push(directory)
74+
appIdMap.set(appId, appNames)
75+
}
76+
77+
const duplicates = [...appIdMap.entries()].filter(([, appNames]) => appNames.length > 1)
78+
if (duplicates.length === 0) {
79+
return
80+
}
81+
82+
const detail = duplicates
83+
.map(([appId, appNames]) => `${appId}: ${appNames.join(', ')}`)
84+
.join('\n')
85+
86+
throw new Error(`检测到重复的 appid,批量编译会覆盖产物:\n${detail}`)
87+
}
88+
4689
async function cleanUpOldApps(targetPath, appList) {
4790
try {
4891
// 清理目标目录
@@ -95,6 +138,8 @@ async function buildMiniApp() {
95138
return fs.existsSync(filePath) && fs.statSync(filePath).isDirectory();
96139
})
97140

141+
assertUniqueAppIds(currentDirectory, directories)
142+
98143
const appList = []
99144
for (const fileName of directories) {
100145
const workPath = path.resolve(`./example/${fileName}`)

0 commit comments

Comments
 (0)