GeezerVison is showing github.com — fix: 🐛 解决出码缺乏对于 i18n 数据的 params 的处理的问题 · puchenglin/lowcode-engine@2cf788c
File treeExpand file treeCollapse file tree Open diff view settings demo01/expected/demo-project/src demo02/expected/demo-project/src demo03/expected/demo-project/src demo04/expected/demo-project/src demo05/expected/demo-project/src demo06-jsslot/expected/demo-project/src demo07-newline-in-props/expected/demo-project/src demo08-jsslot-with-multiple-children/expected/demo-project/src demo09-jsslot-with-conditional-children/expected/demo-project/src demo10-jsslot-with-loop-children/expected/demo-project/src demo11-utils-name-alias/expected/demo-project/src demo12-refs/expected/demo-project/src demo13-datasource-prop/expected/demo-project/src
demo1/expected/demo-project/src demo2-utils-name-alias/expected/demo-project/src demo2/expected/demo-project/src demo3/expected/demo-project/src demo4/expected/demo-project/src demo5/expected/demo-project/src demo6-literal-condition/expected/demo-project/src demo7-literal-condition2/expected/demo-project/src demo8-datasource-prop/expected/demo-project/src demo9-datasource-engine/expected/demo-project/src demo_10-jsslot/expected/demo-project/src demo_11-jsslot-2/expected/demo-project/src
plugins/jsx/__snapshots__
Some content is hiddenLarge Commits have some content hidden by default. Use the searchbox below for content that may be hidden. Dismiss bannerExpand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: modules/code-generator/jest.config.js +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -6,4 +6,5 @@ module.exports = { | 6 | 6 | collectCoverage: false, | 7 | 7 | collectCoverageFrom: ['src/**/*.{ts,tsx}', '!**/node_modules/**', '!**/vendor/**'], | 8 | 8 | testMatch: ['<rootDir>/tests/**/*.test.ts'], | | 9 | + setupFiles: ['./jest.setup.js'], | 9 | 10 | }; |
Collapse file Copy file name to clipboard +12Lines changed: 12 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +// 对于 standalone 模式的专门 polyfills | | 2 | +if (process.env.TEST_TARGET === 'standalone') { | | 3 | + // 模拟浏览器环境 | | 4 | + global.window = global; | | 5 | + global.self = global; | | 6 | + | | 7 | + // 将所有测试用例里面的 './src' 都替换为 './dist/standalone' | | 8 | + jest.mock('./src', () => require('./dist/standalone')); | | 9 | +} | | 10 | + | | 11 | +// 如果在调试模式下,则不限制超时时间 | | 12 | +jest.setTimeout(typeof v8debug === 'object' ? Infinity : 30 * 1000); |
Collapse file Copy file name to clipboardExpand all lines: modules/code-generator/package.json +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 85 | 85 | "lodash-es": "^4.17.21", | 86 | 86 | "mock-fs": "^5.1.2", | 87 | 87 | "moment": "^2.29.1", | | 88 | + "nanomatch": "^1.2.13", | 88 | 89 | "node-fetch": "2.x", | 89 | 90 | "path-browserify": "^1.0.1", | 90 | 91 | "prettier": "^2.5.1", | |
Collapse file Copy file name to clipboard +55Lines changed: 55 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +#!/usr/bin/env node | | 2 | + | | 3 | +// @ts-check | | 4 | +const program = require('commander'); | | 5 | +const { spawnSync } = require('child_process'); | | 6 | +const glob = require('glob'); | | 7 | +const fs = require('fs'); | | 8 | +const path = require('path'); | | 9 | +const _ = require('lodash'); | | 10 | + | | 11 | +program | | 12 | + .option('--npm <npm>', 'specify the npm command location or alias') | | 13 | + .arguments('<project>') | | 14 | + .action((project, options) => { | | 15 | + try { | | 16 | + if (!fs.existsSync(project)) { | | 17 | + throw new Error(`Project ${project} does not exist`); | | 18 | + } | | 19 | + | | 20 | + const getProjectActualPath = [ | | 21 | + () => path.resolve(process.cwd(), project), | | 22 | + () => | | 23 | + path.resolve( | | 24 | + process.cwd(), | | 25 | + path.join( | | 26 | + project, | | 27 | + path.dirname(glob.sync('*/package.json', { cwd: project })[0] || ''), | | 28 | + ), | | 29 | + ), | | 30 | + ] | | 31 | + .map((x) => _.memoize(x)) | | 32 | + .find((x) => fs.existsSync(path.join(x(), 'package.json'))); | | 33 | + | | 34 | + if (!getProjectActualPath) { | | 35 | + throw new Error(`Project ${project} is not a valid project(no package.json)`); | | 36 | + } | | 37 | + | | 38 | + const projectActualPath = getProjectActualPath(); | | 39 | + if (path.resolve(process.cwd(), project) !== projectActualPath) { | | 40 | + console.log('Changing directory to', path.relative(process.cwd(), projectActualPath)); | | 41 | + } | | 42 | + | | 43 | + process.chdir(projectActualPath); | | 44 | + | | 45 | + const npm = options.npm || 'npm'; | | 46 | + const cmd = `${npm} install && ${npm} start`; | | 47 | + console.log('# %s', cmd); | | 48 | + spawnSync(cmd, { stdio: 'inherit', shell: true }); | | 49 | + } catch (err) { | | 50 | + console.log(err); | | 51 | + process.exit(1); | | 52 | + } | | 53 | + }); | | 54 | + | | 55 | +program.parse(process.argv); |
Collapse file Copy file name to clipboardExpand all lines: modules/code-generator/src/cli/solutions/example-solution.ts +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -638,6 +638,7 @@ export default function createHelloWorldProjectBuilder() { | 638 | 638 | CodeGen.plugins.react.reactCommonDeps(), | 639 | 639 | CodeGen.plugins.common.esmodule({ fileType: 'jsx' }), | 640 | 640 | CodeGen.plugins.react.containerClass(), | | 641 | + CodeGen.plugins.react.containerInjectContext(), | 641 | 642 | CodeGen.plugins.react.containerInjectUtils(), | 642 | 643 | CodeGen.plugins.react.containerInjectDataSourceEngine(), | 643 | 644 | CodeGen.plugins.react.containerInjectI18n(), | @@ -659,6 +660,7 @@ export default function createHelloWorldProjectBuilder() { | 659 | 660 | CodeGen.plugins.react.reactCommonDeps(), | 660 | 661 | CodeGen.plugins.common.esmodule({ fileType: 'jsx' }), | 661 | 662 | CodeGen.plugins.react.containerClass(), | | 663 | + CodeGen.plugins.react.containerInjectContext(), | 662 | 664 | CodeGen.plugins.react.containerInjectUtils(), | 663 | 665 | CodeGen.plugins.react.containerInjectDataSourceEngine(), | 664 | 666 | CodeGen.plugins.react.containerInjectI18n(), | |
Collapse file Copy file name to clipboardExpand all lines: modules/code-generator/src/generator/ModuleBuilder.ts +9-1Lines changed: 9 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 5 | 5 | CodeGeneratorError, | 6 | 6 | ICodeChunk, | 7 | 7 | ICompiledModule, | | 8 | + IContextData, | 8 | 9 | IModuleBuilder, | 9 | 10 | IParseResult, | 10 | 11 | ISchemaParser, | @@ -23,6 +24,7 @@ export function createModuleBuilder( | 23 | 24 | plugins: BuilderComponentPlugin[]; | 24 | 25 | postProcessors: PostProcessor[]; | 25 | 26 | mainFileName?: string; | | 27 | + contextData?: IContextData; | 26 | 28 | } = { | 27 | 29 | plugins: [], | 28 | 30 | postProcessors: [], | @@ -41,7 +43,13 @@ export function createModuleBuilder( | 41 | 43 |
| 42 | 44 | let files: ResultFile[] = []; | 43 | 45 |
| 44 | | - const { chunks } = await chunkGenerator.run(input); | | 46 | + const { chunks } = await chunkGenerator.run(input, { | | 47 | + ir: input, | | 48 | + chunks: [], | | 49 | + depNames: [], | | 50 | + contextData: options.contextData || {}, | | 51 | + }); | | 52 | + | 45 | 53 | chunks.forEach((fileChunkList) => { | 46 | 54 | const content = linker.link(fileChunkList); | 47 | 55 | const file = createResultFile( | |
Collapse file Copy file name to clipboardExpand all lines: modules/code-generator/src/generator/ProjectBuilder.ts +19-1Lines changed: 19 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -14,7 +14,7 @@ import { SchemaParser } from '../parser/SchemaParser'; | 14 | 14 | import { createResultDir, addDirectory, addFile } from '../utils/resultHelper'; | 15 | 15 |
| 16 | 16 | import { createModuleBuilder } from './ModuleBuilder'; | 17 | | -import { ProjectPreProcessor, ProjectPostProcessor } from '../types/core'; | | 17 | +import { ProjectPreProcessor, ProjectPostProcessor, IContextData } from '../types/core'; | 18 | 18 | import { CodeGeneratorError } from '../types/error'; | 19 | 19 |
| 20 | 20 | interface IModuleInfo { | @@ -36,6 +36,10 @@ export interface ProjectBuilderInitOptions { | 36 | 36 | projectPreProcessors?: ProjectPreProcessor[]; | 37 | 37 | /** 项目级别的后置处理器 */ | 38 | 38 | projectPostProcessors?: ProjectPostProcessor[]; | | 39 | + /** 是否处于严格模式 */ | | 40 | + inStrictMode?: boolean; | | 41 | + /** 一些额外的上下文数据 */ | | 42 | + extraContextData?: Record<string, unknown>; | 39 | 43 | } | 40 | 44 |
| 41 | 45 | export class ProjectBuilder implements IProjectBuilder { | @@ -57,20 +61,30 @@ export class ProjectBuilder implements IProjectBuilder { | 57 | 61 | /** 项目级别的后置处理器 */ | 58 | 62 | private projectPostProcessors: ProjectPostProcessor[]; | 59 | 63 |
| | 64 | + /** 是否处于严格模式 */ | | 65 | + public readonly inStrictMode: boolean; | | 66 | + | | 67 | + /** 一些额外的上下文数据 */ | | 68 | + public readonly extraContextData: IContextData; | | 69 | + | 60 | 70 | constructor({ | 61 | 71 | template, | 62 | 72 | plugins, | 63 | 73 | postProcessors, | 64 | 74 | schemaParser = new SchemaParser(), | 65 | 75 | projectPreProcessors = [], | 66 | 76 | projectPostProcessors = [], | | 77 | + inStrictMode = false, | | 78 | + extraContextData = {}, | 67 | 79 | }: ProjectBuilderInitOptions) { | 68 | 80 | this.template = template; | 69 | 81 | this.plugins = plugins; | 70 | 82 | this.postProcessors = postProcessors; | 71 | 83 | this.schemaParser = schemaParser; | 72 | 84 | this.projectPreProcessors = projectPreProcessors; | 73 | 85 | this.projectPostProcessors = projectPostProcessors; | | 86 | + this.inStrictMode = inStrictMode; | | 87 | + this.extraContextData = extraContextData; | 74 | 88 | } | 75 | 89 |
| 76 | 90 | async generateProject(originalSchema: ProjectSchema | string): Promise<ResultDir> { | @@ -264,6 +278,10 @@ export class ProjectBuilder implements IProjectBuilder { | 264 | 278 | builders[pluginName] = createModuleBuilder({ | 265 | 279 | plugins: this.plugins[pluginName], | 266 | 280 | postProcessors: this.postProcessors, | | 281 | + contextData: { | | 282 | + inStrictMode: this.inStrictMode, | | 283 | + ...this.extraContextData, | | 284 | + }, | 267 | 285 | ...options, | 268 | 286 | }); | 269 | 287 | } | |
Collapse file Copy file name to clipboardExpand all lines: modules/code-generator/src/index.ts +3-21Lines changed: 3 additions & 21 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -25,16 +25,8 @@ import i18n from './plugins/project/i18n'; | 25 | 25 | import utils from './plugins/project/utils'; | 26 | 26 | import prettier from './postprocessor/prettier'; | 27 | 27 |
| 28 | | -// 引入常用工具 | 29 | | -import * as utilsCommon from './utils/common'; | 30 | | -import * as utilsCompositeType from './utils/compositeType'; | 31 | | -import * as utilsJsExpression from './utils/jsExpression'; | 32 | | -import * as utilsJsSlot from './utils/jsSlot'; | 33 | | -import * as utilsNodeToJSX from './utils/nodeToJSX'; | 34 | | -import * as utilsResultHelper from './utils/resultHelper'; | 35 | | -import * as utilsTemplateHelper from './utils/templateHelper'; | 36 | | -import * as utilsValidate from './utils/validate'; | 37 | | -import * as utilsSchema from './utils/schema'; | | 28 | +// 引入全局常用工具 | | 29 | +import * as globalUtils from './utils'; | 38 | 30 |
| 39 | 31 | import * as CONSTANTS from './const'; | 40 | 32 |
| @@ -85,17 +77,7 @@ export default { | 85 | 77 | postprocessor: { | 86 | 78 | prettier, | 87 | 79 | }, | 88 | | - utils: { | 89 | | - common: utilsCommon, | 90 | | - compositeType: utilsCompositeType, | 91 | | - jsExpression: utilsJsExpression, | 92 | | - jsSlot: utilsJsSlot, | 93 | | - nodeToJSX: utilsNodeToJSX, | 94 | | - resultHelper: utilsResultHelper, | 95 | | - templateHelper: utilsTemplateHelper, | 96 | | - validate: utilsValidate, | 97 | | - schema: utilsSchema, | 98 | | - }, | | 80 | + utils: globalUtils, | 99 | 81 | chunkNames: { | 100 | 82 | COMMON_CHUNK_NAME, | 101 | 83 | CLASS_DEFINE_CHUNK_NAME, | |
Collapse file Copy file name to clipboardExpand all lines: modules/code-generator/src/plugins/component/rax/containerClass.ts -13Lines changed: 0 additions & 13 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -134,19 +134,6 @@ const pluginFactory: BuilderComponentPluginFactory<unknown> = () => { | 134 | 134 | ], | 135 | 135 | }); | 136 | 136 |
| 137 | | - next.chunks.push({ | 138 | | - type: ChunkType.STRING, | 139 | | - fileType: FileType.JSX, | 140 | | - name: CLASS_DEFINE_CHUNK_NAME.InsPrivateMethod, | 141 | | - content: ` | 142 | | - _i18nText(t) { | 143 | | - const locale = this._context.getLocale(); | 144 | | - return t[locale] ?? t[String(locale).replace('-', '_')] ?? t[t.use || 'zh_CN'] ?? t.en_US; | 145 | | - } | 146 | | - `, | 147 | | - linkAfter: [RAX_CHUNK_NAME.ClassRenderEnd], | 148 | | - }); | 149 | | - | 150 | 137 | next.chunks.push({ | 151 | 138 | type: ChunkType.STRING, | 152 | 139 | fileType: FileType.JSX, | |
Collapse file Copy file name to clipboardExpand all lines: modules/code-generator/src/plugins/component/rax/containerInjectContext.ts +11Lines changed: 11 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -10,6 +10,7 @@ import { | 10 | 10 | IContainerInfo, | 11 | 11 | } from '../../../types'; | 12 | 12 | import { RAX_CHUNK_NAME } from './const'; | | 13 | +import { DEFAULT_LINK_AFTER } from '../../../const'; | 13 | 14 |
| 14 | 15 | export interface PluginConfig { | 15 | 16 | fileType: string; | @@ -46,6 +47,16 @@ const pluginFactory: BuilderComponentPluginFactory<PluginConfig> = (config?) => | 46 | 47 | linkAfter: [COMMON_CHUNK_NAME.ExternalDepsImport], | 47 | 48 | }); | 48 | 49 |
| | 50 | + next.chunks.push({ | | 51 | + type: ChunkType.STRING, | | 52 | + fileType: cfg.fileType, | | 53 | + name: CLASS_DEFINE_CHUNK_NAME.ConstructorContent, | | 54 | + content: ` | | 55 | + __$$i18n._inject2(this); | | 56 | + `, | | 57 | + linkAfter: [...DEFAULT_LINK_AFTER[CLASS_DEFINE_CHUNK_NAME.ConstructorContent]], | | 58 | + }); | | 59 | + | 49 | 60 | next.chunks.push({ | 50 | 61 | type: ChunkType.STRING, | 51 | 62 | fileType: cfg.fileType, | |
|