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

View File

@ -2,26 +2,30 @@ import { COMMON_CHUNK_NAME } from '../../const/generator';
import { import {
BuilderComponentPlugin, BuilderComponentPlugin,
BuilderComponentPluginFactory,
ChunkType, ChunkType,
FileType, FileType,
ICodeStruct, ICodeStruct,
} from '../../types'; } from '../../types';
// TODO: How to merge this logic to common deps // TODO: How to merge this logic to common deps
const plugin: BuilderComponentPlugin = async (pre: ICodeStruct) => { const pluginFactory: BuilderComponentPluginFactory<unknown> = () => {
const next: ICodeStruct = { const plugin: BuilderComponentPlugin = async (pre: ICodeStruct) => {
...pre, const next: ICodeStruct = {
...pre,
};
next.chunks.push({
type: ChunkType.STRING,
fileType: FileType.JSX,
name: COMMON_CHUNK_NAME.InternalDepsImport,
content: `import * from 'react';`,
linkAfter: [],
});
return next;
}; };
return plugin;
next.chunks.push({
type: ChunkType.STRING,
fileType: FileType.JSX,
name: COMMON_CHUNK_NAME.InternalDepsImport,
content: `import * from 'react';`,
linkAfter: [],
});
return next;
}; };
export default plugin; export default pluginFactory;

View File

@ -3,99 +3,103 @@ import { REACT_CHUNK_NAME } from './const';
import { import {
BuilderComponentPlugin, BuilderComponentPlugin,
BuilderComponentPluginFactory,
ChunkType, ChunkType,
FileType, FileType,
ICodeStruct, ICodeStruct,
IContainerInfo, IContainerInfo,
} from '../../../types'; } from '../../../types';
const plugin: BuilderComponentPlugin = async (pre: ICodeStruct) => { const pluginFactory: BuilderComponentPluginFactory<unknown> = () => {
const next: ICodeStruct = { const plugin: BuilderComponentPlugin = async (pre: ICodeStruct) => {
...pre, const next: ICodeStruct = {
...pre,
};
const ir = next.ir as IContainerInfo;
next.chunks.push({
type: ChunkType.STRING,
fileType: FileType.JSX,
name: REACT_CHUNK_NAME.ClassStart,
content: `class ${ir.componentName} extends React.Component {`,
linkAfter: [
COMMON_CHUNK_NAME.ExternalDepsImport,
COMMON_CHUNK_NAME.InternalDepsImport,
COMMON_CHUNK_NAME.FileVarDefine,
COMMON_CHUNK_NAME.FileUtilDefine,
],
});
next.chunks.push({
type: ChunkType.STRING,
fileType: FileType.JSX,
name: REACT_CHUNK_NAME.ClassEnd,
content: `}`,
linkAfter: [REACT_CHUNK_NAME.ClassStart, REACT_CHUNK_NAME.ClassRenderEnd],
});
next.chunks.push({
type: ChunkType.STRING,
fileType: FileType.JSX,
name: REACT_CHUNK_NAME.ClassConstructorStart,
content: 'constructor(props, context) { super(props); ',
linkAfter: [REACT_CHUNK_NAME.ClassStart],
});
next.chunks.push({
type: ChunkType.STRING,
fileType: FileType.JSX,
name: REACT_CHUNK_NAME.ClassConstructorEnd,
content: '}',
linkAfter: [
REACT_CHUNK_NAME.ClassConstructorStart,
REACT_CHUNK_NAME.ClassConstructorContent,
],
});
next.chunks.push({
type: ChunkType.STRING,
fileType: FileType.JSX,
name: REACT_CHUNK_NAME.ClassRenderStart,
content: 'render() {',
linkAfter: [
REACT_CHUNK_NAME.ClassStart,
REACT_CHUNK_NAME.ClassConstructorEnd,
REACT_CHUNK_NAME.ClassLifeCycle,
REACT_CHUNK_NAME.ClassMethod,
],
});
next.chunks.push({
type: ChunkType.STRING,
fileType: FileType.JSX,
name: REACT_CHUNK_NAME.ClassRenderEnd,
content: '}',
linkAfter: [
REACT_CHUNK_NAME.ClassRenderStart,
REACT_CHUNK_NAME.ClassRenderPre,
REACT_CHUNK_NAME.ClassRenderJSX,
],
});
next.chunks.push({
type: ChunkType.STRING,
fileType: FileType.JSX,
name: COMMON_CHUNK_NAME.FileExport,
content: `export default ${ir.componentName};`,
linkAfter: [
COMMON_CHUNK_NAME.ExternalDepsImport,
COMMON_CHUNK_NAME.InternalDepsImport,
COMMON_CHUNK_NAME.FileVarDefine,
COMMON_CHUNK_NAME.FileUtilDefine,
REACT_CHUNK_NAME.ClassEnd,
],
});
return next;
}; };
return plugin;
const ir = next.ir as IContainerInfo;
next.chunks.push({
type: ChunkType.STRING,
fileType: FileType.JSX,
name: REACT_CHUNK_NAME.ClassStart,
content: `class ${ir.componentName} extends React.Component {`,
linkAfter: [
COMMON_CHUNK_NAME.ExternalDepsImport,
COMMON_CHUNK_NAME.InternalDepsImport,
COMMON_CHUNK_NAME.FileVarDefine,
COMMON_CHUNK_NAME.FileUtilDefine,
],
});
next.chunks.push({
type: ChunkType.STRING,
fileType: FileType.JSX,
name: REACT_CHUNK_NAME.ClassEnd,
content: `}`,
linkAfter: [REACT_CHUNK_NAME.ClassStart, REACT_CHUNK_NAME.ClassRenderEnd],
});
next.chunks.push({
type: ChunkType.STRING,
fileType: FileType.JSX,
name: REACT_CHUNK_NAME.ClassConstructorStart,
content: 'constructor(props, context) { super(props); ',
linkAfter: [REACT_CHUNK_NAME.ClassStart],
});
next.chunks.push({
type: ChunkType.STRING,
fileType: FileType.JSX,
name: REACT_CHUNK_NAME.ClassConstructorEnd,
content: '}',
linkAfter: [
REACT_CHUNK_NAME.ClassConstructorStart,
REACT_CHUNK_NAME.ClassConstructorContent,
],
});
next.chunks.push({
type: ChunkType.STRING,
fileType: FileType.JSX,
name: REACT_CHUNK_NAME.ClassRenderStart,
content: 'render() {',
linkAfter: [
REACT_CHUNK_NAME.ClassStart,
REACT_CHUNK_NAME.ClassConstructorEnd,
REACT_CHUNK_NAME.ClassLifeCycle,
REACT_CHUNK_NAME.ClassMethod,
],
});
next.chunks.push({
type: ChunkType.STRING,
fileType: FileType.JSX,
name: REACT_CHUNK_NAME.ClassRenderEnd,
content: '}',
linkAfter: [
REACT_CHUNK_NAME.ClassRenderStart,
REACT_CHUNK_NAME.ClassRenderPre,
REACT_CHUNK_NAME.ClassRenderJSX,
],
});
next.chunks.push({
type: ChunkType.STRING,
fileType: FileType.JSX,
name: COMMON_CHUNK_NAME.FileExport,
content: `export default ${ir.componentName};`,
linkAfter: [
COMMON_CHUNK_NAME.ExternalDepsImport,
COMMON_CHUNK_NAME.InternalDepsImport,
COMMON_CHUNK_NAME.FileVarDefine,
COMMON_CHUNK_NAME.FileUtilDefine,
REACT_CHUNK_NAME.ClassEnd,
],
});
return next;
}; };
export default plugin; export default pluginFactory;

View File

@ -4,36 +4,40 @@ import { generateCompositeType } from '../../utils/compositeType';
import { import {
BuilderComponentPlugin, BuilderComponentPlugin,
BuilderComponentPluginFactory,
ChunkType, ChunkType,
FileType, FileType,
ICodeStruct, ICodeStruct,
IContainerInfo, IContainerInfo,
} from '../../../types'; } from '../../../types';
const plugin: BuilderComponentPlugin = async (pre: ICodeStruct) => { const pluginFactory: BuilderComponentPluginFactory<unknown> = () => {
const next: ICodeStruct = { const plugin: BuilderComponentPlugin = async (pre: ICodeStruct) => {
...pre, const next: ICodeStruct = {
...pre,
};
const ir = next.ir as IContainerInfo;
if (ir.state) {
const state = ir.state;
const fields = Object.keys(state).map<string>(stateName => {
const [isString, value] = generateCompositeType(state[stateName]);
return `${stateName}: ${isString ? `'${value}'` : value},`;
});
next.chunks.push({
type: ChunkType.STRING,
fileType: FileType.JSX,
name: REACT_CHUNK_NAME.ClassConstructorContent,
content: `this.state = { ${fields.join('')} };`,
linkAfter: [REACT_CHUNK_NAME.ClassConstructorStart],
});
}
return next;
}; };
return plugin;
const ir = next.ir as IContainerInfo;
if (ir.state) {
const state = ir.state;
const fields = Object.keys(state).map<string>(stateName => {
const [isString, value] = generateCompositeType(state[stateName]);
return `${stateName}: ${isString ? `'${value}'` : value},`;
});
next.chunks.push({
type: ChunkType.STRING,
fileType: FileType.JSX,
name: REACT_CHUNK_NAME.ClassConstructorContent,
content: `this.state = { ${fields.join('')} };`,
linkAfter: [REACT_CHUNK_NAME.ClassConstructorStart],
});
}
return next;
}; };
export default plugin; export default pluginFactory;

View File

@ -4,36 +4,40 @@ import { generateCompositeType } from '../../utils/compositeType';
import { import {
BuilderComponentPlugin, BuilderComponentPlugin,
BuilderComponentPluginFactory,
ChunkType, ChunkType,
FileType, FileType,
ICodeStruct, ICodeStruct,
IContainerInfo, IContainerInfo,
} from '../../../types'; } from '../../../types';
const plugin: BuilderComponentPlugin = async (pre: ICodeStruct) => { const pluginFactory: BuilderComponentPluginFactory<unknown> = () => {
const next: ICodeStruct = { const plugin: BuilderComponentPlugin = async (pre: ICodeStruct) => {
...pre, const next: ICodeStruct = {
...pre,
};
const ir = next.ir as IContainerInfo;
if (ir.state) {
const state = ir.state;
const fields = Object.keys(state).map<string>(stateName => {
const [isString, value] = generateCompositeType(state[stateName]);
return `${stateName}: ${isString ? `'${value}'` : value},`;
});
next.chunks.push({
type: ChunkType.STRING,
fileType: FileType.JSX,
name: REACT_CHUNK_NAME.ClassConstructorContent,
content: `this.state = { ${fields.join('')} };`,
linkAfter: [REACT_CHUNK_NAME.ClassConstructorStart],
});
}
return next;
}; };
return plugin;
const ir = next.ir as IContainerInfo;
if (ir.state) {
const state = ir.state;
const fields = Object.keys(state).map<string>(stateName => {
const [isString, value] = generateCompositeType(state[stateName]);
return `${stateName}: ${isString ? `'${value}'` : value},`;
});
next.chunks.push({
type: ChunkType.STRING,
fileType: FileType.JSX,
name: REACT_CHUNK_NAME.ClassConstructorContent,
content: `this.state = { ${fields.join('')} };`,
linkAfter: [REACT_CHUNK_NAME.ClassConstructorStart],
});
}
return next;
}; };
export default plugin; export default pluginFactory;

View File

@ -2,25 +2,29 @@ import { REACT_CHUNK_NAME } from './const';
import { import {
BuilderComponentPlugin, BuilderComponentPlugin,
BuilderComponentPluginFactory,
ChunkType, ChunkType,
FileType, FileType,
ICodeStruct, ICodeStruct,
} from '../../../types'; } from '../../../types';
const plugin: BuilderComponentPlugin = async (pre: ICodeStruct) => { const pluginFactory: BuilderComponentPluginFactory<unknown> = () => {
const next: ICodeStruct = { const plugin: BuilderComponentPlugin = async (pre: ICodeStruct) => {
...pre, const next: ICodeStruct = {
...pre,
};
next.chunks.push({
type: ChunkType.STRING,
fileType: FileType.JSX,
name: REACT_CHUNK_NAME.ClassConstructorContent,
content: `this.utils = utils;`,
linkAfter: [REACT_CHUNK_NAME.ClassConstructorStart],
});
return next;
}; };
return plugin;
next.chunks.push({
type: ChunkType.STRING,
fileType: FileType.JSX,
name: REACT_CHUNK_NAME.ClassConstructorContent,
content: `this.utils = utils;`,
linkAfter: [REACT_CHUNK_NAME.ClassConstructorStart],
});
return next;
}; };
export default plugin; export default pluginFactory;

View File

@ -7,6 +7,7 @@ import {
import { import {
BuilderComponentPlugin, BuilderComponentPlugin,
BuilderComponentPluginFactory,
ChunkType, ChunkType,
CodeGeneratorError, CodeGeneratorError,
FileType, FileType,
@ -16,66 +17,69 @@ import {
IJSExpression, IJSExpression,
} from '../../../types'; } from '../../../types';
const plugin: BuilderComponentPlugin = async (pre: ICodeStruct) => { const pluginFactory: BuilderComponentPluginFactory<unknown> = () => {
const next: ICodeStruct = { const plugin: BuilderComponentPlugin = async (pre: ICodeStruct) => {
...pre, const next: ICodeStruct = {
...pre,
};
const ir = next.ir as IContainerInfo;
if (ir.lifeCycles) {
const lifeCycles = ir.lifeCycles;
const chunks = Object.keys(lifeCycles).map<ICodeChunk>(lifeCycleName => {
if (lifeCycleName === 'constructor') {
return {
type: ChunkType.STRING,
fileType: FileType.JSX,
name: REACT_CHUNK_NAME.ClassConstructorContent,
content: getFuncExprBody(
(lifeCycles[lifeCycleName] as IJSExpression).value,
),
linkAfter: [REACT_CHUNK_NAME.ClassConstructorStart],
};
}
if (lifeCycleName === 'render') {
return {
type: ChunkType.STRING,
fileType: FileType.JSX,
name: REACT_CHUNK_NAME.ClassRenderPre,
content: getFuncExprBody(
(lifeCycles[lifeCycleName] as IJSExpression).value,
),
linkAfter: [REACT_CHUNK_NAME.ClassRenderStart],
};
}
if (
lifeCycleName === 'componentDidMount' ||
lifeCycleName === 'componentDidUpdate' ||
lifeCycleName === 'componentWillUnmount' ||
lifeCycleName === 'componentDidCatch'
) {
return {
type: ChunkType.STRING,
fileType: FileType.JSX,
name: REACT_CHUNK_NAME.ClassLifeCycle,
content: transformFuncExpr2MethodMember(
lifeCycleName,
(lifeCycles[lifeCycleName] as IJSExpression).value,
),
linkAfter: [
REACT_CHUNK_NAME.ClassStart,
REACT_CHUNK_NAME.ClassConstructorEnd,
],
};
}
throw new CodeGeneratorError('Unknown life cycle method name');
});
next.chunks.push.apply(next.chunks, chunks);
}
return next;
}; };
return plugin;
const ir = next.ir as IContainerInfo;
if (ir.lifeCycles) {
const lifeCycles = ir.lifeCycles;
const chunks = Object.keys(lifeCycles).map<ICodeChunk>(lifeCycleName => {
if (lifeCycleName === 'constructor') {
return {
type: ChunkType.STRING,
fileType: FileType.JSX,
name: REACT_CHUNK_NAME.ClassConstructorContent,
content: getFuncExprBody(
(lifeCycles[lifeCycleName] as IJSExpression).value,
),
linkAfter: [REACT_CHUNK_NAME.ClassConstructorStart],
};
}
if (lifeCycleName === 'render') {
return {
type: ChunkType.STRING,
fileType: FileType.JSX,
name: REACT_CHUNK_NAME.ClassRenderPre,
content: getFuncExprBody(
(lifeCycles[lifeCycleName] as IJSExpression).value,
),
linkAfter: [REACT_CHUNK_NAME.ClassRenderStart],
};
}
if (
lifeCycleName === 'componentDidMount' ||
lifeCycleName === 'componentDidUpdate' ||
lifeCycleName === 'componentWillUnmount' ||
lifeCycleName === 'componentDidCatch'
) {
return {
type: ChunkType.STRING,
fileType: FileType.JSX,
name: REACT_CHUNK_NAME.ClassLifeCycle,
content: transformFuncExpr2MethodMember(
lifeCycleName,
(lifeCycles[lifeCycleName] as IJSExpression).value,
),
linkAfter: [
REACT_CHUNK_NAME.ClassStart,
REACT_CHUNK_NAME.ClassConstructorEnd,
],
};
}
throw new CodeGeneratorError('Unknown life cycle method name');
});
next.chunks.push.apply(next.chunks, chunks);
}
return next;
}; };
export default plugin; export default pluginFactory;

View File

@ -4,6 +4,7 @@ import { transformFuncExpr2MethodMember } from '../../utils/jsExpression';
import { import {
BuilderComponentPlugin, BuilderComponentPlugin,
BuilderComponentPluginFactory,
ChunkType, ChunkType,
FileType, FileType,
ICodeChunk, ICodeChunk,
@ -12,34 +13,37 @@ import {
IJSExpression, IJSExpression,
} from '../../../types'; } from '../../../types';
const plugin: BuilderComponentPlugin = async (pre: ICodeStruct) => { const pluginFactory: BuilderComponentPluginFactory<unknown> = () => {
const next: ICodeStruct = { const plugin: BuilderComponentPlugin = async (pre: ICodeStruct) => {
...pre, const next: ICodeStruct = {
...pre,
};
const ir = next.ir as IContainerInfo;
if (ir.methods) {
const methods = ir.methods;
const chunks = Object.keys(methods).map<ICodeChunk>(methodName => ({
type: ChunkType.STRING,
fileType: FileType.JSX,
name: REACT_CHUNK_NAME.ClassMethod,
content: transformFuncExpr2MethodMember(
methodName,
(methods[methodName] as IJSExpression).value,
),
linkAfter: [
REACT_CHUNK_NAME.ClassStart,
REACT_CHUNK_NAME.ClassConstructorEnd,
REACT_CHUNK_NAME.ClassLifeCycle,
],
}));
next.chunks.push.apply(next.chunks, chunks);
}
return next;
}; };
return plugin;
const ir = next.ir as IContainerInfo;
if (ir.methods) {
const methods = ir.methods;
const chunks = Object.keys(methods).map<ICodeChunk>(methodName => ({
type: ChunkType.STRING,
fileType: FileType.JSX,
name: REACT_CHUNK_NAME.ClassMethod,
content: transformFuncExpr2MethodMember(
methodName,
(methods[methodName] as IJSExpression).value,
),
linkAfter: [
REACT_CHUNK_NAME.ClassStart,
REACT_CHUNK_NAME.ClassConstructorEnd,
REACT_CHUNK_NAME.ClassLifeCycle,
],
}));
next.chunks.push.apply(next.chunks, chunks);
}
return next;
}; };
export default plugin; export default pluginFactory;

View File

@ -1,5 +1,6 @@
import { import {
BuilderComponentPlugin, BuilderComponentPlugin,
BuilderComponentPluginFactory,
ChildNodeItem, ChildNodeItem,
ChildNodeType, ChildNodeType,
ChunkType, ChunkType,
@ -111,39 +112,42 @@ function generateChildren(children: ChildNodeType): string[] {
}); });
} }
const plugin: BuilderComponentPlugin = async (pre: ICodeStruct) => { const pluginFactory: BuilderComponentPluginFactory<unknown> = () => {
const next: ICodeStruct = { const plugin: BuilderComponentPlugin = async (pre: ICodeStruct) => {
...pre, const next: ICodeStruct = {
}; ...pre,
};
const ir = next.ir as IContainerInfo; const ir = next.ir as IContainerInfo;
let jsxContent: string; let jsxContent: string;
if (!ir.children || (ir.children as unknown[]).length === 0) { if (!ir.children || (ir.children as unknown[]).length === 0) {
jsxContent = 'null'; jsxContent = 'null';
} else {
const childrenCode = generateChildren(ir.children);
if (childrenCode.length === 1) {
jsxContent = `(${childrenCode[0]})`;
} else { } else {
jsxContent = `(<React.Fragment>${childrenCode.join( const childrenCode = generateChildren(ir.children);
'', if (childrenCode.length === 1) {
)}</React.Fragment>)`; jsxContent = `(${childrenCode[0]})`;
} else {
jsxContent = `(<React.Fragment>${childrenCode.join(
'',
)}</React.Fragment>)`;
}
} }
}
next.chunks.push({ next.chunks.push({
type: ChunkType.STRING, type: ChunkType.STRING,
fileType: FileType.JSX, fileType: FileType.JSX,
name: REACT_CHUNK_NAME.ClassRenderJSX, name: REACT_CHUNK_NAME.ClassRenderJSX,
content: `return ${jsxContent};`, content: `return ${jsxContent};`,
linkAfter: [ linkAfter: [
REACT_CHUNK_NAME.ClassRenderStart, REACT_CHUNK_NAME.ClassRenderStart,
REACT_CHUNK_NAME.ClassRenderPre, REACT_CHUNK_NAME.ClassRenderPre,
], ],
}); });
return next; return next;
};
return plugin;
}; };
export default plugin; export default pluginFactory;

View File

@ -2,6 +2,7 @@ import { COMMON_CHUNK_NAME } from '../../../const/generator';
import { import {
BuilderComponentPlugin, BuilderComponentPlugin,
BuilderComponentPluginFactory,
ChunkType, ChunkType,
FileType, FileType,
ICodeStruct, ICodeStruct,
@ -9,20 +10,23 @@ import {
} from '../../../types'; } from '../../../types';
// TODO: How to merge this logic to common deps // TODO: How to merge this logic to common deps
const plugin: BuilderComponentPlugin = async (pre: ICodeStruct) => { const pluginFactory: BuilderComponentPluginFactory<unknown> = () => {
const next: ICodeStruct = { const plugin: BuilderComponentPlugin = async (pre: ICodeStruct) => {
...pre, const next: ICodeStruct = {
...pre,
};
next.chunks.push({
type: ChunkType.STRING,
fileType: FileType.JSX,
name: COMMON_CHUNK_NAME.ExternalDepsImport,
content: `import React from 'react';`,
linkAfter: [],
});
return next;
}; };
return plugin;
next.chunks.push({
type: ChunkType.STRING,
fileType: FileType.JSX,
name: COMMON_CHUNK_NAME.ExternalDepsImport,
content: `import React from 'react';`,
linkAfter: [],
});
return next;
}; };
export default plugin; export default pluginFactory;

View File

@ -2,36 +2,40 @@ import { COMMON_CHUNK_NAME } from '../../../const/generator';
import { import {
BuilderComponentPlugin, BuilderComponentPlugin,
BuilderComponentPluginFactory,
ChunkType, ChunkType,
FileType, FileType,
ICodeStruct, ICodeStruct,
IContainerInfo, IContainerInfo,
} from '../../../types'; } from '../../../types';
const plugin: BuilderComponentPlugin = async (pre: ICodeStruct) => { const pluginFactory: BuilderComponentPluginFactory<unknown> = () => {
const next: ICodeStruct = { const plugin: BuilderComponentPlugin = async (pre: ICodeStruct) => {
...pre, const next: ICodeStruct = {
...pre,
};
const ir = next.ir as IContainerInfo;
next.chunks.push({
type: ChunkType.STRING,
fileType: FileType.CSS,
name: COMMON_CHUNK_NAME.StyleCssContent,
content: ir.css,
linkAfter: [COMMON_CHUNK_NAME.StyleDepsImport],
});
next.chunks.push({
type: ChunkType.STRING,
fileType: FileType.JSX,
name: COMMON_CHUNK_NAME.InternalDepsImport,
content: `import './index.css';`,
linkAfter: [COMMON_CHUNK_NAME.ExternalDepsImport],
});
return next;
}; };
return plugin;
const ir = next.ir as IContainerInfo;
next.chunks.push({
type: ChunkType.STRING,
fileType: FileType.CSS,
name: COMMON_CHUNK_NAME.StyleCssContent,
content: ir.css,
linkAfter: [COMMON_CHUNK_NAME.StyleDepsImport],
});
next.chunks.push({
type: ChunkType.STRING,
fileType: FileType.JSX,
name: COMMON_CHUNK_NAME.InternalDepsImport,
content: `import './index.css';`,
linkAfter: [COMMON_CHUNK_NAME.ExternalDepsImport],
});
return next;
}; };
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 { generateCompositeType } from '../../plugins/utils/compositeType';
import { import {
BuilderComponentPlugin, BuilderComponentPlugin,
BuilderComponentPluginFactory,
ChunkType, ChunkType,
FileType, FileType,
ICodeStruct, ICodeStruct,
@ -9,46 +10,49 @@ import {
} from '../../types'; } from '../../types';
// TODO: How to merge this logic to common deps // TODO: How to merge this logic to common deps
const plugin: BuilderComponentPlugin = async (pre: ICodeStruct) => { const pluginFactory: BuilderComponentPluginFactory<unknown> = () => {
const next: ICodeStruct = { const plugin: BuilderComponentPlugin = async (pre: ICodeStruct) => {
...pre, const next: ICodeStruct = {
...pre,
};
const ir = next.ir as IProjectInfo;
if (ir.constants) {
const [, constantStr] = generateCompositeType(ir.constants);
next.chunks.push({
type: ChunkType.STRING,
fileType: FileType.JS,
name: COMMON_CHUNK_NAME.FileVarDefine,
content: `
const constantConfig = ${constantStr};
`,
linkAfter: [
COMMON_CHUNK_NAME.ExternalDepsImport,
COMMON_CHUNK_NAME.InternalDepsImport,
],
});
next.chunks.push({
type: ChunkType.STRING,
fileType: FileType.JS,
name: COMMON_CHUNK_NAME.FileExport,
content: `
export default constantConfig;
`,
linkAfter: [
COMMON_CHUNK_NAME.ExternalDepsImport,
COMMON_CHUNK_NAME.InternalDepsImport,
COMMON_CHUNK_NAME.FileVarDefine,
COMMON_CHUNK_NAME.FileUtilDefine,
COMMON_CHUNK_NAME.FileMainContent,
],
});
}
return next;
}; };
return plugin;
const ir = next.ir as IProjectInfo;
if (ir.constants) {
const [, constantStr] = generateCompositeType(ir.constants);
next.chunks.push({
type: ChunkType.STRING,
fileType: FileType.JS,
name: COMMON_CHUNK_NAME.FileVarDefine,
content: `
const constantConfig = ${constantStr};
`,
linkAfter: [
COMMON_CHUNK_NAME.ExternalDepsImport,
COMMON_CHUNK_NAME.InternalDepsImport,
],
});
next.chunks.push({
type: ChunkType.STRING,
fileType: FileType.JS,
name: COMMON_CHUNK_NAME.FileExport,
content: `
export default constantConfig;
`,
linkAfter: [
COMMON_CHUNK_NAME.ExternalDepsImport,
COMMON_CHUNK_NAME.InternalDepsImport,
COMMON_CHUNK_NAME.FileVarDefine,
COMMON_CHUNK_NAME.FileUtilDefine,
COMMON_CHUNK_NAME.FileMainContent,
],
});
}
return next;
}; };
export default plugin; export default pluginFactory;

View File

@ -2,6 +2,7 @@ import { COMMON_CHUNK_NAME } from '../../../../../const/generator';
import { import {
BuilderComponentPlugin, BuilderComponentPlugin,
BuilderComponentPluginFactory,
ChunkType, ChunkType,
FileType, FileType,
ICodeStruct, ICodeStruct,
@ -9,47 +10,50 @@ import {
} from '../../../../../types'; } from '../../../../../types';
// TODO: How to merge this logic to common deps // TODO: How to merge this logic to common deps
const plugin: BuilderComponentPlugin = async (pre: ICodeStruct) => { const pluginFactory: BuilderComponentPluginFactory<unknown> = () => {
const next: ICodeStruct = { const plugin: BuilderComponentPlugin = async (pre: ICodeStruct) => {
...pre, const next: ICodeStruct = {
...pre,
};
const ir = next.ir as IProjectInfo;
next.chunks.push({
type: ChunkType.STRING,
fileType: FileType.JS,
name: COMMON_CHUNK_NAME.ExternalDepsImport,
content: `
import { createApp } from 'ice';
`,
linkAfter: [],
});
next.chunks.push({
type: ChunkType.STRING,
fileType: FileType.JS,
name: COMMON_CHUNK_NAME.FileMainContent,
content: `
const appConfig = {
app: {
rootId: '${ir.config.targetRootID}',
},
router: {
type: '${ir.config.historyMode}',
},
};
createApp(appConfig);
`,
linkAfter: [
COMMON_CHUNK_NAME.ExternalDepsImport,
COMMON_CHUNK_NAME.InternalDepsImport,
COMMON_CHUNK_NAME.FileVarDefine,
COMMON_CHUNK_NAME.FileUtilDefine,
],
});
return next;
}; };
return plugin;
const ir = next.ir as IProjectInfo;
next.chunks.push({
type: ChunkType.STRING,
fileType: FileType.JS,
name: COMMON_CHUNK_NAME.ExternalDepsImport,
content: `
import { createApp } from 'ice';
`,
linkAfter: [],
});
next.chunks.push({
type: ChunkType.STRING,
fileType: FileType.JS,
name: COMMON_CHUNK_NAME.FileMainContent,
content: `
const appConfig = {
app: {
rootId: '${ir.config.targetRootID}',
},
router: {
type: '${ir.config.historyMode}',
},
};
createApp(appConfig);
`,
linkAfter: [
COMMON_CHUNK_NAME.ExternalDepsImport,
COMMON_CHUNK_NAME.InternalDepsImport,
COMMON_CHUNK_NAME.FileVarDefine,
COMMON_CHUNK_NAME.FileUtilDefine,
],
});
return next;
}; };
export default plugin; export default pluginFactory;

View File

@ -2,6 +2,7 @@ import { COMMON_CHUNK_NAME } from '../../../../../const/generator';
import { import {
BuilderComponentPlugin, BuilderComponentPlugin,
BuilderComponentPluginFactory,
ChunkType, ChunkType,
FileType, FileType,
ICodeStruct, ICodeStruct,
@ -9,35 +10,38 @@ import {
} from '../../../../../types'; } from '../../../../../types';
// TODO: How to merge this logic to common deps // TODO: How to merge this logic to common deps
const plugin: BuilderComponentPlugin = async (pre: ICodeStruct) => { const pluginFactory: BuilderComponentPluginFactory<unknown> = () => {
const next: ICodeStruct = { const plugin: BuilderComponentPlugin = async (pre: ICodeStruct) => {
...pre, const next: ICodeStruct = {
...pre,
};
const ir = next.ir as IProjectInfo;
next.chunks.push({
type: ChunkType.STRING,
fileType: FileType.HTML,
name: COMMON_CHUNK_NAME.HtmlContent,
content: `
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="x-ua-compatible" content="ie=edge,chrome=1" />
<meta name="viewport" content="width=device-width" />
<title>${ir.meta.name}</title>
</head>
<body>
<div id="${ir.config.targetRootID}"></div>
</body>
</html>
`,
linkAfter: [],
});
return next;
}; };
return plugin;
const ir = next.ir as IProjectInfo;
next.chunks.push({
type: ChunkType.STRING,
fileType: FileType.HTML,
name: COMMON_CHUNK_NAME.HtmlContent,
content: `
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="x-ua-compatible" content="ie=edge,chrome=1" />
<meta name="viewport" content="width=device-width" />
<title>${ir.meta.name}</title>
</head>
<body>
<div id="${ir.config.targetRootID}"></div>
</body>
</html>
`,
linkAfter: [],
});
return next;
}; };
export default plugin; export default pluginFactory;

View File

@ -2,6 +2,7 @@ import { COMMON_CHUNK_NAME } from '../../../../../const/generator';
import { import {
BuilderComponentPlugin, BuilderComponentPlugin,
BuilderComponentPluginFactory,
ChunkType, ChunkType,
FileType, FileType,
ICodeStruct, ICodeStruct,
@ -9,45 +10,48 @@ import {
} from '../../../../../types'; } from '../../../../../types';
// TODO: How to merge this logic to common deps // TODO: How to merge this logic to common deps
const plugin: BuilderComponentPlugin = async (pre: ICodeStruct) => { const pluginFactory: BuilderComponentPluginFactory<unknown> = () => {
const next: ICodeStruct = { const plugin: BuilderComponentPlugin = async (pre: ICodeStruct) => {
...pre, const next: ICodeStruct = {
...pre,
};
const ir = next.ir as IProjectInfo;
next.chunks.push({
type: ChunkType.STRING,
fileType: FileType.SCSS,
name: COMMON_CHUNK_NAME.StyleDepsImport,
content: `
// 引入默认全局样式
@import '@alifd/next/reset.scss';
`,
linkAfter: [],
});
next.chunks.push({
type: ChunkType.STRING,
fileType: FileType.SCSS,
name: COMMON_CHUNK_NAME.StyleCssContent,
content: `
body {
-webkit-font-smoothing: antialiased;
}
`,
linkAfter: [COMMON_CHUNK_NAME.StyleDepsImport],
});
next.chunks.push({
type: ChunkType.STRING,
fileType: FileType.SCSS,
name: COMMON_CHUNK_NAME.StyleCssContent,
content: ir.css || '',
linkAfter: [COMMON_CHUNK_NAME.StyleDepsImport],
});
return next;
}; };
return plugin;
const ir = next.ir as IProjectInfo;
next.chunks.push({
type: ChunkType.STRING,
fileType: FileType.SCSS,
name: COMMON_CHUNK_NAME.StyleDepsImport,
content: `
// 引入默认全局样式
@import '@alifd/next/reset.scss';
`,
linkAfter: [],
});
next.chunks.push({
type: ChunkType.STRING,
fileType: FileType.SCSS,
name: COMMON_CHUNK_NAME.StyleCssContent,
content: `
body {
-webkit-font-smoothing: antialiased;
}
`,
linkAfter: [COMMON_CHUNK_NAME.StyleDepsImport],
});
next.chunks.push({
type: ChunkType.STRING,
fileType: FileType.SCSS,
name: COMMON_CHUNK_NAME.StyleCssContent,
content: ir.css || '',
linkAfter: [COMMON_CHUNK_NAME.StyleDepsImport],
});
return next;
}; };
export default plugin; export default pluginFactory;

View File

@ -2,6 +2,7 @@ import { COMMON_CHUNK_NAME } from '../../../../../const/generator';
import { import {
BuilderComponentPlugin, BuilderComponentPlugin,
BuilderComponentPluginFactory,
ChunkType, ChunkType,
FileType, FileType,
ICodeStruct, ICodeStruct,
@ -21,66 +22,69 @@ interface IIceJsPackageJSON extends IPackageJSON {
} }
// TODO: How to merge this logic to common deps // TODO: How to merge this logic to common deps
const plugin: BuilderComponentPlugin = async (pre: ICodeStruct) => { const pluginFactory: BuilderComponentPluginFactory<unknown> = () => {
const next: ICodeStruct = { const plugin: BuilderComponentPlugin = async (pre: ICodeStruct) => {
...pre, const next: ICodeStruct = {
...pre,
};
const ir = next.ir as IProjectInfo;
const packageJson: IIceJsPackageJSON = {
name: '@alifd/scaffold-lite-js',
version: '0.1.5',
description: '轻量级模板,使用 JavaScript仅包含基础的 Layout。',
dependencies: {
moment: '^2.24.0',
react: '^16.4.1',
'react-dom': '^16.4.1',
'@alifd/theme-design-pro': '^0.x',
},
devDependencies: {
'@ice/spec': '^1.0.0',
'build-plugin-fusion': '^0.1.0',
'build-plugin-moment-locales': '^0.1.0',
eslint: '^6.0.1',
'ice.js': '^1.0.0',
stylelint: '^13.2.0',
'@ali/build-plugin-ice-def': '^0.1.0',
},
scripts: {
start: 'icejs start',
build: 'icejs build',
lint: 'npm run eslint && npm run stylelint',
eslint: 'eslint --cache --ext .js,.jsx ./',
stylelint: 'stylelint ./**/*.scss',
},
ideMode: {
name: 'ice-react',
},
iceworks: {
type: 'react',
adapter: 'adapter-react-v3',
},
engines: {
node: '>=8.0.0',
},
repository: {
type: 'git',
url: 'http://gitlab.alibaba-inc.com/msd/leak-scan/tree/master',
},
private: true,
originTemplate: '@alifd/scaffold-lite-js',
};
next.chunks.push({
type: ChunkType.JSON,
fileType: FileType.JSON,
name: COMMON_CHUNK_NAME.FileMainContent,
content: packageJson,
linkAfter: [],
});
return next;
}; };
return plugin;
const ir = next.ir as IProjectInfo;
const packageJson: IIceJsPackageJSON = {
name: '@alifd/scaffold-lite-js',
version: '0.1.5',
description: '轻量级模板,使用 JavaScript仅包含基础的 Layout。',
dependencies: {
moment: '^2.24.0',
react: '^16.4.1',
'react-dom': '^16.4.1',
'@alifd/theme-design-pro': '^0.x',
},
devDependencies: {
'@ice/spec': '^1.0.0',
'build-plugin-fusion': '^0.1.0',
'build-plugin-moment-locales': '^0.1.0',
eslint: '^6.0.1',
'ice.js': '^1.0.0',
stylelint: '^13.2.0',
'@ali/build-plugin-ice-def': '^0.1.0',
},
scripts: {
start: 'icejs start',
build: 'icejs build',
lint: 'npm run eslint && npm run stylelint',
eslint: 'eslint --cache --ext .js,.jsx ./',
stylelint: 'stylelint ./**/*.scss',
},
ideMode: {
name: 'ice-react',
},
iceworks: {
type: 'react',
adapter: 'adapter-react-v3',
},
engines: {
node: '>=8.0.0',
},
repository: {
type: 'git',
url: 'http://gitlab.alibaba-inc.com/msd/leak-scan/tree/master',
},
private: true,
originTemplate: '@alifd/scaffold-lite-js',
};
next.chunks.push({
type: ChunkType.JSON,
fileType: FileType.JSON,
name: COMMON_CHUNK_NAME.FileMainContent,
content: packageJson,
linkAfter: [],
});
return next;
}; };
export default plugin; export default pluginFactory;

View File

@ -2,6 +2,7 @@ import { COMMON_CHUNK_NAME } from '../../../../../const/generator';
import { import {
BuilderComponentPlugin, BuilderComponentPlugin,
BuilderComponentPluginFactory,
ChunkType, ChunkType,
FileType, FileType,
ICodeStruct, ICodeStruct,
@ -9,71 +10,74 @@ import {
} from '../../../../../types'; } from '../../../../../types';
// TODO: How to merge this logic to common deps // TODO: How to merge this logic to common deps
const plugin: BuilderComponentPlugin = async (pre: ICodeStruct) => { const pluginFactory: BuilderComponentPluginFactory<unknown> = () => {
const next: ICodeStruct = { const plugin: BuilderComponentPlugin = async (pre: ICodeStruct) => {
...pre, const next: ICodeStruct = {
...pre,
};
const ir = next.ir as IRouterInfo;
next.chunks.push({
type: ChunkType.STRING,
fileType: FileType.JS,
name: COMMON_CHUNK_NAME.InternalDepsImport,
content: `
import BasicLayout from '@/layouts/BasicLayout';
`,
linkAfter: [COMMON_CHUNK_NAME.ExternalDepsImport],
});
next.chunks.push({
type: ChunkType.STRING,
fileType: FileType.JS,
name: COMMON_CHUNK_NAME.FileVarDefine,
content: `
const routerConfig = [
{
path: '/',
component: BasicLayout,
children: [
${ir.routes
.map(
route => `
{
path: '${route.path}',
component: ${route.componentName},
}
`,
)
.join(',')}
],
},
];
`,
linkAfter: [
COMMON_CHUNK_NAME.ExternalDepsImport,
COMMON_CHUNK_NAME.InternalDepsImport,
COMMON_CHUNK_NAME.FileUtilDefine,
],
});
next.chunks.push({
type: ChunkType.STRING,
fileType: FileType.JS,
name: COMMON_CHUNK_NAME.FileExport,
content: `
export default routerConfig;
`,
linkAfter: [
COMMON_CHUNK_NAME.ExternalDepsImport,
COMMON_CHUNK_NAME.InternalDepsImport,
COMMON_CHUNK_NAME.FileUtilDefine,
COMMON_CHUNK_NAME.FileVarDefine,
COMMON_CHUNK_NAME.FileMainContent,
],
});
return next;
}; };
return plugin;
const ir = next.ir as IRouterInfo;
next.chunks.push({
type: ChunkType.STRING,
fileType: FileType.JS,
name: COMMON_CHUNK_NAME.InternalDepsImport,
content: `
import BasicLayout from '@/layouts/BasicLayout';
`,
linkAfter: [COMMON_CHUNK_NAME.ExternalDepsImport],
});
next.chunks.push({
type: ChunkType.STRING,
fileType: FileType.JS,
name: COMMON_CHUNK_NAME.FileVarDefine,
content: `
const routerConfig = [
{
path: '/',
component: BasicLayout,
children: [
${ir.routes
.map(
route => `
{
path: '${route.path}',
component: ${route.componentName},
}
`,
)
.join(',')}
],
},
];
`,
linkAfter: [
COMMON_CHUNK_NAME.ExternalDepsImport,
COMMON_CHUNK_NAME.InternalDepsImport,
COMMON_CHUNK_NAME.FileUtilDefine,
],
});
next.chunks.push({
type: ChunkType.STRING,
fileType: FileType.JS,
name: COMMON_CHUNK_NAME.FileExport,
content: `
export default routerConfig;
`,
linkAfter: [
COMMON_CHUNK_NAME.ExternalDepsImport,
COMMON_CHUNK_NAME.InternalDepsImport,
COMMON_CHUNK_NAME.FileUtilDefine,
COMMON_CHUNK_NAME.FileVarDefine,
COMMON_CHUNK_NAME.FileMainContent,
],
});
return next;
}; };
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 { generateCompositeType } from '../../plugins/utils/compositeType';
import { import {
BuilderComponentPlugin, BuilderComponentPlugin,
BuilderComponentPluginFactory,
ChunkType, ChunkType,
FileType, FileType,
ICodeStruct, ICodeStruct,
@ -9,58 +10,61 @@ import {
} from '../../types'; } from '../../types';
// TODO: How to merge this logic to common deps // TODO: How to merge this logic to common deps
const plugin: BuilderComponentPlugin = async (pre: ICodeStruct) => { const pluginFactory: BuilderComponentPluginFactory<unknown> = () => {
const next: ICodeStruct = { const plugin: BuilderComponentPlugin = async (pre: ICodeStruct) => {
...pre, const next: ICodeStruct = {
...pre,
};
const ir = next.ir as IProjectInfo;
if (ir.i18n) {
const [, i18nStr] = generateCompositeType(ir.i18n);
next.chunks.push({
type: ChunkType.STRING,
fileType: FileType.JS,
name: COMMON_CHUNK_NAME.FileMainContent,
content: `
const i18nConfig = ${i18nStr};
let locale = 'en_US';
const changeLocale = (target) => {
locale = target;
};
const i18n = key => i18nConfig && i18nConfig[locale] && i18nConfig[locale][key] || '';
`,
linkAfter: [
COMMON_CHUNK_NAME.ExternalDepsImport,
COMMON_CHUNK_NAME.InternalDepsImport,
COMMON_CHUNK_NAME.FileVarDefine,
COMMON_CHUNK_NAME.FileUtilDefine,
],
});
next.chunks.push({
type: ChunkType.STRING,
fileType: FileType.JS,
name: COMMON_CHUNK_NAME.FileExport,
content: `
export {
changeLocale,
i18n,
};
`,
linkAfter: [
COMMON_CHUNK_NAME.ExternalDepsImport,
COMMON_CHUNK_NAME.InternalDepsImport,
COMMON_CHUNK_NAME.FileVarDefine,
COMMON_CHUNK_NAME.FileUtilDefine,
COMMON_CHUNK_NAME.FileMainContent,
],
});
}
return next;
}; };
return plugin;
const ir = next.ir as IProjectInfo;
if (ir.i18n) {
const [, i18nStr] = generateCompositeType(ir.i18n);
next.chunks.push({
type: ChunkType.STRING,
fileType: FileType.JS,
name: COMMON_CHUNK_NAME.FileMainContent,
content: `
const i18nConfig = ${i18nStr};
let locale = 'en_US';
const changeLocale = (target) => {
locale = target;
};
const i18n = key => i18nConfig && i18nConfig[locale] && i18nConfig[locale][key] || '';
`,
linkAfter: [
COMMON_CHUNK_NAME.ExternalDepsImport,
COMMON_CHUNK_NAME.InternalDepsImport,
COMMON_CHUNK_NAME.FileVarDefine,
COMMON_CHUNK_NAME.FileUtilDefine,
],
});
next.chunks.push({
type: ChunkType.STRING,
fileType: FileType.JS,
name: COMMON_CHUNK_NAME.FileExport,
content: `
export {
changeLocale,
i18n,
};
`,
linkAfter: [
COMMON_CHUNK_NAME.ExternalDepsImport,
COMMON_CHUNK_NAME.InternalDepsImport,
COMMON_CHUNK_NAME.FileVarDefine,
COMMON_CHUNK_NAME.FileUtilDefine,
COMMON_CHUNK_NAME.FileMainContent,
],
});
}
return next;
}; };
export default plugin; export default pluginFactory;

View File

@ -2,6 +2,7 @@ import { COMMON_CHUNK_NAME } from '../../const/generator';
import { import {
BuilderComponentPlugin, BuilderComponentPlugin,
BuilderComponentPluginFactory,
ChunkType, ChunkType,
FileType, FileType,
ICodeStruct, ICodeStruct,
@ -9,52 +10,21 @@ import {
} from '../../types'; } from '../../types';
// TODO: How to merge this logic to common deps // TODO: How to merge this logic to common deps
const plugin: BuilderComponentPlugin = async (pre: ICodeStruct) => { const pluginFactory: BuilderComponentPluginFactory<unknown> = () => {
const next: ICodeStruct = { const plugin: BuilderComponentPlugin = async (pre: ICodeStruct) => {
...pre, const next: ICodeStruct = {
}; ...pre,
};
const ir = next.ir as IUtilInfo; const ir = next.ir as IUtilInfo;
if (ir.utils) {
next.chunks.push({
type: ChunkType.STRING,
fileType: FileType.JS,
name: COMMON_CHUNK_NAME.FileExport,
content: `
export default {
`,
linkAfter: [
COMMON_CHUNK_NAME.ExternalDepsImport,
COMMON_CHUNK_NAME.InternalDepsImport,
COMMON_CHUNK_NAME.FileVarDefine,
COMMON_CHUNK_NAME.FileUtilDefine,
COMMON_CHUNK_NAME.FileMainContent,
],
});
ir.utils.forEach(util => {
if (util.type === 'function') {
next.chunks.push({
type: ChunkType.STRING,
fileType: FileType.JS,
name: COMMON_CHUNK_NAME.FileVarDefine,
content: `
const ${util.name} = ${util.content};
`,
linkAfter: [
COMMON_CHUNK_NAME.ExternalDepsImport,
COMMON_CHUNK_NAME.InternalDepsImport,
],
});
}
if (ir.utils) {
next.chunks.push({ next.chunks.push({
type: ChunkType.STRING, type: ChunkType.STRING,
fileType: FileType.JS, fileType: FileType.JS,
name: COMMON_CHUNK_NAME.FileExport, name: COMMON_CHUNK_NAME.FileExport,
content: ` content: `
${util.name}, export default {
`, `,
linkAfter: [ linkAfter: [
COMMON_CHUNK_NAME.ExternalDepsImport, COMMON_CHUNK_NAME.ExternalDepsImport,
@ -64,26 +34,60 @@ const plugin: BuilderComponentPlugin = async (pre: ICodeStruct) => {
COMMON_CHUNK_NAME.FileMainContent, COMMON_CHUNK_NAME.FileMainContent,
], ],
}); });
});
next.chunks.push({ ir.utils.forEach(util => {
type: ChunkType.STRING, if (util.type === 'function') {
fileType: FileType.JS, next.chunks.push({
name: COMMON_CHUNK_NAME.FileExport, type: ChunkType.STRING,
content: ` fileType: FileType.JS,
}; name: COMMON_CHUNK_NAME.FileVarDefine,
`, content: `
linkAfter: [ const ${util.name} = ${util.content};
COMMON_CHUNK_NAME.ExternalDepsImport, `,
COMMON_CHUNK_NAME.InternalDepsImport, linkAfter: [
COMMON_CHUNK_NAME.FileVarDefine, COMMON_CHUNK_NAME.ExternalDepsImport,
COMMON_CHUNK_NAME.FileUtilDefine, COMMON_CHUNK_NAME.InternalDepsImport,
COMMON_CHUNK_NAME.FileMainContent, ],
], });
}); }
}
return next; next.chunks.push({
type: ChunkType.STRING,
fileType: FileType.JS,
name: COMMON_CHUNK_NAME.FileExport,
content: `
${util.name},
`,
linkAfter: [
COMMON_CHUNK_NAME.ExternalDepsImport,
COMMON_CHUNK_NAME.InternalDepsImport,
COMMON_CHUNK_NAME.FileVarDefine,
COMMON_CHUNK_NAME.FileUtilDefine,
COMMON_CHUNK_NAME.FileMainContent,
],
});
});
next.chunks.push({
type: ChunkType.STRING,
fileType: FileType.JS,
name: COMMON_CHUNK_NAME.FileExport,
content: `
};
`,
linkAfter: [
COMMON_CHUNK_NAME.ExternalDepsImport,
COMMON_CHUNK_NAME.InternalDepsImport,
COMMON_CHUNK_NAME.FileVarDefine,
COMMON_CHUNK_NAME.FileUtilDefine,
COMMON_CHUNK_NAME.FileMainContent,
],
});
}
return next;
};
return plugin;
}; };
export default plugin; export default pluginFactory;

View File

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

View File

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

View File

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

View File

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

View File

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