Skip to content

Commit 574e348

Browse files
committed
feat: 支持 esModule#preferClassProperty 配置项
1 parent a37a647 commit 574e348

File tree

6 files changed

+50
-8
lines changed

6 files changed

+50
-8
lines changed

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,31 @@ interface IModuleInfo {
2424
}
2525

2626
export interface ProjectBuilderInitOptions {
27+
2728
/** 项目模板 */
2829
template: IProjectTemplate;
30+
2931
/** 项目插件 */
3032
plugins: IProjectPlugins;
33+
3134
/** 模块后置处理器 */
3235
postProcessors: PostProcessor[];
36+
3337
/** Schema 解析器 */
3438
schemaParser?: ISchemaParser;
39+
3540
/** 项目级别的前置处理器 */
3641
projectPreProcessors?: ProjectPreProcessor[];
42+
3743
/** 项目级别的后置处理器 */
3844
projectPostProcessors?: ProjectPostProcessor[];
45+
3946
/** 是否处于严格模式 */
4047
inStrictMode?: boolean;
48+
4149
/** 一些额外的上下文数据 */
4250
extraContextData?: Record<string, unknown>;
51+
4352
/**
4453
* Hook which is used to customize original options, we can reorder/add/remove plugins/processors
4554
* of the existing solution.
@@ -264,6 +273,16 @@ export class ProjectBuilder implements IProjectBuilder {
264273
}
265274

266275
// 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+
// });
267286

268287
// Post Process
269288
const isSingleComponent = parseResult?.project?.projectRemark?.isSingleComponent;

modules/code-generator/src/plugins/common/esmodule.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,7 @@ function buildPackageImport(
443443
export interface PluginConfig {
444444
fileType?: string; // 导出的文件类型
445445
useAliasName?: boolean; // 是否使用 componentName 重命名组件 identifier
446+
filter?: (deps: IDependency[]) => IDependency[]; // 支持过滤能力
446447
}
447448

448449
const pluginFactory: BuilderComponentPluginFactory<PluginConfig> = (config?: PluginConfig) => {
@@ -460,7 +461,8 @@ const pluginFactory: BuilderComponentPluginFactory<PluginConfig> = (config?: Plu
460461
const ir = next.ir as IWithDependency;
461462

462463
if (ir && ir.deps && ir.deps.length > 0) {
463-
const packs = groupDepsByPack(ir.deps);
464+
const deps = cfg.filter ? cfg.filter(ir.deps) : ir.deps;
465+
const packs = groupDepsByPack(deps);
464466

465467
Object.keys(packs).forEach((pkg) => {
466468
const chunks = buildPackageImport(pkg, packs[pkg], cfg.fileType, cfg.useAliasName);

modules/code-generator/src/plugins/component/react/containerInjectUtils.ts

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ import {
1616

1717
export interface PluginConfig {
1818
fileType: string;
19+
20+
/** prefer using class property to define utils */
21+
preferClassProperty?: boolean;
1922
}
2023

2124
const pluginFactory: BuilderComponentPluginFactory<PluginConfig> = (config?) => {
@@ -57,13 +60,25 @@ const pluginFactory: BuilderComponentPluginFactory<PluginConfig> = (config?) =>
5760
linkAfter: [COMMON_CHUNK_NAME.ExternalDepsImport],
5861
});
5962

60-
next.chunks.push({
61-
type: ChunkType.STRING,
62-
fileType: cfg.fileType,
63-
name: CLASS_DEFINE_CHUNK_NAME.ConstructorContent,
64-
content: 'this.utils = utils;',
65-
linkAfter: [CLASS_DEFINE_CHUNK_NAME.ConstructorStart],
66-
});
63+
if (cfg.preferClassProperty) {
64+
// mode: class property
65+
next.chunks.push({
66+
type: ChunkType.STRING,
67+
fileType: cfg.fileType,
68+
name: CLASS_DEFINE_CHUNK_NAME.InsVar,
69+
content: 'utils = utils;',
70+
linkAfter: [...DEFAULT_LINK_AFTER[CLASS_DEFINE_CHUNK_NAME.InsVar]],
71+
});
72+
} else {
73+
// mode: assign in constructor
74+
next.chunks.push({
75+
type: ChunkType.STRING,
76+
fileType: cfg.fileType,
77+
name: CLASS_DEFINE_CHUNK_NAME.ConstructorContent,
78+
content: 'this.utils = utils;',
79+
linkAfter: [CLASS_DEFINE_CHUNK_NAME.ConstructorStart],
80+
});
81+
}
6782

6883
if (useRef) {
6984
next.chunks.push({

modules/code-generator/src/postprocessor/prettier/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ const factory: PostProcessorFactory<ProcessorConfig> = (config?: ProcessorConfig
2222
let parser: prettier.BuiltInParserName | any;
2323
if (fileType === 'js' || fileType === 'jsx') {
2424
parser = 'babel';
25+
} else if (fileType === 'json') {
26+
parser = 'json-stringify';
2527
} else if (PARSERS.indexOf(fileType) >= 0) {
2628
parser = fileType;
2729
} else if (cfg.customFileTypeParser[fileType]) {

modules/code-generator/src/types/core.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@ export enum FileType {
2222
LESS = 'less',
2323
HTML = 'html',
2424
JS = 'js',
25+
MJS = 'mjs',
2526
JSX = 'jsx',
2627
TS = 'ts',
28+
MTS = 'mts',
2729
TSX = 'tsx',
2830
JSON = 'json',
2931
MD = 'md',
@@ -168,6 +170,7 @@ export interface IProjectBuilderOptions {
168170
* - expr: 求值的表达式
169171
*/
170172
evalErrorsHandler?: string;
173+
171174
/**
172175
* Hook which is used to customize original options, we can reorder/add/remove plugins/processors
173176
* of the existing solution.

modules/code-generator/src/types/intermediate.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export interface IRouterInfo extends IWithDependency {
4242
* project's remarks
4343
*/
4444
export interface ProjectRemark {
45+
4546
/** if current project only contain one container which type is `Component` */
4647
isSingleComponent?: boolean;
4748
}

0 commit comments

Comments
 (0)