diff --git a/packages/code-generator/demo/demo.js b/packages/code-generator/demo/demo.js index 91e145de9..61c71de97 100644 --- a/packages/code-generator/demo/demo.js +++ b/packages/code-generator/demo/demo.js @@ -1,4 +1,5 @@ const fs = require('fs'); +// ../lib 可以替换成 @ali/lowcode-code-generator const CodeGenerator = require('../lib').default; function flatFiles(rootName, dir) { @@ -50,4 +51,104 @@ function main() { }); } -main(); +function exportModule() { + const schemaJson = fs.readFileSync('./demo/shenmaSample.json', { encoding: 'utf8' }); + const moduleBuilder = CodeGenerator.createModuleBuilder({ + plugins: [ + CodeGenerator.plugins.react.reactCommonDeps(), + CodeGenerator.plugins.common.esmodule({ + fileType: 'jsx', + }), + CodeGenerator.plugins.react.containerClass(), + CodeGenerator.plugins.react.containerInitState(), + CodeGenerator.plugins.react.containerLifeCycle(), + CodeGenerator.plugins.react.containerMethod(), + CodeGenerator.plugins.react.jsx(), + CodeGenerator.plugins.style.css(), + ], + postProcessors: [ + CodeGenerator.postprocessor.prettier(), + ], + mainFileName: 'index', + }); + + moduleBuilder.generateModuleCode(schemaJson).then(result => { + displayResultInConsole(result); + return result; + }); +} + +function exportProject() { + const schemaJson = fs.readFileSync('./demo/sampleSchema.json', { encoding: 'utf8' }); + + const builder = CodeGenerator.createProjectBuilder({ + template: CodeGenerator.solutionParts.icejs.template, + plugins: { + components: [ + CodeGenerator.plugins.react.reactCommonDeps(), + CodeGenerator.plugins.common.esmodule({ + fileType: 'jsx', + }), + CodeGenerator.plugins.react.containerClass(), + CodeGenerator.plugins.react.containerInitState(), + CodeGenerator.plugins.react.containerLifeCycle(), + CodeGenerator.plugins.react.containerMethod(), + CodeGenerator.plugins.react.jsx(), + CodeGenerator.plugins.style.css(), + ], + pages: [ + CodeGenerator.plugins.react.reactCommonDeps(), + CodeGenerator.plugins.common.esmodule({ + fileType: 'jsx', + }), + CodeGenerator.plugins.react.containerClass(), + CodeGenerator.plugins.react.containerInitState(), + CodeGenerator.plugins.react.containerLifeCycle(), + CodeGenerator.plugins.react.containerMethod(), + CodeGenerator.plugins.react.jsx(), + CodeGenerator.plugins.style.css(), + ], + router: [ + CodeGenerator.plugins.common.esmodule(), + CodeGenerator.solutionParts.icejs.plugins.router(), + ], + entry: [ + CodeGenerator.solutionParts.icejs.plugins.entry(), + ], + constants: [ + CodeGenerator.plugins.project.constants(), + ], + utils: [ + CodeGenerator.plugins.common.esmodule(), + CodeGenerator.plugins.project.utils(), + ], + i18n: [ + CodeGenerator.plugins.project.i18n(), + ], + globalStyle: [ + CodeGenerator.solutionParts.icejs.plugins.globalStyle(), + ], + htmlEntry: [ + CodeGenerator.solutionParts.icejs.plugins.entryHtml(), + ], + packageJSON: [ + CodeGenerator.solutionParts.icejs.plugins.packageJSON(), + ], + }, + postProcessors: [ + CodeGenerator.postprocessor.prettier(), + ], + }); + + builder.generateProject(schemaJson).then(result => { + displayResultInConsole(result); + writeResultToDisk(result, 'output/lowcodeDemo').then(response => + console.log('Write to disk: ', JSON.stringify(response)), + ); + return result; + }); +} + +// main(); +// exportModule(); +exportProject(); diff --git a/packages/code-generator/demo/shenmaSample.json b/packages/code-generator/demo/shenmaSample.json new file mode 100644 index 000000000..eed4c1031 --- /dev/null +++ b/packages/code-generator/demo/shenmaSample.json @@ -0,0 +1,36 @@ +{ + "version": "1.0.0", + "componentsMap": [ + { + "componentName": "Demo", + "package": "@ali/demo", + "version": "1.19.18", + "destructuring": true, + "exportName": "Demo" + } + ], + "id": "page_kc326r8m", + "componentsTree": [{ + "componentName": "Page", + "id": "node_kc326r8h", + "props": {}, + "condition": true, + "loopArgs": ["item", "index"], + "children": [{ + "componentName": "Demo", + "id": "node_kc326r8i", + "props": { + "value": "文本内容", + "color": "#ffffff", + "ui_maxLine": 2, + "url": "", + "ui_type": "xs", + "style": {}, + "className": "" + }, + "condition": true, + "loopArgs": ["item", "index"] + }] + }], + "params": {} +} diff --git a/packages/code-generator/package.json b/packages/code-generator/package.json index 22220ee8e..a6359455c 100644 --- a/packages/code-generator/package.json +++ b/packages/code-generator/package.json @@ -1,6 +1,6 @@ { "name": "@ali/lowcode-code-generator", - "version": "0.8.7", + "version": "0.8.8", "description": "出码引擎 for LowCode Engine", "main": "lib/index.js", "files": [ diff --git a/packages/code-generator/src/index.ts b/packages/code-generator/src/index.ts index 9de9c1538..2ef5cf28c 100644 --- a/packages/code-generator/src/index.ts +++ b/packages/code-generator/src/index.ts @@ -10,6 +10,11 @@ import createRecoreProjectBuilder from './solutions/recore'; // 引入说明 import { REACT_CHUNK_NAME } from './plugins/component/react/const'; +import { + COMMON_CHUNK_NAME, + CLASS_DEFINE_CHUNK_NAME, + DEFAULT_LINK_AFTER, +} from './const/generator'; // 引入通用插件组 import esmodule from './plugins/common/esmodule'; @@ -26,6 +31,7 @@ import css from './plugins/component/style/css'; import constants from './plugins/project/constants'; import i18n from './plugins/project/i18n'; import utils from './plugins/project/utils'; +import prettier from './postprocessor/prettier'; // 引入常用工具 import * as utilsCommon from './utils/common'; @@ -34,6 +40,9 @@ import * as utilsJsExpression from './utils/jsExpression'; import * as utilsNodeToJSX from './utils/nodeToJSX'; import * as utilsTemplateHelper from './utils/templateHelper'; +// 引入内置解决方案模块 +import icejs from './plugins/project/framework/icejs'; + export * from './types'; export default { @@ -43,6 +52,9 @@ export default { icejs: createIceJsProjectBuilder, recore: createRecoreProjectBuilder, }, + solutionParts: { + icejs, + }, publishers: { disk: createDiskPublisher, }, @@ -70,6 +82,9 @@ export default { utils, }, }, + postprocessor: { + prettier, + }, utils: { common: utilsCommon, compositeType: utilsCompositeType, @@ -77,4 +92,12 @@ export default { nodeToJSX: utilsNodeToJSX, templateHelper: utilsTemplateHelper, }, + chunkNames: { + COMMON_CHUNK_NAME, + CLASS_DEFINE_CHUNK_NAME, + REACT_CHUNK_NAME, + }, + defaultLinkAfter: { + COMMON_DEFAULT_LINK_AFTER: DEFAULT_LINK_AFTER, + }, }; diff --git a/packages/code-generator/src/plugins/project/framework/icejs/index.ts b/packages/code-generator/src/plugins/project/framework/icejs/index.ts new file mode 100644 index 000000000..e9c8f255f --- /dev/null +++ b/packages/code-generator/src/plugins/project/framework/icejs/index.ts @@ -0,0 +1,17 @@ +import template from './template'; +import entry from './plugins/entry'; +import entryHtml from './plugins/entryHtml'; +import globalStyle from './plugins/globalStyle'; +import packageJSON from './plugins/packageJSON'; +import router from './plugins/router'; + +export default { + template, + plugins: { + entry, + entryHtml, + globalStyle, + packageJSON, + router, + }, +}; diff --git a/packages/code-generator/src/postprocessor/prettier/index.ts b/packages/code-generator/src/postprocessor/prettier/index.ts index 519c2db5d..85c2f9741 100644 --- a/packages/code-generator/src/postprocessor/prettier/index.ts +++ b/packages/code-generator/src/postprocessor/prettier/index.ts @@ -5,7 +5,7 @@ import { PostProcessor, PostProcessorFactory } from '../../types'; const PARSERS = ['css', 'scss', 'less', 'json', 'html', 'vue']; -interface ProcessorConfig { +type ProcessorConfig = { customFileTypeParser: Record; } diff --git a/packages/code-generator/src/solutions/icejs.ts b/packages/code-generator/src/solutions/icejs.ts index 62a514008..bfb7e28f8 100644 --- a/packages/code-generator/src/solutions/icejs.ts +++ b/packages/code-generator/src/solutions/icejs.ts @@ -12,20 +12,16 @@ import jsx from '../plugins/component/react/jsx'; import reactCommonDeps from '../plugins/component/react/reactCommonDeps'; import css from '../plugins/component/style/css'; import constants from '../plugins/project/constants'; -import iceJsEntry from '../plugins/project/framework/icejs/plugins/entry'; -import iceJsEntryHtml from '../plugins/project/framework/icejs/plugins/entryHtml'; -import iceJsGlobalStyle from '../plugins/project/framework/icejs/plugins/globalStyle'; -import iceJsPackageJSON from '../plugins/project/framework/icejs/plugins/packageJSON'; -import iceJsRouter from '../plugins/project/framework/icejs/plugins/router'; -import template from '../plugins/project/framework/icejs/template'; import i18n from '../plugins/project/i18n'; import utils from '../plugins/project/utils'; +import icejs from '../plugins/project/framework/icejs'; + import { prettier } from '../postprocessor'; export default function createIceJsProjectBuilder(): IProjectBuilder { return createProjectBuilder({ - template, + template: icejs.template, plugins: { components: [ reactCommonDeps(), @@ -53,14 +49,14 @@ export default function createIceJsProjectBuilder(): IProjectBuilder { jsx(), css(), ], - router: [esmodule(), iceJsRouter()], - entry: [iceJsEntry()], + router: [esmodule(), icejs.plugins.router()], + entry: [icejs.plugins.entry()], constants: [constants()], utils: [esmodule(), utils()], i18n: [i18n()], - globalStyle: [iceJsGlobalStyle()], - htmlEntry: [iceJsEntryHtml()], - packageJSON: [iceJsPackageJSON()], + globalStyle: [icejs.plugins.globalStyle()], + htmlEntry: [icejs.plugins.entryHtml()], + packageJSON: [icejs.plugins.packageJSON()], }, postProcessors: [prettier()], });