feat: 支持 esModule#preferClassProperty 配置项

This commit is contained in:
LeoYuan 袁力皓 2023-02-02 20:12:37 +08:00
parent a37a647ef6
commit 574e348c1e
6 changed files with 50 additions and 8 deletions

View File

@ -24,22 +24,31 @@ interface IModuleInfo {
}
export interface ProjectBuilderInitOptions {
/** 项目模板 */
template: IProjectTemplate;
/** 项目插件 */
plugins: IProjectPlugins;
/** 模块后置处理器 */
postProcessors: PostProcessor[];
/** Schema 解析器 */
schemaParser?: ISchemaParser;
/** 项目级别的前置处理器 */
projectPreProcessors?: ProjectPreProcessor[];
/** 项目级别的后置处理器 */
projectPostProcessors?: ProjectPostProcessor[];
/** 是否处于严格模式 */
inStrictMode?: boolean;
/** 一些额外的上下文数据 */
extraContextData?: Record<string, unknown>;
/**
* Hook which is used to customize original options, we can reorder/add/remove plugins/processors
* of the existing solution.
@ -264,6 +273,16 @@ export class ProjectBuilder implements IProjectBuilder {
}
// TODO: 更多 slots 的处理??是不是可以考虑把 template 中所有的 slots 都处理下?
// const whitelistSlotNames = [
// 'router',
// 'entry',
// 'appConfig',
// 'buildConfig',
// 'router',
// ];
// Object.keys(this.template.slots).forEach((slotName: string) => {
// });
// Post Process
const isSingleComponent = parseResult?.project?.projectRemark?.isSingleComponent;

View File

@ -443,6 +443,7 @@ function buildPackageImport(
export interface PluginConfig {
fileType?: string; // 导出的文件类型
useAliasName?: boolean; // 是否使用 componentName 重命名组件 identifier
filter?: (deps: IDependency[]) => IDependency[]; // 支持过滤能力
}
const pluginFactory: BuilderComponentPluginFactory<PluginConfig> = (config?: PluginConfig) => {
@ -460,7 +461,8 @@ const pluginFactory: BuilderComponentPluginFactory<PluginConfig> = (config?: Plu
const ir = next.ir as IWithDependency;
if (ir && ir.deps && ir.deps.length > 0) {
const packs = groupDepsByPack(ir.deps);
const deps = cfg.filter ? cfg.filter(ir.deps) : ir.deps;
const packs = groupDepsByPack(deps);
Object.keys(packs).forEach((pkg) => {
const chunks = buildPackageImport(pkg, packs[pkg], cfg.fileType, cfg.useAliasName);

View File

@ -16,6 +16,9 @@ import {
export interface PluginConfig {
fileType: string;
/** prefer using class property to define utils */
preferClassProperty?: boolean;
}
const pluginFactory: BuilderComponentPluginFactory<PluginConfig> = (config?) => {
@ -57,13 +60,25 @@ const pluginFactory: BuilderComponentPluginFactory<PluginConfig> = (config?) =>
linkAfter: [COMMON_CHUNK_NAME.ExternalDepsImport],
});
next.chunks.push({
type: ChunkType.STRING,
fileType: cfg.fileType,
name: CLASS_DEFINE_CHUNK_NAME.ConstructorContent,
content: 'this.utils = utils;',
linkAfter: [CLASS_DEFINE_CHUNK_NAME.ConstructorStart],
});
if (cfg.preferClassProperty) {
// mode: class property
next.chunks.push({
type: ChunkType.STRING,
fileType: cfg.fileType,
name: CLASS_DEFINE_CHUNK_NAME.InsVar,
content: 'utils = utils;',
linkAfter: [...DEFAULT_LINK_AFTER[CLASS_DEFINE_CHUNK_NAME.InsVar]],
});
} else {
// mode: assign in constructor
next.chunks.push({
type: ChunkType.STRING,
fileType: cfg.fileType,
name: CLASS_DEFINE_CHUNK_NAME.ConstructorContent,
content: 'this.utils = utils;',
linkAfter: [CLASS_DEFINE_CHUNK_NAME.ConstructorStart],
});
}
if (useRef) {
next.chunks.push({

View File

@ -22,6 +22,8 @@ const factory: PostProcessorFactory<ProcessorConfig> = (config?: ProcessorConfig
let parser: prettier.BuiltInParserName | any;
if (fileType === 'js' || fileType === 'jsx') {
parser = 'babel';
} else if (fileType === 'json') {
parser = 'json-stringify';
} else if (PARSERS.indexOf(fileType) >= 0) {
parser = fileType;
} else if (cfg.customFileTypeParser[fileType]) {

View File

@ -22,8 +22,10 @@ export enum FileType {
LESS = 'less',
HTML = 'html',
JS = 'js',
MJS = 'mjs',
JSX = 'jsx',
TS = 'ts',
MTS = 'mts',
TSX = 'tsx',
JSON = 'json',
MD = 'md',
@ -168,6 +170,7 @@ export interface IProjectBuilderOptions {
* - expr: 求值的表达式
*/
evalErrorsHandler?: string;
/**
* Hook which is used to customize original options, we can reorder/add/remove plugins/processors
* of the existing solution.

View File

@ -42,6 +42,7 @@ export interface IRouterInfo extends IWithDependency {
* project's remarks
*/
export interface ProjectRemark {
/** if current project only contain one container which type is `Component` */
isSingleComponent?: boolean;
}