feat(cli): 增加 useTs 配置

This commit is contained in:
kevinyzheng 2022-11-16 17:23:18 +08:00 committed by roymondchen
parent bc4b62a4c1
commit b512e14129
3 changed files with 17 additions and 11 deletions

View File

@ -8,5 +8,6 @@ cli({
componentFileAffix: '', componentFileAffix: '',
cleanTemp: true, cleanTemp: true,
temp: '.tmagic', temp: '.tmagic',
useTs: true,
dynamicImport: false, dynamicImport: false,
}); });

View File

@ -60,6 +60,8 @@ export interface UserConfig {
npmConfig?: NpmConfig; npmConfig?: NpmConfig;
/** 是否使用import()加载组件 */ /** 是否使用import()加载组件 */
dynamicImport?: boolean; dynamicImport?: boolean;
/** 入口文件是否生成为 ts 格式 */
useTs?: boolean;
hooks?: { hooks?: {
beforeWriteEntry?: (genContentMap: Record<string, string>, app: Core) => Promise<Record<string, string>>; beforeWriteEntry?: (genContentMap: Record<string, string>, app: Core) => Promise<Record<string, string>>;
}; };

View File

@ -5,27 +5,30 @@ import { EntryType } from '../types';
export const prepareEntryFile = async (app: App) => { export const prepareEntryFile = async (app: App) => {
const { componentMap = {}, pluginMap = {}, configMap = {}, valueMap = {}, eventMap = {} } = app.moduleMainFilePath; const { componentMap = {}, pluginMap = {}, configMap = {}, valueMap = {}, eventMap = {} } = app.moduleMainFilePath;
const { componentFileAffix, dynamicImport, hooks } = app.options; const { componentFileAffix, dynamicImport, hooks, useTs } = app.options;
let contentMap: Record<string, string> = { let contentMap: Record<string, string> = {
'comp-entry.ts': generateContent(EntryType.COMPONENT, componentMap, componentFileAffix), 'comp-entry': generateContent(useTs, EntryType.COMPONENT, componentMap, componentFileAffix),
'async-comp-entry.ts': generateContent(EntryType.COMPONENT, componentMap, componentFileAffix, dynamicImport), 'async-comp-entry': generateContent(useTs, EntryType.COMPONENT, componentMap, componentFileAffix, dynamicImport),
'plugin-entry.ts': generateContent(EntryType.PLUGIN, pluginMap), 'plugin-entry': generateContent(useTs, EntryType.PLUGIN, pluginMap),
'async-plugin-entry.ts': generateContent(EntryType.PLUGIN, pluginMap, '', dynamicImport), 'async-plugin-entry': generateContent(useTs, EntryType.PLUGIN, pluginMap, '', dynamicImport),
'config-entry.ts': generateContent(EntryType.CONFIG, configMap), 'config-entry': generateContent(useTs, EntryType.CONFIG, configMap),
'value-entry.ts': generateContent(EntryType.VALUE, valueMap), 'value-entry': generateContent(useTs, EntryType.VALUE, valueMap),
'event-entry.ts': generateContent(EntryType.EVENT, eventMap), 'event-entry': generateContent(useTs, EntryType.EVENT, eventMap),
}; };
if (typeof hooks?.beforeWriteEntry === 'function') { if (typeof hooks?.beforeWriteEntry === 'function') {
contentMap = await hooks.beforeWriteEntry(contentMap, app); contentMap = await hooks.beforeWriteEntry(contentMap, app);
} }
Object.keys(contentMap).forEach((fileName: string) => { Object.keys(contentMap).forEach((file: string) => {
app.writeTemp(fileName, contentMap[fileName]); const fileName = `${file}.${useTs ? 'ts' : 'js'}`;
app.writeTemp(fileName, contentMap[file]);
}); });
}; };
const generateContent = ( const generateContent = (
useTs = true,
type: EntryType, type: EntryType,
map: Record<string, string>, map: Record<string, string>,
componentFileAffix = '', componentFileAffix = '',
@ -51,7 +54,7 @@ const generateContent = (
const exportToken = `${type}s`; const exportToken = `${type}s`;
return prettyCode(`${importDeclarations.join(';')} return prettyCode(`${importDeclarations.join(';')}
const ${exportToken}: Record<string, any> = { const ${exportToken}${useTs ? ': Record<string, any>' : ''} = {
${list.join(',')} ${list.join(',')}
} }
export default ${exportToken}; export default ${exportToken};