fix: factory api

This commit is contained in:
春希 2020-04-20 14:27:51 +08:00
parent d6855e2236
commit 237b86656b
24 changed files with 860 additions and 769 deletions

View File

@ -2,6 +2,7 @@ import { COMMON_CHUNK_NAME } from '../../const/generator';
import {
BuilderComponentPlugin,
BuilderComponentPluginFactory,
ChunkType,
CodeGeneratorError,
DependencyType,
@ -41,7 +42,7 @@ function groupDepsByPack(deps: IDependency[]): Record<string, IDependency[]> {
function buildPackageImport(
pkg: string,
deps: IDependency[],
isJSX: boolean,
targetFileType: string,
): ICodeChunk[] {
const chunks: ICodeChunk[] = [];
let defaultImport: string = '';
@ -58,7 +59,7 @@ function buildPackageImport(
if (dep.subName) {
chunks.push({
type: ChunkType.STRING,
fileType: isJSX ? FileType.JSX : FileType.JS,
fileType: targetFileType,
name: COMMON_CHUNK_NAME.FileVarDefine,
content: `const ${targetName} = ${srcName}.${dep.subName};`,
linkAfter: [
@ -103,7 +104,7 @@ function buildPackageImport(
statementL.push(`'@/${(deps[0] as IInternalDependency).type}/${pkg}';`);
chunks.push({
type: ChunkType.STRING,
fileType: isJSX ? FileType.JSX : FileType.JS,
fileType: targetFileType,
name: COMMON_CHUNK_NAME.InternalDepsImport,
content: statementL.join(' '),
linkAfter: [COMMON_CHUNK_NAME.ExternalDepsImport],
@ -112,7 +113,7 @@ function buildPackageImport(
statementL.push(`'${pkg}';`);
chunks.push({
type: ChunkType.STRING,
fileType: isJSX ? FileType.JSX : FileType.JS,
fileType: targetFileType,
name: COMMON_CHUNK_NAME.ExternalDepsImport,
content: statementL.join(' '),
linkAfter: [],
@ -122,20 +123,28 @@ function buildPackageImport(
return chunks;
}
interface PluginConfig {
fileType: string;
}
const pluginFactory: BuilderComponentPluginFactory<PluginConfig> = (config?: PluginConfig) => {
const cfg: PluginConfig = {
fileType: FileType.JS,
...config,
};
const plugin: BuilderComponentPlugin = async (pre: ICodeStruct) => {
const next: ICodeStruct = {
...pre,
};
const isJSX = next.chunks.some(chunk => chunk.fileType === FileType.JSX);
const ir = next.ir as IWithDependency;
if (ir && ir.deps && ir.deps.length > 0) {
const packs = groupDepsByPack(ir.deps);
Object.keys(packs).forEach(pkg => {
const chunks = buildPackageImport(pkg, packs[pkg], isJSX);
const chunks = buildPackageImport(pkg, packs[pkg], cfg.fileType);
next.chunks.push.apply(next.chunks, chunks);
});
}
@ -143,4 +152,7 @@ const plugin: BuilderComponentPlugin = async (pre: ICodeStruct) => {
return next;
};
export default plugin;
return plugin;
};
export default pluginFactory;

View File

@ -2,12 +2,14 @@ import { COMMON_CHUNK_NAME } from '../../const/generator';
import {
BuilderComponentPlugin,
BuilderComponentPluginFactory,
ChunkType,
FileType,
ICodeStruct,
} from '../../types';
// TODO: How to merge this logic to common deps
const pluginFactory: BuilderComponentPluginFactory<unknown> = () => {
const plugin: BuilderComponentPlugin = async (pre: ICodeStruct) => {
const next: ICodeStruct = {
...pre,
@ -23,5 +25,7 @@ const plugin: BuilderComponentPlugin = async (pre: ICodeStruct) => {
return next;
};
return plugin;
};
export default plugin;
export default pluginFactory;

View File

@ -3,12 +3,14 @@ import { REACT_CHUNK_NAME } from './const';
import {
BuilderComponentPlugin,
BuilderComponentPluginFactory,
ChunkType,
FileType,
ICodeStruct,
IContainerInfo,
} from '../../../types';
const pluginFactory: BuilderComponentPluginFactory<unknown> = () => {
const plugin: BuilderComponentPlugin = async (pre: ICodeStruct) => {
const next: ICodeStruct = {
...pre,
@ -97,5 +99,7 @@ const plugin: BuilderComponentPlugin = async (pre: ICodeStruct) => {
return next;
};
return plugin;
};
export default plugin;
export default pluginFactory;

View File

@ -4,12 +4,14 @@ import { generateCompositeType } from '../../utils/compositeType';
import {
BuilderComponentPlugin,
BuilderComponentPluginFactory,
ChunkType,
FileType,
ICodeStruct,
IContainerInfo,
} from '../../../types';
const pluginFactory: BuilderComponentPluginFactory<unknown> = () => {
const plugin: BuilderComponentPlugin = async (pre: ICodeStruct) => {
const next: ICodeStruct = {
...pre,
@ -35,5 +37,7 @@ const plugin: BuilderComponentPlugin = async (pre: ICodeStruct) => {
return next;
};
return plugin;
};
export default plugin;
export default pluginFactory;

View File

@ -4,12 +4,14 @@ import { generateCompositeType } from '../../utils/compositeType';
import {
BuilderComponentPlugin,
BuilderComponentPluginFactory,
ChunkType,
FileType,
ICodeStruct,
IContainerInfo,
} from '../../../types';
const pluginFactory: BuilderComponentPluginFactory<unknown> = () => {
const plugin: BuilderComponentPlugin = async (pre: ICodeStruct) => {
const next: ICodeStruct = {
...pre,
@ -35,5 +37,7 @@ const plugin: BuilderComponentPlugin = async (pre: ICodeStruct) => {
return next;
};
return plugin;
};
export default plugin;
export default pluginFactory;

View File

@ -2,11 +2,13 @@ import { REACT_CHUNK_NAME } from './const';
import {
BuilderComponentPlugin,
BuilderComponentPluginFactory,
ChunkType,
FileType,
ICodeStruct,
} from '../../../types';
const pluginFactory: BuilderComponentPluginFactory<unknown> = () => {
const plugin: BuilderComponentPlugin = async (pre: ICodeStruct) => {
const next: ICodeStruct = {
...pre,
@ -22,5 +24,7 @@ const plugin: BuilderComponentPlugin = async (pre: ICodeStruct) => {
return next;
};
return plugin;
};
export default plugin;
export default pluginFactory;

View File

@ -7,6 +7,7 @@ import {
import {
BuilderComponentPlugin,
BuilderComponentPluginFactory,
ChunkType,
CodeGeneratorError,
FileType,
@ -16,6 +17,7 @@ import {
IJSExpression,
} from '../../../types';
const pluginFactory: BuilderComponentPluginFactory<unknown> = () => {
const plugin: BuilderComponentPlugin = async (pre: ICodeStruct) => {
const next: ICodeStruct = {
...pre,
@ -77,5 +79,7 @@ const plugin: BuilderComponentPlugin = async (pre: ICodeStruct) => {
return next;
};
return plugin;
};
export default plugin;
export default pluginFactory;

View File

@ -4,6 +4,7 @@ import { transformFuncExpr2MethodMember } from '../../utils/jsExpression';
import {
BuilderComponentPlugin,
BuilderComponentPluginFactory,
ChunkType,
FileType,
ICodeChunk,
@ -12,6 +13,7 @@ import {
IJSExpression,
} from '../../../types';
const pluginFactory: BuilderComponentPluginFactory<unknown> = () => {
const plugin: BuilderComponentPlugin = async (pre: ICodeStruct) => {
const next: ICodeStruct = {
...pre,
@ -41,5 +43,7 @@ const plugin: BuilderComponentPlugin = async (pre: ICodeStruct) => {
return next;
};
return plugin;
};
export default plugin;
export default pluginFactory;

View File

@ -1,5 +1,6 @@
import {
BuilderComponentPlugin,
BuilderComponentPluginFactory,
ChildNodeItem,
ChildNodeType,
ChunkType,
@ -111,6 +112,7 @@ function generateChildren(children: ChildNodeType): string[] {
});
}
const pluginFactory: BuilderComponentPluginFactory<unknown> = () => {
const plugin: BuilderComponentPlugin = async (pre: ICodeStruct) => {
const next: ICodeStruct = {
...pre,
@ -145,5 +147,7 @@ const plugin: BuilderComponentPlugin = async (pre: ICodeStruct) => {
return next;
};
return plugin;
};
export default plugin;
export default pluginFactory;

View File

@ -2,6 +2,7 @@ import { COMMON_CHUNK_NAME } from '../../../const/generator';
import {
BuilderComponentPlugin,
BuilderComponentPluginFactory,
ChunkType,
FileType,
ICodeStruct,
@ -9,6 +10,7 @@ import {
} from '../../../types';
// TODO: How to merge this logic to common deps
const pluginFactory: BuilderComponentPluginFactory<unknown> = () => {
const plugin: BuilderComponentPlugin = async (pre: ICodeStruct) => {
const next: ICodeStruct = {
...pre,
@ -24,5 +26,7 @@ const plugin: BuilderComponentPlugin = async (pre: ICodeStruct) => {
return next;
};
return plugin;
};
export default plugin;
export default pluginFactory;

View File

@ -2,12 +2,14 @@ import { COMMON_CHUNK_NAME } from '../../../const/generator';
import {
BuilderComponentPlugin,
BuilderComponentPluginFactory,
ChunkType,
FileType,
ICodeStruct,
IContainerInfo,
} from '../../../types';
const pluginFactory: BuilderComponentPluginFactory<unknown> = () => {
const plugin: BuilderComponentPlugin = async (pre: ICodeStruct) => {
const next: ICodeStruct = {
...pre,
@ -33,5 +35,7 @@ const plugin: BuilderComponentPlugin = async (pre: ICodeStruct) => {
return next;
};
return plugin;
};
export default plugin;
export default pluginFactory;

View File

@ -2,6 +2,7 @@ import { COMMON_CHUNK_NAME } from '../../const/generator';
import { generateCompositeType } from '../../plugins/utils/compositeType';
import {
BuilderComponentPlugin,
BuilderComponentPluginFactory,
ChunkType,
FileType,
ICodeStruct,
@ -9,6 +10,7 @@ import {
} from '../../types';
// TODO: How to merge this logic to common deps
const pluginFactory: BuilderComponentPluginFactory<unknown> = () => {
const plugin: BuilderComponentPlugin = async (pre: ICodeStruct) => {
const next: ICodeStruct = {
...pre,
@ -50,5 +52,7 @@ const plugin: BuilderComponentPlugin = async (pre: ICodeStruct) => {
return next;
};
return plugin;
};
export default plugin;
export default pluginFactory;

View File

@ -2,6 +2,7 @@ import { COMMON_CHUNK_NAME } from '../../../../../const/generator';
import {
BuilderComponentPlugin,
BuilderComponentPluginFactory,
ChunkType,
FileType,
ICodeStruct,
@ -9,6 +10,7 @@ import {
} from '../../../../../types';
// TODO: How to merge this logic to common deps
const pluginFactory: BuilderComponentPluginFactory<unknown> = () => {
const plugin: BuilderComponentPlugin = async (pre: ICodeStruct) => {
const next: ICodeStruct = {
...pre,
@ -51,5 +53,7 @@ const plugin: BuilderComponentPlugin = async (pre: ICodeStruct) => {
return next;
};
return plugin;
};
export default plugin;
export default pluginFactory;

View File

@ -2,6 +2,7 @@ import { COMMON_CHUNK_NAME } from '../../../../../const/generator';
import {
BuilderComponentPlugin,
BuilderComponentPluginFactory,
ChunkType,
FileType,
ICodeStruct,
@ -9,6 +10,7 @@ import {
} from '../../../../../types';
// TODO: How to merge this logic to common deps
const pluginFactory: BuilderComponentPluginFactory<unknown> = () => {
const plugin: BuilderComponentPlugin = async (pre: ICodeStruct) => {
const next: ICodeStruct = {
...pre,
@ -39,5 +41,7 @@ const plugin: BuilderComponentPlugin = async (pre: ICodeStruct) => {
return next;
};
return plugin;
};
export default plugin;
export default pluginFactory;

View File

@ -2,6 +2,7 @@ import { COMMON_CHUNK_NAME } from '../../../../../const/generator';
import {
BuilderComponentPlugin,
BuilderComponentPluginFactory,
ChunkType,
FileType,
ICodeStruct,
@ -9,6 +10,7 @@ import {
} from '../../../../../types';
// TODO: How to merge this logic to common deps
const pluginFactory: BuilderComponentPluginFactory<unknown> = () => {
const plugin: BuilderComponentPlugin = async (pre: ICodeStruct) => {
const next: ICodeStruct = {
...pre,
@ -49,5 +51,7 @@ const plugin: BuilderComponentPlugin = async (pre: ICodeStruct) => {
return next;
};
return plugin;
};
export default plugin;
export default pluginFactory;

View File

@ -2,6 +2,7 @@ import { COMMON_CHUNK_NAME } from '../../../../../const/generator';
import {
BuilderComponentPlugin,
BuilderComponentPluginFactory,
ChunkType,
FileType,
ICodeStruct,
@ -21,6 +22,7 @@ interface IIceJsPackageJSON extends IPackageJSON {
}
// TODO: How to merge this logic to common deps
const pluginFactory: BuilderComponentPluginFactory<unknown> = () => {
const plugin: BuilderComponentPlugin = async (pre: ICodeStruct) => {
const next: ICodeStruct = {
...pre,
@ -82,5 +84,7 @@ const plugin: BuilderComponentPlugin = async (pre: ICodeStruct) => {
return next;
};
return plugin;
};
export default plugin;
export default pluginFactory;

View File

@ -2,6 +2,7 @@ import { COMMON_CHUNK_NAME } from '../../../../../const/generator';
import {
BuilderComponentPlugin,
BuilderComponentPluginFactory,
ChunkType,
FileType,
ICodeStruct,
@ -9,6 +10,7 @@ import {
} from '../../../../../types';
// TODO: How to merge this logic to common deps
const pluginFactory: BuilderComponentPluginFactory<unknown> = () => {
const plugin: BuilderComponentPlugin = async (pre: ICodeStruct) => {
const next: ICodeStruct = {
...pre,
@ -75,5 +77,7 @@ const plugin: BuilderComponentPlugin = async (pre: ICodeStruct) => {
return next;
};
return plugin;
};
export default plugin;
export default pluginFactory;

View File

@ -2,6 +2,7 @@ import { COMMON_CHUNK_NAME } from '../../const/generator';
import { generateCompositeType } from '../../plugins/utils/compositeType';
import {
BuilderComponentPlugin,
BuilderComponentPluginFactory,
ChunkType,
FileType,
ICodeStruct,
@ -9,6 +10,7 @@ import {
} from '../../types';
// TODO: How to merge this logic to common deps
const pluginFactory: BuilderComponentPluginFactory<unknown> = () => {
const plugin: BuilderComponentPlugin = async (pre: ICodeStruct) => {
const next: ICodeStruct = {
...pre,
@ -62,5 +64,7 @@ const plugin: BuilderComponentPlugin = async (pre: ICodeStruct) => {
return next;
};
return plugin;
};
export default plugin;
export default pluginFactory;

View File

@ -2,6 +2,7 @@ import { COMMON_CHUNK_NAME } from '../../const/generator';
import {
BuilderComponentPlugin,
BuilderComponentPluginFactory,
ChunkType,
FileType,
ICodeStruct,
@ -9,6 +10,7 @@ import {
} from '../../types';
// TODO: How to merge this logic to common deps
const pluginFactory: BuilderComponentPluginFactory<unknown> = () => {
const plugin: BuilderComponentPlugin = async (pre: ICodeStruct) => {
const next: ICodeStruct = {
...pre,
@ -85,5 +87,7 @@ const plugin: BuilderComponentPlugin = async (pre: ICodeStruct) => {
return next;
};
return plugin;
};
export default plugin;
export default pluginFactory;

View File

@ -1,12 +1,13 @@
import prettier from 'prettier';
import { PostProcessor } from '../../types';
import { PostProcessor, PostProcessorFactory } from '../../types';
const PARSERS = ['css', 'scss', 'less', 'json', 'html', 'vue'];
const factory: PostProcessorFactory<unknown> = () => {
const codePrettier: PostProcessor = (content: string, fileType: string) => {
let parser: prettier.BuiltInParserName;
if (fileType === 'js' || fileType === 'jsx') {
if (fileType === 'js' || fileType === 'jsx' || fileType === 'ts' || fileType === 'tsx') {
parser = 'babel';
} else if (PARSERS.indexOf(fileType) >= 0) {
parser = fileType as prettier.BuiltInParserName;
@ -19,4 +20,7 @@ const codePrettier: PostProcessor = (content: string, fileType: string) => {
});
};
export default codePrettier;
return codePrettier;
};
export default factory;

View File

@ -28,36 +28,40 @@ export default function createIceJsProjectBuilder(): IProjectBuilder {
template,
plugins: {
components: [
reactCommonDeps,
esmodule,
containerClass,
containerInjectUtils,
containerInitState,
containerLifeCycle,
containerMethod,
jsx,
css,
reactCommonDeps(),
esmodule({
fileType: 'jsx',
}),
containerClass(),
containerInjectUtils(),
containerInitState(),
containerLifeCycle(),
containerMethod(),
jsx(),
css(),
],
pages: [
reactCommonDeps,
esmodule,
containerClass,
containerInjectUtils,
containerInitState,
containerLifeCycle,
containerMethod,
jsx,
css,
reactCommonDeps(),
esmodule({
fileType: 'jsx',
}),
containerClass(),
containerInjectUtils(),
containerInitState(),
containerLifeCycle(),
containerMethod(),
jsx(),
css(),
],
router: [esmodule, iceJsRouter],
entry: [iceJsEntry],
constants: [constants],
utils: [esmodule, utils],
i18n: [i18n],
globalStyle: [iceJsGlobalStyle],
htmlEntry: [iceJsEntryHtml],
packageJSON: [iceJsPackageJSON],
router: [esmodule(), iceJsRouter()],
entry: [iceJsEntry()],
constants: [constants()],
utils: [esmodule(), utils()],
i18n: [i18n()],
globalStyle: [iceJsGlobalStyle()],
htmlEntry: [iceJsEntryHtml()],
packageJSON: [iceJsPackageJSON()],
},
postProcessors: [prettier],
postProcessors: [prettier()],
});
}

View File

@ -12,6 +12,8 @@ export enum FileType {
HTML = 'html',
JS = 'js',
JSX = 'jsx',
TS = 'ts',
TSX = 'tsx',
JSON = 'json',
}
@ -32,7 +34,7 @@ export type CodeGeneratorFunction<T> = (content: T) => string;
export interface ICodeChunk {
type: ChunkType;
fileType: FileType;
fileType: string;
name: string;
subModule?: string;
content: ChunkContent;
@ -53,6 +55,8 @@ export type BuilderComponentPlugin = (
initStruct: ICodeStruct,
) => Promise<ICodeStruct>;
export type BuilderComponentPluginFactory<T> = (config?: T) => BuilderComponentPlugin;
export interface IChunkBuilder {
run(
ir: any,
@ -142,6 +146,7 @@ export interface IProjectBuilder {
generateProject(schema: IProjectSchema): Promise<IResultDir>;
}
export type PostProcessorFactory<T> = (config?: T) => PostProcessor;
export type PostProcessor = (content: string, fileType: string) => string;
// TODO: temp interface, need modify

View File

@ -144,14 +144,7 @@ export interface IContainerNodeItem extends IComponentNodeItem {
* componentWillUnmount()
* componentDidCatch(error, info)
*/
lifeCycles?: {
constructor?: IJSExpression;
render?: IJSExpression;
componentDidMount?: IJSExpression;
componentDidUpdate?: IJSExpression;
componentWillUnmount?: IJSExpression;
componentDidCatch?: IJSExpression;
}; // 生命周期Hook方法
lifeCycles?: Record<string, IJSExpression>; // 生命周期Hook方法
methods?: {
[methodName: string]: IJSExpression;
}; // 自定义方法设置

View File

@ -5,6 +5,7 @@
"lib": [
"es6"
],
"module": "commonjs",
"types": ["node"],
"baseUrl": ".", /* Base directory to resolve non-absolute module names. */
},