mirror of
https://github.com/alibaba/lowcode-engine.git
synced 2026-01-13 09:41:57 +00:00
feat: add prettier post processor
This commit is contained in:
parent
b350a882d6
commit
49ac9a3f08
@ -14,7 +14,9 @@
|
||||
},
|
||||
"dependencies": {
|
||||
"@ali/am-eslint-config": "*",
|
||||
"@types/prettier": "^1.19.1",
|
||||
"change-case": "^3.1.0",
|
||||
"prettier": "^2.0.2",
|
||||
"short-uuid": "^3.1.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
|
||||
@ -5,6 +5,7 @@ import {
|
||||
ICompiledModule,
|
||||
IModuleBuilder,
|
||||
IResultFile,
|
||||
PostProcessor,
|
||||
} from '../types';
|
||||
|
||||
import { COMMON_SUB_MODULE_NAME } from '../const/generator';
|
||||
@ -17,9 +18,11 @@ import ResultFile from '../model/ResultFile';
|
||||
export function createModuleBuilder(
|
||||
options: {
|
||||
plugins: BuilderComponentPlugin[];
|
||||
postProcessors: PostProcessor[];
|
||||
mainFileName?: string;
|
||||
} = {
|
||||
plugins: [],
|
||||
postProcessors: [],
|
||||
},
|
||||
): IModuleBuilder {
|
||||
const chunkGenerator = new ChunkBuilder(options.plugins);
|
||||
@ -33,7 +36,7 @@ export function createModuleBuilder(
|
||||
);
|
||||
}
|
||||
|
||||
const files: IResultFile[] = [];
|
||||
let files: IResultFile[] = [];
|
||||
|
||||
const { chunks } = await chunkGenerator.run(input);
|
||||
chunks.forEach(fileChunkList => {
|
||||
@ -46,6 +49,18 @@ export function createModuleBuilder(
|
||||
files.push(file);
|
||||
});
|
||||
|
||||
if (options.postProcessors.length > 0) {
|
||||
files = files.map(file => {
|
||||
let content = file.content;
|
||||
const type = file.ext;
|
||||
options.postProcessors.forEach(processer => {
|
||||
content = processer(content, type);
|
||||
});
|
||||
|
||||
return new ResultFile(content, type, file.name);
|
||||
});
|
||||
}
|
||||
|
||||
return {
|
||||
files,
|
||||
};
|
||||
|
||||
@ -8,10 +8,11 @@ import {
|
||||
IResultDir,
|
||||
IResultFile,
|
||||
ISchemaParser,
|
||||
PostProcessor,
|
||||
} from '../types';
|
||||
|
||||
import ResultDir from '@/model/ResultDir';
|
||||
import SchemaParser from '@/parse/SchemaParser';
|
||||
import SchemaParser from '@/parser/SchemaParser';
|
||||
|
||||
import { createModuleBuilder } from '@/generator/ModuleBuilder';
|
||||
|
||||
@ -40,16 +41,20 @@ function getDirFromRoot(root: IResultDir, path: string[]): IResultDir {
|
||||
export class ProjectBuilder implements IProjectBuilder {
|
||||
private template: IProjectTemplate;
|
||||
private plugins: IProjectPlugins;
|
||||
private postProcessors: PostProcessor[];
|
||||
|
||||
constructor({
|
||||
template,
|
||||
plugins,
|
||||
postProcessors,
|
||||
}: {
|
||||
template: IProjectTemplate;
|
||||
plugins: IProjectPlugins;
|
||||
postProcessors: PostProcessor[];
|
||||
}) {
|
||||
this.template = template;
|
||||
this.plugins = plugins;
|
||||
this.postProcessors = postProcessors;
|
||||
}
|
||||
|
||||
public async generateProject(schema: IProjectSchema): Promise<IResultDir> {
|
||||
@ -212,45 +217,57 @@ export class ProjectBuilder implements IProjectBuilder {
|
||||
|
||||
builders.components = createModuleBuilder({
|
||||
plugins: this.plugins.components,
|
||||
postProcessors: this.postProcessors,
|
||||
});
|
||||
builders.pages = createModuleBuilder({
|
||||
plugins: this.plugins.pages,
|
||||
postProcessors: this.postProcessors,
|
||||
});
|
||||
builders.pages = createModuleBuilder({ plugins: this.plugins.pages });
|
||||
builders.router = createModuleBuilder({
|
||||
plugins: this.plugins.router,
|
||||
mainFileName: this.template.slots.router.fileName,
|
||||
postProcessors: this.postProcessors,
|
||||
});
|
||||
builders.entry = createModuleBuilder({
|
||||
plugins: this.plugins.entry,
|
||||
mainFileName: this.template.slots.entry.fileName,
|
||||
postProcessors: this.postProcessors,
|
||||
});
|
||||
builders.globalStyle = createModuleBuilder({
|
||||
plugins: this.plugins.globalStyle,
|
||||
mainFileName: this.template.slots.globalStyle.fileName,
|
||||
postProcessors: this.postProcessors,
|
||||
});
|
||||
builders.htmlEntry = createModuleBuilder({
|
||||
plugins: this.plugins.htmlEntry,
|
||||
mainFileName: this.template.slots.htmlEntry.fileName,
|
||||
postProcessors: this.postProcessors,
|
||||
});
|
||||
builders.packageJSON = createModuleBuilder({
|
||||
plugins: this.plugins.packageJSON,
|
||||
mainFileName: this.template.slots.packageJSON.fileName,
|
||||
postProcessors: this.postProcessors,
|
||||
});
|
||||
|
||||
if (this.template.slots.constants && this.plugins.constants) {
|
||||
builders.constants = createModuleBuilder({
|
||||
plugins: this.plugins.constants,
|
||||
mainFileName: this.template.slots.constants.fileName,
|
||||
postProcessors: this.postProcessors,
|
||||
});
|
||||
}
|
||||
if (this.template.slots.utils && this.plugins.utils) {
|
||||
builders.utils = createModuleBuilder({
|
||||
plugins: this.plugins.utils,
|
||||
mainFileName: this.template.slots.utils.fileName,
|
||||
postProcessors: this.postProcessors,
|
||||
});
|
||||
}
|
||||
if (this.template.slots.i18n && this.plugins.i18n) {
|
||||
builders.i18n = createModuleBuilder({
|
||||
plugins: this.plugins.i18n,
|
||||
mainFileName: this.template.slots.i18n.fileName,
|
||||
postProcessors: this.postProcessors,
|
||||
});
|
||||
}
|
||||
|
||||
@ -261,12 +278,15 @@ export class ProjectBuilder implements IProjectBuilder {
|
||||
export function createProjectBuilder({
|
||||
template,
|
||||
plugins,
|
||||
postProcessors,
|
||||
}: {
|
||||
template: IProjectTemplate;
|
||||
plugins: IProjectPlugins;
|
||||
postProcessors: PostProcessor[];
|
||||
}): IProjectBuilder {
|
||||
return new ProjectBuilder({
|
||||
template,
|
||||
plugins,
|
||||
postProcessors,
|
||||
});
|
||||
}
|
||||
|
||||
3
packages/code-generator/src/postprocessor/index.ts
Normal file
3
packages/code-generator/src/postprocessor/index.ts
Normal file
@ -0,0 +1,3 @@
|
||||
import prettier from './prettier';
|
||||
|
||||
export { prettier };
|
||||
22
packages/code-generator/src/postprocessor/prettier/index.ts
Normal file
22
packages/code-generator/src/postprocessor/prettier/index.ts
Normal file
@ -0,0 +1,22 @@
|
||||
import prettier from 'prettier';
|
||||
|
||||
import { PostProcessor } from '@/types';
|
||||
|
||||
const PARSERS = ['css', 'scss', 'less', 'json', 'html', 'vue'];
|
||||
|
||||
const codePrettier: PostProcessor = (content: string, fileType: string) => {
|
||||
let parser: prettier.BuiltInParserName;
|
||||
if (fileType === 'js' || fileType === 'jsx') {
|
||||
parser = 'babel';
|
||||
} else if (PARSERS.indexOf(fileType) >= 0) {
|
||||
parser = fileType as prettier.BuiltInParserName;
|
||||
} else {
|
||||
return content;
|
||||
}
|
||||
|
||||
return prettier.format(content, {
|
||||
parser,
|
||||
});
|
||||
};
|
||||
|
||||
export default codePrettier;
|
||||
@ -21,6 +21,8 @@ import template from '@/plugins/project/framework/icejs/template';
|
||||
import i18n from '@/plugins/project/i18n';
|
||||
import utils from '@/plugins/project/utils';
|
||||
|
||||
import { prettier } from '@/postprocessor';
|
||||
|
||||
export default function createIceJsProjectBuilder(): IProjectBuilder {
|
||||
return createProjectBuilder({
|
||||
template,
|
||||
@ -56,5 +58,6 @@ export default function createIceJsProjectBuilder(): IProjectBuilder {
|
||||
htmlEntry: [iceJsEntryHtml],
|
||||
packageJSON: [iceJsPackageJSON],
|
||||
},
|
||||
postProcessors: [prettier],
|
||||
});
|
||||
}
|
||||
|
||||
@ -141,3 +141,5 @@ export interface IProjectPlugins {
|
||||
export interface IProjectBuilder {
|
||||
generateProject(schema: IProjectSchema): Promise<IResultDir>;
|
||||
}
|
||||
|
||||
export type PostProcessor = (content: string, fileType: string) => string;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user