lowcode-engine icon indicating copy to clipboard operation
lowcode-engine copied to clipboard

出码缺少全局上下文共享数据

Open iqinning opened this issue 3 years ago • 6 comments

当前出码上下文数据 IContextData在模块内是可用的,跨模块无法使用共享IContextData。可否如下加一个全局上下文数据传递?

  private createModuleBuilders(): Record<string, IModuleBuilder> {
    const builders: Record<string, IModuleBuilder> = {};

    Object.keys(this.plugins).forEach((pluginName) => {
      if (this.plugins[pluginName].length > 0) {
        const options: { mainFileName?: string } = {};
        if (this.template.slots[pluginName] && this.template.slots[pluginName].fileName) {
          options.mainFileName = this.template.slots[pluginName].fileName;
        }
        
        builders[pluginName] = createModuleBuilder({
          plugins: this.plugins[pluginName],
          postProcessors: this.postProcessors,
          contextData: {
            inStrictMode: this.inStrictMode,
            tolerateEvalErrors: true,
            evalErrorsHandler: '',
            ...this.extraContextData,
            globalContextData: this.globalContextData  // 这是全局上下文传递示例
          },
          ...options,
        });
      }
    });

    return builders;
  }


iqinning avatar Nov 28 '22 02:11 iqinning

globalContextData 这个从哪里来?

LeoYuan avatar Nov 28 '22 03:11 LeoYuan

我是写了一个示例,现在源码里面没有

iqinning avatar Nov 28 '22 03:11 iqinning

嗯嗯,我意思就是这么个上下文,如何构造?从 schema 中某个字段?完整用途是?

信息还是要详实一点,不然目前除了能感知到你想要一个跨 module 的公共 context,其他基本一无所知。。。

LeoYuan avatar Nov 28 '22 03:11 LeoYuan

好的,我把自己思路整理一下

iqinning avatar Nov 28 '22 03:11 iqinning

先说下为什么加的初衷,出码这块引擎设计的非常棒特别易于扩展。我们在扩展Vue出码时做了一些全局性配置,比如出码语言、状态管理配置,针对状态管理配置来说是跨模块的数据源出码、Vue文件出码都会用到。目前这些信息配置在 schema 应用描述中的 Config 字段上。 然后通过 generateModule 方法入参 往下传递。 也就是每次在调用 generateModule 之前要附加一些全局数据在里面,所以也就有了出码时共享全局数据的想法。

有个思路: 模块出码builder在协议解析完成后在创建,把解析后的配置信息传递给 createModuleBuilders 让他作为全局配置项供所有模块通用。

  async generateProject(originalSchema: ProjectSchema | string): Promise<ResultDir> {
    // Init
    const { schemaParser } = this;

    // builder 等协议解析完成在创建builder
    // const builders = this.createModuleBuilders();

    const projectRoot = await this.template.generateTemplate();

    let schema: ProjectSchema =
      typeof originalSchema === 'string' ? JSON.parse(originalSchema) : originalSchema;

    // Validate
    if (!schemaParser.validate(schema)) {
      throw new CodeGeneratorError('Schema is invalid');
    }

    // Parse / Format

    // Preprocess
    for (const preProcessor of this.projectPreProcessors) {
      // eslint-disable-next-line no-await-in-loop
      schema = await preProcessor(schema);
    }

    // Collect Deps
    // Parse JSExpression
    const parseResult: IParseResult = schemaParser.parse(schema);

    // 类似这样把 config 作为全局参数传递给 builder
    const builders = this.createModuleBuilders({...parseResult.project?.config});
    // ...
}

iqinning avatar Nov 28 '22 06:11 iqinning

目前从 ProjectBuilder 传入的 contextData,在 ModuleBuilder 和 Plugin 都能获取到,已经算是比较 global 了,你还想在其他什么地方获取么? @iqinning

LeoYuan avatar Dec 09 '22 02:12 LeoYuan