feat: fix gaps

This commit is contained in:
春希 2020-03-17 19:02:06 +08:00
parent 26983b38c2
commit 32af3d3a3c
10 changed files with 75 additions and 33 deletions

View File

@ -17,12 +17,14 @@ function flatFiles(rootName: string | null, dir: IResultDir): IResultFile[] {
return result; return result;
} }
function displayResultInConsole(root: IResultDir): void { function displayResultInConsole(root: IResultDir, fileName?: string): void {
const files = flatFiles('.', root); const files = flatFiles('.', root);
files.forEach(file => { files.forEach(file => {
if (!fileName || fileName === file.name) {
console.log(`========== ${file.name} Start ==========`); console.log(`========== ${file.name} Start ==========`);
console.log(file.content); console.log(file.content);
console.log(`========== ${file.name} End ==========`); console.log(`========== ${file.name} End ==========`);
}
}); });
} }
@ -41,7 +43,7 @@ function main() {
const createIceJsProjectBuilder = CodeGenerator.solutions.icejs; const createIceJsProjectBuilder = CodeGenerator.solutions.icejs;
const builder = createIceJsProjectBuilder(); const builder = createIceJsProjectBuilder();
builder.generateProject(demoSchema).then(result => { builder.generateProject(demoSchema).then(result => {
// displayResultInConsole(result); // displayResultInConsole(result, '././src/routes.js');
writeResultToDisk(result, '/Users/armslave/lowcodeDemo').then(response => writeResultToDisk(result, '/Users/armslave/lowcodeDemo').then(response =>
console.log('Write to disk: ', JSON.stringify(response)), console.log('Write to disk: ', JSON.stringify(response)),
); );

View File

@ -100,7 +100,7 @@ export class ProjectBuilder implements IProjectBuilder {
); );
buildResult.push({ buildResult.push({
path: this.template.slots.pages.path, path: this.template.slots.router.path,
files, files,
}); });
} }

View File

@ -118,10 +118,13 @@ class SchemaParser implements ISchemaParser {
// 分析容器内部组件依赖 // 分析容器内部组件依赖
containers.forEach(container => { containers.forEach(container => {
if (container.children) { if (container.children) {
const depNames = this.getComponentNames(container.children); // const depNames = this.getComponentNames(container.children);
container.deps = uniqueArray<string>(depNames) // container.deps = uniqueArray<string>(depNames)
.map(depName => internalDeps[depName] || compDeps[depName]) // .map(depName => internalDeps[depName] || compDeps[depName])
.filter(dep => !!dep); // .filter(dep => !!dep);
container.deps = Object.keys(compDeps).map(
depName => compDeps[depName],
);
} }
}); });

View File

@ -38,7 +38,11 @@ function groupDepsByPack(deps: IDependency[]): Record<string, IDependency[]> {
return depMap; return depMap;
} }
function buildPackageImport(pkg: string, deps: IDependency[]): ICodeChunk[] { function buildPackageImport(
pkg: string,
deps: IDependency[],
isJSX: boolean,
): ICodeChunk[] {
const chunks: ICodeChunk[] = []; const chunks: ICodeChunk[] = [];
let defaultImport: string = ''; let defaultImport: string = '';
let defaultImportAs: string = ''; let defaultImportAs: string = '';
@ -47,11 +51,14 @@ function buildPackageImport(pkg: string, deps: IDependency[]): ICodeChunk[] {
deps.forEach(dep => { deps.forEach(dep => {
const srcName = dep.exportName; const srcName = dep.exportName;
let targetName = dep.importName || dep.exportName; let targetName = dep.importName || dep.exportName;
if (dep.subName) {
return;
}
if (dep.subName) { if (dep.subName) {
chunks.push({ chunks.push({
type: ChunkType.STRING, type: ChunkType.STRING,
fileType: FileType.JSX, fileType: isJSX ? FileType.JSX : FileType.JS,
name: COMMON_CHUNK_NAME.FileVarDefine, name: COMMON_CHUNK_NAME.FileVarDefine,
content: `const ${targetName} = ${srcName}.${dep.subName};`, content: `const ${targetName} = ${srcName}.${dep.subName};`,
linkAfter: [ linkAfter: [
@ -93,10 +100,10 @@ function buildPackageImport(pkg: string, deps: IDependency[]): ICodeChunk[] {
if (deps[0].dependencyType === DependencyType.Internal) { if (deps[0].dependencyType === DependencyType.Internal) {
// TODO: Internal Deps path use project slot setting // TODO: Internal Deps path use project slot setting
statementL.push(`'@src/${(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: FileType.JSX, fileType: isJSX ? FileType.JSX : FileType.JS,
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],
@ -105,7 +112,7 @@ function buildPackageImport(pkg: string, deps: IDependency[]): ICodeChunk[] {
statementL.push(`'${pkg}';`); statementL.push(`'${pkg}';`);
chunks.push({ chunks.push({
type: ChunkType.STRING, type: ChunkType.STRING,
fileType: FileType.JSX, fileType: isJSX ? FileType.JSX : FileType.JS,
name: COMMON_CHUNK_NAME.ExternalDepsImport, name: COMMON_CHUNK_NAME.ExternalDepsImport,
content: statementL.join(' '), content: statementL.join(' '),
linkAfter: [], linkAfter: [],
@ -120,13 +127,15 @@ const plugin: BuilderComponentPlugin = async (pre: ICodeStruct) => {
...pre, ...pre,
}; };
const isJSX = next.chunks.some(chunk => chunk.fileType === FileType.JSX);
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]); const chunks = buildPackageImport(pkg, packs[pkg], isJSX);
next.chunks.push.apply(next.chunks, chunks); next.chunks.push.apply(next.chunks, chunks);
}); });
} }

View File

@ -81,6 +81,20 @@ const plugin: BuilderComponentPlugin = async (pre: ICodeStruct) => {
], ],
}); });
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 next;
}; };

View File

@ -30,35 +30,48 @@ function generateInlineStyle(style: IInlineStyle): string | null {
} }
function generateAttr(attrName: string, attrValue: any): string { function generateAttr(attrName: string, attrValue: any): string {
if (attrName === 'initValue' || attrName === 'labelCol') {
return '';
}
const [isString, valueStr] = generateCompositeType(attrValue); const [isString, valueStr] = generateCompositeType(attrValue);
return `${attrName}=${isString ? `"${valueStr}"` : `{${valueStr}}`}`; return `${attrName}=${isString ? `"${valueStr}"` : `{${valueStr}}`}`;
} }
function mapNodeName(src: string): string {
if (src === 'Div') {
return 'div';
}
return src;
}
function generateNode(nodeItem: IComponentNodeItem): string { function generateNode(nodeItem: IComponentNodeItem): string {
const codePieces: string[] = []; const codePieces: string[] = [];
let propLines: string[] = [];
const { className, style, ...props } = nodeItem.props; const { className, style, ...props } = nodeItem.props;
codePieces.push(`<${nodeItem.componentName}`); codePieces.push(`<${mapNodeName(nodeItem.componentName)}`);
if (className) { if (className) {
codePieces.push(`className="${className}"`); propLines.push(`className="${className}"`);
} }
if (style) { if (style) {
const inlineStyle = generateInlineStyle(style); const inlineStyle = generateInlineStyle(style);
if (inlineStyle !== null) { if (inlineStyle !== null) {
codePieces.push(`style={${inlineStyle}}`); propLines.push(`style={${inlineStyle}}`);
} }
} }
const propLines = Object.keys(props).map((propName: string) => propLines = propLines.concat(
Object.keys(props).map((propName: string) =>
generateAttr(propName, props[propName]), generateAttr(propName, props[propName]),
),
); );
codePieces.push.apply(codePieces, propLines); codePieces.push(` ${propLines.join(' ')} `);
if (nodeItem.children && (nodeItem.children as unknown[]).length > 0) { if (nodeItem.children && (nodeItem.children as unknown[]).length > 0) {
codePieces.push('>'); codePieces.push('>');
const childrenLines = generateChildren(nodeItem.children); const childrenLines = generateChildren(nodeItem.children);
codePieces.push.apply(codePieces, childrenLines); codePieces.push.apply(codePieces, childrenLines);
codePieces.push(`</${nodeItem.componentName}>`); codePieces.push(`</${mapNodeName(nodeItem.componentName)}>`);
} else { } else {
codePieces.push('/>'); codePieces.push('/>');
} }
@ -86,7 +99,7 @@ function generateNode(nodeItem: IComponentNodeItem): string {
codePieces.push('}'); codePieces.push('}');
} }
return codePieces.join(' '); return codePieces.join('');
} }
function generateChildren(children: ChildNodeType): string[] { function generateChildren(children: ChildNodeType): string[] {

View File

@ -18,7 +18,7 @@ const plugin: BuilderComponentPlugin = async (pre: ICodeStruct) => {
next.chunks.push({ next.chunks.push({
type: ChunkType.STRING, type: ChunkType.STRING,
fileType: FileType.CSS, fileType: FileType.SCSS,
name: COMMON_CHUNK_NAME.StyleDepsImport, name: COMMON_CHUNK_NAME.StyleDepsImport,
content: ` content: `
// 引入默认全局样式 // 引入默认全局样式
@ -29,7 +29,7 @@ const plugin: BuilderComponentPlugin = async (pre: ICodeStruct) => {
next.chunks.push({ next.chunks.push({
type: ChunkType.STRING, type: ChunkType.STRING,
fileType: FileType.CSS, fileType: FileType.SCSS,
name: COMMON_CHUNK_NAME.StyleCssContent, name: COMMON_CHUNK_NAME.StyleCssContent,
content: ` content: `
body { body {
@ -41,7 +41,7 @@ const plugin: BuilderComponentPlugin = async (pre: ICodeStruct) => {
next.chunks.push({ next.chunks.push({
type: ChunkType.STRING, type: ChunkType.STRING,
fileType: FileType.CSS, fileType: FileType.SCSS,
name: COMMON_CHUNK_NAME.StyleCssContent, name: COMMON_CHUNK_NAME.StyleCssContent,
content: ir.css || '', content: ir.css || '',
linkAfter: [COMMON_CHUNK_NAME.StyleDepsImport], linkAfter: [COMMON_CHUNK_NAME.StyleDepsImport],

View File

@ -5,7 +5,7 @@ import { createProjectBuilder } from '@/generator/ProjectBuilder';
import esmodule from '@/plugins/common/esmodule'; import esmodule from '@/plugins/common/esmodule';
import containerClass from '@/plugins/component/react/containerClass'; import containerClass from '@/plugins/component/react/containerClass';
import containerInitState from '@/plugins/component/react/containerInitState'; import containerInitState from '@/plugins/component/react/containerInitState';
import containerInjectUtils from '@/plugins/component/react/containerInjectUtils'; // import containerInjectUtils from '@/plugins/component/react/containerInjectUtils';
import containerLifeCycle from '@/plugins/component/react/containerLifeCycle'; import containerLifeCycle from '@/plugins/component/react/containerLifeCycle';
import containerMethod from '@/plugins/component/react/containerMethod'; import containerMethod from '@/plugins/component/react/containerMethod';
import jsx from '@/plugins/component/react/jsx'; import jsx from '@/plugins/component/react/jsx';
@ -29,7 +29,7 @@ export default function createIceJsProjectBuilder(): IProjectBuilder {
reactCommonDeps, reactCommonDeps,
esmodule, esmodule,
containerClass, containerClass,
containerInjectUtils, // containerInjectUtils,
containerInitState, containerInitState,
containerLifeCycle, containerLifeCycle,
containerMethod, containerMethod,
@ -40,7 +40,7 @@ export default function createIceJsProjectBuilder(): IProjectBuilder {
reactCommonDeps, reactCommonDeps,
esmodule, esmodule,
containerClass, containerClass,
containerInjectUtils, // containerInjectUtils,
containerInitState, containerInitState,
containerLifeCycle, containerLifeCycle,
containerMethod, containerMethod,

View File

@ -8,6 +8,7 @@ import {
export enum FileType { export enum FileType {
CSS = 'css', CSS = 'css',
SCSS = 'scss',
HTML = 'html', HTML = 'html',
JS = 'js', JS = 'js',
JSX = 'jsx', JSX = 'jsx',

View File

@ -10,9 +10,9 @@ export interface IExternalDependency extends IDependency {
} }
export enum InternalDependencyType { export enum InternalDependencyType {
PAGE = 'page', PAGE = 'pages',
BLOCK = 'block', BLOCK = 'components',
COMPONENT = 'component', COMPONENT = 'components',
UTILS = 'utils', UTILS = 'utils',
} }