Skip to content

Commit 20b34b7

Browse files
committed
feat: handle extra slots
1 parent 37e07bb commit 20b34b7

File tree

2 files changed

+40
-11
lines changed

2 files changed

+40
-11
lines changed

modules/code-generator/src/const/index.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,26 @@ export const CONTAINER_TYPE = {
88

99
export const SUPPORT_SCHEMA_VERSION_LIST = ['0.0.1', '1.0.0'];
1010

11+
// built-in slot names which have been handled in ProjectBuilder
12+
export const BUILTIN_SLOT_NAMES = [
13+
'pages',
14+
'components',
15+
'router',
16+
'entry',
17+
'appConfig',
18+
'buildConfig',
19+
'constants',
20+
'utils',
21+
'i18n',
22+
'globalStyle',
23+
'htmlEntry',
24+
'packageJSON',
25+
'demo',
26+
];
27+
28+
export const isBuiltinSlotName = function (name: string) {
29+
return BUILTIN_SLOT_NAMES.includes(name);
30+
};
31+
1132
export * from './file';
1233
export * from './generator';

modules/code-generator/src/generator/ProjectBuilder.ts

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { createResultDir, addDirectory, addFile } from '../utils/resultHelper';
1616
import { createModuleBuilder } from './ModuleBuilder';
1717
import { ProjectPreProcessor, ProjectPostProcessor, IContextData } from '../types/core';
1818
import { CodeGeneratorError } from '../types/error';
19+
import { isBuiltinSlotName } from '../const';
1920

2021
interface IModuleInfo {
2122
moduleName?: string;
@@ -272,17 +273,8 @@ export class ProjectBuilder implements IProjectBuilder {
272273
});
273274
}
274275

275-
// TODO: 更多 slots 的处理??是不是可以考虑把 template 中所有的 slots 都处理下?
276-
// const whitelistSlotNames = [
277-
// 'router',
278-
// 'entry',
279-
// 'appConfig',
280-
// 'buildConfig',
281-
// 'router',
282-
// ];
283-
// Object.keys(this.template.slots).forEach((slotName: string) => {
284-
285-
// });
276+
// handle extra slots
277+
await this.generateExtraSlots(builders, parseResult, buildResult);
286278

287279
// Post Process
288280
const isSingleComponent = parseResult?.project?.projectRemark?.isSingleComponent;
@@ -339,6 +331,22 @@ export class ProjectBuilder implements IProjectBuilder {
339331

340332
return builders;
341333
}
334+
335+
private async generateExtraSlots(
336+
builders: Record<string, IModuleBuilder>,
337+
parseResult: IParseResult,
338+
buildResult: IModuleInfo[],
339+
) {
340+
for (const slotName in this.template.slots) {
341+
if (!isBuiltinSlotName(slotName)) {
342+
const { files } = await builders[slotName].generateModule(parseResult);
343+
buildResult.push({
344+
path: this.template.slots[slotName].path,
345+
files,
346+
});
347+
}
348+
}
349+
}
342350
}
343351

344352
export function createProjectBuilder(initOptions: ProjectBuilderInitOptions): IProjectBuilder {

0 commit comments

Comments
 (0)