From 20b34b74a257497a6b766796076cc88b045af49b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?LeoYuan=20=E8=A2=81=E5=8A=9B=E7=9A=93?= Date: Tue, 14 Feb 2023 17:49:58 +0800 Subject: [PATCH] feat: handle extra slots --- modules/code-generator/src/const/index.ts | 21 +++++++++++++ .../src/generator/ProjectBuilder.ts | 30 ++++++++++++------- 2 files changed, 40 insertions(+), 11 deletions(-) diff --git a/modules/code-generator/src/const/index.ts b/modules/code-generator/src/const/index.ts index 103d912db..24449ca42 100644 --- a/modules/code-generator/src/const/index.ts +++ b/modules/code-generator/src/const/index.ts @@ -8,5 +8,26 @@ export const CONTAINER_TYPE = { export const SUPPORT_SCHEMA_VERSION_LIST = ['0.0.1', '1.0.0']; +// built-in slot names which have been handled in ProjectBuilder +export const BUILTIN_SLOT_NAMES = [ + 'pages', + 'components', + 'router', + 'entry', + 'appConfig', + 'buildConfig', + 'constants', + 'utils', + 'i18n', + 'globalStyle', + 'htmlEntry', + 'packageJSON', + 'demo', +]; + +export const isBuiltinSlotName = function (name: string) { + return BUILTIN_SLOT_NAMES.includes(name); +}; + export * from './file'; export * from './generator'; diff --git a/modules/code-generator/src/generator/ProjectBuilder.ts b/modules/code-generator/src/generator/ProjectBuilder.ts index 040136057..f3fb5fc1c 100644 --- a/modules/code-generator/src/generator/ProjectBuilder.ts +++ b/modules/code-generator/src/generator/ProjectBuilder.ts @@ -16,6 +16,7 @@ import { createResultDir, addDirectory, addFile } from '../utils/resultHelper'; import { createModuleBuilder } from './ModuleBuilder'; import { ProjectPreProcessor, ProjectPostProcessor, IContextData } from '../types/core'; import { CodeGeneratorError } from '../types/error'; +import { isBuiltinSlotName } from '../const'; interface IModuleInfo { moduleName?: string; @@ -272,17 +273,8 @@ export class ProjectBuilder implements IProjectBuilder { }); } - // TODO: 更多 slots 的处理??是不是可以考虑把 template 中所有的 slots 都处理下? - // const whitelistSlotNames = [ - // 'router', - // 'entry', - // 'appConfig', - // 'buildConfig', - // 'router', - // ]; - // Object.keys(this.template.slots).forEach((slotName: string) => { - - // }); + // handle extra slots + await this.generateExtraSlots(builders, parseResult, buildResult); // Post Process const isSingleComponent = parseResult?.project?.projectRemark?.isSingleComponent; @@ -339,6 +331,22 @@ export class ProjectBuilder implements IProjectBuilder { return builders; } + + private async generateExtraSlots( + builders: Record, + parseResult: IParseResult, + buildResult: IModuleInfo[], + ) { + for (const slotName in this.template.slots) { + if (!isBuiltinSlotName(slotName)) { + const { files } = await builders[slotName].generateModule(parseResult); + buildResult.push({ + path: this.template.slots[slotName].path, + files, + }); + } + } + } } export function createProjectBuilder(initOptions: ProjectBuilderInitOptions): IProjectBuilder {