@tramvai/module-child-app 1.65.1 → 1.66.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,16 +1,20 @@
1
- import type { ChildAppCommandLineRunner, ChildAppRequestConfig, ChildAppLoader, ChildAppPreloadManager, CHILD_APP_RESOLVE_CONFIG_TOKEN } from '@tramvai/tokens-child-app';
1
+ import type { ChildAppCommandLineRunner, ChildAppRequestConfig, ChildAppLoader, ChildAppPreloadManager, CHILD_APP_RESOLVE_CONFIG_TOKEN, CHILD_APP_RESOLUTION_CONFIG_MANAGER_TOKEN } from '@tramvai/tokens-child-app';
2
2
  import type { STORE_TOKEN } from '@tramvai/tokens-common';
3
3
  export declare class PreloadManager implements ChildAppPreloadManager {
4
4
  private loader;
5
5
  private runner;
6
+ private store;
7
+ private resolutionConfigManager;
6
8
  private resolveExternalConfig;
7
9
  private pageHasLoaded;
8
10
  private map;
9
11
  private serverPreloaded;
10
12
  private preloadMap;
11
- constructor({ loader, runner, resolveExternalConfig, store, }: {
13
+ private hasInitialized;
14
+ constructor({ loader, runner, resolutionConfigManager, resolveExternalConfig, store, }: {
12
15
  loader: ChildAppLoader;
13
16
  runner: ChildAppCommandLineRunner;
17
+ resolutionConfigManager: typeof CHILD_APP_RESOLUTION_CONFIG_MANAGER_TOKEN;
14
18
  resolveExternalConfig: typeof CHILD_APP_RESOLVE_CONFIG_TOKEN;
15
19
  store: typeof STORE_TOKEN;
16
20
  });
@@ -19,5 +23,7 @@ export declare class PreloadManager implements ChildAppPreloadManager {
19
23
  runPreloaded(): Promise<void>;
20
24
  clearPreloaded(): Promise<void>;
21
25
  getPreloadedList(): ChildAppRequestConfig[];
26
+ private initServerPreloaded;
27
+ private init;
22
28
  private run;
23
29
  }
@@ -1,20 +1,19 @@
1
- import type { ChildAppCommandLineRunner, ChildAppRequestConfig, ChildAppLoader, ChildAppPreloadManager, ChildAppStateManager, CHILD_APP_RESOLVE_CONFIG_TOKEN, CHILD_APP_PRELOAD_EXTERNAL_CONFIG_TOKEN } from '@tramvai/tokens-child-app';
1
+ import type { ChildAppCommandLineRunner, ChildAppRequestConfig, ChildAppLoader, ChildAppPreloadManager, ChildAppStateManager, CHILD_APP_RESOLVE_CONFIG_TOKEN, CHILD_APP_RESOLUTION_CONFIG_MANAGER_TOKEN } from '@tramvai/tokens-child-app';
2
2
  export declare class PreloadManager implements ChildAppPreloadManager {
3
3
  private loader;
4
4
  private runner;
5
5
  private stateManager;
6
- private preloadExternalConfig;
6
+ private resolutionConfigManager;
7
7
  private readonly resolveFullConfig;
8
8
  private shouldRunImmediately;
9
9
  private map;
10
10
  private preloadMap;
11
- private configHasBeenPreloaded;
12
- constructor({ loader, runner, stateManager, preloadExternalConfig, resolveFullConfig, }: {
11
+ constructor({ loader, runner, stateManager, resolutionConfigManager, resolveFullConfig, }: {
13
12
  loader: ChildAppLoader;
14
13
  runner: ChildAppCommandLineRunner;
15
14
  stateManager: ChildAppStateManager;
15
+ resolutionConfigManager: typeof CHILD_APP_RESOLUTION_CONFIG_MANAGER_TOKEN;
16
16
  resolveFullConfig: typeof CHILD_APP_RESOLVE_CONFIG_TOKEN;
17
- preloadExternalConfig: typeof CHILD_APP_PRELOAD_EXTERNAL_CONFIG_TOKEN | null;
18
17
  });
19
18
  preload(request: ChildAppRequestConfig): Promise<void>;
20
19
  isPreloaded(request: ChildAppRequestConfig): boolean;
@@ -1,14 +1,15 @@
1
1
  import { __decorate } from 'tslib';
2
2
  import { provide, COMMAND_LINE_RUNNER_TOKEN, walkOfModules, getModuleParameters, commandLineListTokens as commandLineListTokens$1, Module } from '@tramvai/core';
3
- import flatten from '@tinkoff/utils/array/flatten';
4
3
  import { Container, ChildContainer, Scope, DI_TOKEN } from '@tinkoff/dippy';
5
- import { commandLineListTokens, CHILD_APP_INTERNAL_ROOT_STATE_SUBSCRIPTION_TOKEN, CHILD_APP_INTERNAL_ACTION_TOKEN, CHILD_APP_INTERNAL_CONFIG_TOKEN, IS_CHILD_APP_DI_TOKEN, CHILD_APP_INTERNAL_ROOT_DI_BORROW_TOKEN, CHILD_APP_GET_RESOLUTION_CONFIG_TOKEN, CHILD_APP_RESOLUTION_CONFIGS_TOKEN, CHILD_APP_RESOLVE_CONFIG_TOKEN, CHILD_APP_RESOLVE_BASE_URL_TOKEN, CHILD_APP_SINGLETON_DI_MANAGER_TOKEN, CHILD_APP_LOADER_TOKEN, CHILD_APP_DI_MANAGER_TOKEN, CHILD_APP_COMMAND_LINE_RUNNER_TOKEN, CHILD_APP_PRELOAD_MANAGER_TOKEN, CHILD_APP_RENDER_MANAGER_TOKEN, CHILD_APP_COMMON_INITIAL_STATE_TOKEN, CHILD_APP_INTERNAL_RENDER_TOKEN } from '@tramvai/tokens-child-app';
4
+ import { commandLineListTokens, CHILD_APP_INTERNAL_ROOT_STATE_SUBSCRIPTION_TOKEN, CHILD_APP_INTERNAL_ACTION_TOKEN, CHILD_APP_INTERNAL_CONFIG_TOKEN, IS_CHILD_APP_DI_TOKEN, CHILD_APP_INTERNAL_ROOT_DI_BORROW_TOKEN, CHILD_APP_RESOLUTION_CONFIG_MANAGER_TOKEN, CHILD_APP_RESOLUTION_CONFIGS_TOKEN, CHILD_APP_RESOLVE_CONFIG_TOKEN, CHILD_APP_RESOLVE_BASE_URL_TOKEN, CHILD_APP_SINGLETON_DI_MANAGER_TOKEN, CHILD_APP_LOADER_TOKEN, CHILD_APP_DI_MANAGER_TOKEN, CHILD_APP_COMMAND_LINE_RUNNER_TOKEN, CHILD_APP_PRELOAD_MANAGER_TOKEN, CHILD_APP_RENDER_MANAGER_TOKEN, CHILD_APP_COMMON_INITIAL_STATE_TOKEN, CHILD_APP_INTERNAL_RENDER_TOKEN } from '@tramvai/tokens-child-app';
6
5
  export * from '@tramvai/tokens-child-app';
7
6
  import { ACTION_PAGE_RUNNER_TOKEN, CONTEXT_TOKEN, LOGGER_TOKEN, DISPATCHER_TOKEN, STORE_TOKEN, COMBINE_REDUCERS, ENV_MANAGER_TOKEN, REGISTER_CLEAR_CACHE_TOKEN, CLEAR_CACHE_TOKEN, ENV_USED_TOKEN } from '@tramvai/tokens-common';
8
7
  import { RENDER_SLOTS, EXTEND_RENDER } from '@tramvai/tokens-render';
9
8
  import { PAGE_SERVICE_TOKEN } from '@tramvai/tokens-router';
9
+ import flatten from '@tinkoff/utils/array/flatten';
10
10
  import { Subscription, createEvent, createReducer } from '@tramvai/state';
11
11
  import React, { createContext, useContext, useMemo, useState, useEffect, createElement } from 'react';
12
+ import applyOrReturn from '@tinkoff/utils/function/applyOrReturn';
12
13
  import { loadModule } from '@tinkoff/module-loader-client';
13
14
  import noop from '@tinkoff/utils/function/noop';
14
15
  import { useDi } from '@tramvai/react';
@@ -298,6 +299,51 @@ const resolveComponent = async (componentOrLoader) => {
298
299
  : componentOrLoader;
299
300
  };
300
301
 
302
+ class ChildAppResolutionConfigManager {
303
+ constructor({ configs }) {
304
+ this.hasInitialized = false;
305
+ this.rawConfigs = configs !== null && configs !== void 0 ? configs : [];
306
+ this.mapping = new Map();
307
+ }
308
+ async init() {
309
+ if (this.hasInitialized) {
310
+ return;
311
+ }
312
+ if (this.initPromise) {
313
+ return this.initPromise;
314
+ }
315
+ this.initPromise = (async () => {
316
+ const configs = await Promise.all(this.rawConfigs.map((rawConfig) => {
317
+ return applyOrReturn([], rawConfig);
318
+ }));
319
+ flatten(configs).forEach((config) => {
320
+ this.mapping.set(config.name, config);
321
+ });
322
+ this.hasInitialized = true;
323
+ })();
324
+ return this.initPromise;
325
+ }
326
+ resolve({ name, version, tag = 'latest' }) {
327
+ var _a;
328
+ const fromMapping = this.mapping.get(name);
329
+ if (!fromMapping) {
330
+ return null;
331
+ }
332
+ const cfg = fromMapping.byTag[tag];
333
+ if (process.env.NODE_ENV === 'development' && tag === 'debug' && !cfg) {
334
+ return {
335
+ baseUrl: 'http://localhost:4040/',
336
+ version: '0.0.0-stub',
337
+ };
338
+ }
339
+ return {
340
+ ...cfg,
341
+ baseUrl: (_a = cfg.baseUrl) !== null && _a !== void 0 ? _a : fromMapping.baseUrl,
342
+ version: version !== null && version !== void 0 ? version : cfg.version,
343
+ };
344
+ }
345
+ }
346
+
301
347
  const sharedProviders = [
302
348
  provide({
303
349
  provide: COMBINE_REDUCERS,
@@ -310,38 +356,27 @@ const sharedProviders = [
310
356
  useValue: initModuleFederation,
311
357
  }),
312
358
  provide({
313
- provide: CHILD_APP_GET_RESOLUTION_CONFIG_TOKEN,
314
- useFactory: ({ configs }) => {
315
- const mapping = flatten(configs !== null && configs !== void 0 ? configs : []).reduce((map, config) => {
316
- return map.set(config.name, config);
317
- }, new Map());
318
- return (({ name, version, tag = 'latest' }) => {
319
- var _a;
320
- const fromMapping = mapping.get(name);
321
- if (!fromMapping) {
322
- return null;
323
- }
324
- const cfg = fromMapping.byTag[tag];
325
- if (process.env.NODE_ENV === 'development' && tag === 'debug' && !cfg) {
326
- return {
327
- baseUrl: 'http://localhost:4040/',
328
- version: '0.0.0-stub',
329
- };
330
- }
331
- return {
332
- ...cfg,
333
- baseUrl: (_a = cfg.baseUrl) !== null && _a !== void 0 ? _a : fromMapping.baseUrl,
334
- version: version !== null && version !== void 0 ? version : cfg.version,
335
- };
336
- });
337
- },
359
+ provide: CHILD_APP_RESOLUTION_CONFIG_MANAGER_TOKEN,
360
+ useClass: ChildAppResolutionConfigManager,
338
361
  deps: {
339
362
  configs: { token: CHILD_APP_RESOLUTION_CONFIGS_TOKEN, optional: true },
340
363
  },
341
364
  }),
365
+ provide({
366
+ provide: commandLineListTokens$1.resolvePageDeps,
367
+ multi: true,
368
+ useFactory: ({ resolutionConfigManager }) => {
369
+ return async function fallbackResolutionConfigManagerInit() {
370
+ await resolutionConfigManager.init();
371
+ };
372
+ },
373
+ deps: {
374
+ resolutionConfigManager: CHILD_APP_RESOLUTION_CONFIG_MANAGER_TOKEN,
375
+ },
376
+ }),
342
377
  provide({
343
378
  provide: CHILD_APP_RESOLVE_CONFIG_TOKEN,
344
- useFactory: ({ envManager, rootBaseUrl, getResolutionConfig }) => {
379
+ useFactory: ({ envManager, rootBaseUrl, resolutionConfigManager }) => {
345
380
  const rawEnv = envManager.get('CHILD_APP_DEBUG');
346
381
  const debug = new Map();
347
382
  rawEnv === null || rawEnv === void 0 ? void 0 : rawEnv.split(';').reduce((acc, entry) => {
@@ -352,7 +387,10 @@ const sharedProviders = [
352
387
  var _a, _b;
353
388
  const { name, tag = debug.has(name) ? 'debug' : 'latest' } = request;
354
389
  const req = { name, tag, version: request.version };
355
- const config = getResolutionConfig(req);
390
+ const config = resolutionConfigManager.resolve(req);
391
+ if (!config) {
392
+ throw new Error(`Child-app "${name}" with tag "${tag}" has not found`);
393
+ }
356
394
  const { version, baseUrl: configBaseUrl, client, server, css, withoutCss } = config;
357
395
  const baseUrl = (_b = (_a = debug.get(name)) !== null && _a !== void 0 ? _a : configBaseUrl) !== null && _b !== void 0 ? _b : rootBaseUrl;
358
396
  if (!baseUrl) {
@@ -384,7 +422,7 @@ const sharedProviders = [
384
422
  deps: {
385
423
  envManager: ENV_MANAGER_TOKEN,
386
424
  rootBaseUrl: CHILD_APP_RESOLVE_BASE_URL_TOKEN,
387
- getResolutionConfig: CHILD_APP_GET_RESOLUTION_CONFIG_TOKEN,
425
+ resolutionConfigManager: CHILD_APP_RESOLUTION_CONFIG_MANAGER_TOKEN,
388
426
  },
389
427
  }),
390
428
  provide({
@@ -568,21 +606,20 @@ class BrowserLoader extends Loader {
568
606
  }
569
607
 
570
608
  class PreloadManager {
571
- constructor({ loader, runner, resolveExternalConfig, store, }) {
609
+ constructor({ loader, runner, resolutionConfigManager, resolveExternalConfig, store, }) {
572
610
  this.pageHasLoaded = false;
573
611
  this.map = new Map();
574
612
  this.serverPreloaded = new Map();
575
613
  this.preloadMap = new Map();
614
+ this.hasInitialized = false;
576
615
  this.loader = loader;
577
616
  this.runner = runner;
617
+ this.store = store;
618
+ this.resolutionConfigManager = resolutionConfigManager;
578
619
  this.resolveExternalConfig = resolveExternalConfig;
579
- const { preloaded } = store.getState(ChildAppStore);
580
- preloaded.forEach((request) => {
581
- const config = this.resolveExternalConfig(request);
582
- this.serverPreloaded.set(config.key, config);
583
- });
584
620
  }
585
621
  async preload(request) {
622
+ await this.init();
586
623
  const config = this.resolveExternalConfig(request);
587
624
  const { key } = config;
588
625
  this.preloadMap.set(key, config);
@@ -607,6 +644,7 @@ class PreloadManager {
607
644
  return this.map.has(key) || this.serverPreloaded.has(key);
608
645
  }
609
646
  async runPreloaded() {
647
+ await this.init();
610
648
  const promises = [];
611
649
  if (this.pageHasLoaded) {
612
650
  this.map.forEach((childAppPromise) => {
@@ -651,6 +689,20 @@ class PreloadManager {
651
689
  getPreloadedList() {
652
690
  return [...this.preloadMap.values()];
653
691
  }
692
+ initServerPreloaded() {
693
+ if (!this.hasInitialized) {
694
+ const { preloaded } = this.store.getState(ChildAppStore);
695
+ preloaded.forEach((request) => {
696
+ const config = this.resolveExternalConfig(request);
697
+ this.serverPreloaded.set(config.key, config);
698
+ });
699
+ this.hasInitialized = true;
700
+ }
701
+ }
702
+ async init() {
703
+ await this.resolutionConfigManager.init();
704
+ this.initServerPreloaded();
705
+ }
654
706
  async run(status, config) {
655
707
  const childApp = this.loader.get(config);
656
708
  if (!childApp) {
@@ -706,6 +758,7 @@ const browserProviders = [
706
758
  deps: {
707
759
  loader: CHILD_APP_LOADER_TOKEN,
708
760
  runner: CHILD_APP_COMMAND_LINE_RUNNER_TOKEN,
761
+ resolutionConfigManager: CHILD_APP_RESOLUTION_CONFIG_MANAGER_TOKEN,
709
762
  resolveExternalConfig: CHILD_APP_RESOLVE_CONFIG_TOKEN,
710
763
  store: STORE_TOKEN,
711
764
  },
@@ -721,6 +774,7 @@ const browserProviders = [
721
774
  logger: LOGGER_TOKEN,
722
775
  diManager: CHILD_APP_DI_MANAGER_TOKEN,
723
776
  preloadManager: CHILD_APP_PRELOAD_MANAGER_TOKEN,
777
+ resolutionConfigManager: CHILD_APP_RESOLUTION_CONFIG_MANAGER_TOKEN,
724
778
  resolveExternalConfig: CHILD_APP_RESOLVE_CONFIG_TOKEN,
725
779
  },
726
780
  }),
package/lib/server.es.js CHANGED
@@ -1,14 +1,15 @@
1
1
  import { __decorate } from 'tslib';
2
2
  import { provide, COMMAND_LINE_RUNNER_TOKEN, walkOfModules, getModuleParameters, commandLineListTokens as commandLineListTokens$1, Module } from '@tramvai/core';
3
- import flatten from '@tinkoff/utils/array/flatten';
4
3
  import { Container, ChildContainer, Scope, DI_TOKEN } from '@tinkoff/dippy';
5
- import { commandLineListTokens, CHILD_APP_INTERNAL_ROOT_STATE_SUBSCRIPTION_TOKEN, CHILD_APP_INTERNAL_ACTION_TOKEN, CHILD_APP_INTERNAL_CONFIG_TOKEN, IS_CHILD_APP_DI_TOKEN, CHILD_APP_INTERNAL_ROOT_DI_BORROW_TOKEN, CHILD_APP_GET_RESOLUTION_CONFIG_TOKEN, CHILD_APP_RESOLUTION_CONFIGS_TOKEN, CHILD_APP_RESOLVE_CONFIG_TOKEN, CHILD_APP_RESOLVE_BASE_URL_TOKEN, CHILD_APP_SINGLETON_DI_MANAGER_TOKEN, CHILD_APP_LOADER_TOKEN, CHILD_APP_DI_MANAGER_TOKEN, CHILD_APP_COMMAND_LINE_RUNNER_TOKEN, CHILD_APP_PRELOAD_MANAGER_TOKEN, CHILD_APP_RENDER_MANAGER_TOKEN, CHILD_APP_STATE_MANAGER_TOKEN, CHILD_APP_PRELOAD_EXTERNAL_CONFIG_TOKEN, CHILD_APP_INTERNAL_RENDER_TOKEN } from '@tramvai/tokens-child-app';
4
+ import { commandLineListTokens, CHILD_APP_INTERNAL_ROOT_STATE_SUBSCRIPTION_TOKEN, CHILD_APP_INTERNAL_ACTION_TOKEN, CHILD_APP_INTERNAL_CONFIG_TOKEN, IS_CHILD_APP_DI_TOKEN, CHILD_APP_INTERNAL_ROOT_DI_BORROW_TOKEN, CHILD_APP_RESOLUTION_CONFIG_MANAGER_TOKEN, CHILD_APP_RESOLUTION_CONFIGS_TOKEN, CHILD_APP_RESOLVE_CONFIG_TOKEN, CHILD_APP_RESOLVE_BASE_URL_TOKEN, CHILD_APP_SINGLETON_DI_MANAGER_TOKEN, CHILD_APP_LOADER_TOKEN, CHILD_APP_DI_MANAGER_TOKEN, CHILD_APP_COMMAND_LINE_RUNNER_TOKEN, CHILD_APP_PRELOAD_MANAGER_TOKEN, CHILD_APP_RENDER_MANAGER_TOKEN, CHILD_APP_STATE_MANAGER_TOKEN, CHILD_APP_INTERNAL_RENDER_TOKEN } from '@tramvai/tokens-child-app';
6
5
  export * from '@tramvai/tokens-child-app';
7
6
  import { ACTION_PAGE_RUNNER_TOKEN, CONTEXT_TOKEN, LOGGER_TOKEN, DISPATCHER_TOKEN, STORE_TOKEN, COMBINE_REDUCERS, ENV_MANAGER_TOKEN, REGISTER_CLEAR_CACHE_TOKEN, CLEAR_CACHE_TOKEN, ENV_USED_TOKEN, CREATE_CACHE_TOKEN } from '@tramvai/tokens-common';
8
7
  import { RENDER_SLOTS, EXTEND_RENDER, ResourceType, ResourceSlot, RESOURCES_REGISTRY, CUSTOM_RENDER } from '@tramvai/tokens-render';
9
8
  import { PAGE_SERVICE_TOKEN } from '@tramvai/tokens-router';
9
+ import flatten from '@tinkoff/utils/array/flatten';
10
10
  import { createEvent, createReducer } from '@tramvai/state';
11
11
  import React, { createContext, useContext, useMemo, useState, useEffect, createElement } from 'react';
12
+ import applyOrReturn from '@tinkoff/utils/function/applyOrReturn';
12
13
  import { combineValidators, isUrl, endsWith } from '@tinkoff/env-validators';
13
14
  import { safeDehydrate } from '@tramvai/safe-strings';
14
15
  import { ServerLoader as ServerLoader$1 } from '@tinkoff/module-loader-server';
@@ -296,6 +297,51 @@ const resolveComponent = async (componentOrLoader) => {
296
297
  : componentOrLoader;
297
298
  };
298
299
 
300
+ class ChildAppResolutionConfigManager {
301
+ constructor({ configs }) {
302
+ this.hasInitialized = false;
303
+ this.rawConfigs = configs !== null && configs !== void 0 ? configs : [];
304
+ this.mapping = new Map();
305
+ }
306
+ async init() {
307
+ if (this.hasInitialized) {
308
+ return;
309
+ }
310
+ if (this.initPromise) {
311
+ return this.initPromise;
312
+ }
313
+ this.initPromise = (async () => {
314
+ const configs = await Promise.all(this.rawConfigs.map((rawConfig) => {
315
+ return applyOrReturn([], rawConfig);
316
+ }));
317
+ flatten(configs).forEach((config) => {
318
+ this.mapping.set(config.name, config);
319
+ });
320
+ this.hasInitialized = true;
321
+ })();
322
+ return this.initPromise;
323
+ }
324
+ resolve({ name, version, tag = 'latest' }) {
325
+ var _a;
326
+ const fromMapping = this.mapping.get(name);
327
+ if (!fromMapping) {
328
+ return null;
329
+ }
330
+ const cfg = fromMapping.byTag[tag];
331
+ if (process.env.NODE_ENV === 'development' && tag === 'debug' && !cfg) {
332
+ return {
333
+ baseUrl: 'http://localhost:4040/',
334
+ version: '0.0.0-stub',
335
+ };
336
+ }
337
+ return {
338
+ ...cfg,
339
+ baseUrl: (_a = cfg.baseUrl) !== null && _a !== void 0 ? _a : fromMapping.baseUrl,
340
+ version: version !== null && version !== void 0 ? version : cfg.version,
341
+ };
342
+ }
343
+ }
344
+
299
345
  const sharedProviders = [
300
346
  provide({
301
347
  provide: COMBINE_REDUCERS,
@@ -308,38 +354,27 @@ const sharedProviders = [
308
354
  useValue: initModuleFederation,
309
355
  }),
310
356
  provide({
311
- provide: CHILD_APP_GET_RESOLUTION_CONFIG_TOKEN,
312
- useFactory: ({ configs }) => {
313
- const mapping = flatten(configs !== null && configs !== void 0 ? configs : []).reduce((map, config) => {
314
- return map.set(config.name, config);
315
- }, new Map());
316
- return (({ name, version, tag = 'latest' }) => {
317
- var _a;
318
- const fromMapping = mapping.get(name);
319
- if (!fromMapping) {
320
- return null;
321
- }
322
- const cfg = fromMapping.byTag[tag];
323
- if (process.env.NODE_ENV === 'development' && tag === 'debug' && !cfg) {
324
- return {
325
- baseUrl: 'http://localhost:4040/',
326
- version: '0.0.0-stub',
327
- };
328
- }
329
- return {
330
- ...cfg,
331
- baseUrl: (_a = cfg.baseUrl) !== null && _a !== void 0 ? _a : fromMapping.baseUrl,
332
- version: version !== null && version !== void 0 ? version : cfg.version,
333
- };
334
- });
335
- },
357
+ provide: CHILD_APP_RESOLUTION_CONFIG_MANAGER_TOKEN,
358
+ useClass: ChildAppResolutionConfigManager,
336
359
  deps: {
337
360
  configs: { token: CHILD_APP_RESOLUTION_CONFIGS_TOKEN, optional: true },
338
361
  },
339
362
  }),
363
+ provide({
364
+ provide: commandLineListTokens$1.resolvePageDeps,
365
+ multi: true,
366
+ useFactory: ({ resolutionConfigManager }) => {
367
+ return async function fallbackResolutionConfigManagerInit() {
368
+ await resolutionConfigManager.init();
369
+ };
370
+ },
371
+ deps: {
372
+ resolutionConfigManager: CHILD_APP_RESOLUTION_CONFIG_MANAGER_TOKEN,
373
+ },
374
+ }),
340
375
  provide({
341
376
  provide: CHILD_APP_RESOLVE_CONFIG_TOKEN,
342
- useFactory: ({ envManager, rootBaseUrl, getResolutionConfig }) => {
377
+ useFactory: ({ envManager, rootBaseUrl, resolutionConfigManager }) => {
343
378
  const rawEnv = envManager.get('CHILD_APP_DEBUG');
344
379
  const debug = new Map();
345
380
  rawEnv === null || rawEnv === void 0 ? void 0 : rawEnv.split(';').reduce((acc, entry) => {
@@ -350,7 +385,10 @@ const sharedProviders = [
350
385
  var _a, _b;
351
386
  const { name, tag = debug.has(name) ? 'debug' : 'latest' } = request;
352
387
  const req = { name, tag, version: request.version };
353
- const config = getResolutionConfig(req);
388
+ const config = resolutionConfigManager.resolve(req);
389
+ if (!config) {
390
+ throw new Error(`Child-app "${name}" with tag "${tag}" has not found`);
391
+ }
354
392
  const { version, baseUrl: configBaseUrl, client, server, css, withoutCss } = config;
355
393
  const baseUrl = (_b = (_a = debug.get(name)) !== null && _a !== void 0 ? _a : configBaseUrl) !== null && _b !== void 0 ? _b : rootBaseUrl;
356
394
  if (!baseUrl) {
@@ -382,7 +420,7 @@ const sharedProviders = [
382
420
  deps: {
383
421
  envManager: ENV_MANAGER_TOKEN,
384
422
  rootBaseUrl: CHILD_APP_RESOLVE_BASE_URL_TOKEN,
385
- getResolutionConfig: CHILD_APP_GET_RESOLUTION_CONFIG_TOKEN,
423
+ resolutionConfigManager: CHILD_APP_RESOLUTION_CONFIG_MANAGER_TOKEN,
386
424
  },
387
425
  }),
388
426
  provide({
@@ -551,23 +589,18 @@ class ServerLoader extends Loader {
551
589
  }
552
590
 
553
591
  class PreloadManager {
554
- constructor({ loader, runner, stateManager, preloadExternalConfig, resolveFullConfig, }) {
592
+ constructor({ loader, runner, stateManager, resolutionConfigManager, resolveFullConfig, }) {
555
593
  this.shouldRunImmediately = false;
556
594
  this.map = new Map();
557
595
  this.preloadMap = new Map();
558
- this.configHasBeenPreloaded = false;
559
596
  this.loader = loader;
560
597
  this.runner = runner;
561
598
  this.stateManager = stateManager;
562
- this.preloadExternalConfig = preloadExternalConfig;
599
+ this.resolutionConfigManager = resolutionConfigManager;
563
600
  this.resolveFullConfig = resolveFullConfig;
564
601
  }
565
602
  async preload(request) {
566
- var _a;
567
- if (!this.configHasBeenPreloaded) {
568
- await ((_a = this.preloadExternalConfig) === null || _a === void 0 ? void 0 : _a.call(this));
569
- this.configHasBeenPreloaded = true;
570
- }
603
+ await this.resolutionConfigManager.init();
571
604
  const config = this.resolveFullConfig(request);
572
605
  const { key } = config;
573
606
  if (this.map.has(key)) {
@@ -806,7 +839,7 @@ const serverProviders = [
806
839
  loader: CHILD_APP_LOADER_TOKEN,
807
840
  runner: CHILD_APP_COMMAND_LINE_RUNNER_TOKEN,
808
841
  stateManager: CHILD_APP_STATE_MANAGER_TOKEN,
809
- preloadExternalConfig: { token: CHILD_APP_PRELOAD_EXTERNAL_CONFIG_TOKEN, optional: true },
842
+ resolutionConfigManager: CHILD_APP_RESOLUTION_CONFIG_MANAGER_TOKEN,
810
843
  resolveFullConfig: CHILD_APP_RESOLVE_CONFIG_TOKEN,
811
844
  },
812
845
  }),
package/lib/server.js CHANGED
@@ -4,14 +4,15 @@ Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var tslib = require('tslib');
6
6
  var core = require('@tramvai/core');
7
- var flatten = require('@tinkoff/utils/array/flatten');
8
7
  var dippy = require('@tinkoff/dippy');
9
8
  var tokensChildApp = require('@tramvai/tokens-child-app');
10
9
  var tokensCommon = require('@tramvai/tokens-common');
11
10
  var tokensRender = require('@tramvai/tokens-render');
12
11
  var tokensRouter = require('@tramvai/tokens-router');
12
+ var flatten = require('@tinkoff/utils/array/flatten');
13
13
  var state = require('@tramvai/state');
14
14
  var React = require('react');
15
+ var applyOrReturn = require('@tinkoff/utils/function/applyOrReturn');
15
16
  var envValidators = require('@tinkoff/env-validators');
16
17
  var safeStrings = require('@tramvai/safe-strings');
17
18
  var moduleLoaderServer = require('@tinkoff/module-loader-server');
@@ -23,6 +24,7 @@ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'defau
23
24
 
24
25
  var flatten__default = /*#__PURE__*/_interopDefaultLegacy(flatten);
25
26
  var React__default = /*#__PURE__*/_interopDefaultLegacy(React);
27
+ var applyOrReturn__default = /*#__PURE__*/_interopDefaultLegacy(applyOrReturn);
26
28
  var noop__default = /*#__PURE__*/_interopDefaultLegacy(noop);
27
29
 
28
30
  const getChildProviders$1 = (appDi) => {
@@ -305,6 +307,51 @@ const resolveComponent = async (componentOrLoader) => {
305
307
  : componentOrLoader;
306
308
  };
307
309
 
310
+ class ChildAppResolutionConfigManager {
311
+ constructor({ configs }) {
312
+ this.hasInitialized = false;
313
+ this.rawConfigs = configs !== null && configs !== void 0 ? configs : [];
314
+ this.mapping = new Map();
315
+ }
316
+ async init() {
317
+ if (this.hasInitialized) {
318
+ return;
319
+ }
320
+ if (this.initPromise) {
321
+ return this.initPromise;
322
+ }
323
+ this.initPromise = (async () => {
324
+ const configs = await Promise.all(this.rawConfigs.map((rawConfig) => {
325
+ return applyOrReturn__default["default"]([], rawConfig);
326
+ }));
327
+ flatten__default["default"](configs).forEach((config) => {
328
+ this.mapping.set(config.name, config);
329
+ });
330
+ this.hasInitialized = true;
331
+ })();
332
+ return this.initPromise;
333
+ }
334
+ resolve({ name, version, tag = 'latest' }) {
335
+ var _a;
336
+ const fromMapping = this.mapping.get(name);
337
+ if (!fromMapping) {
338
+ return null;
339
+ }
340
+ const cfg = fromMapping.byTag[tag];
341
+ if (process.env.NODE_ENV === 'development' && tag === 'debug' && !cfg) {
342
+ return {
343
+ baseUrl: 'http://localhost:4040/',
344
+ version: '0.0.0-stub',
345
+ };
346
+ }
347
+ return {
348
+ ...cfg,
349
+ baseUrl: (_a = cfg.baseUrl) !== null && _a !== void 0 ? _a : fromMapping.baseUrl,
350
+ version: version !== null && version !== void 0 ? version : cfg.version,
351
+ };
352
+ }
353
+ }
354
+
308
355
  const sharedProviders = [
309
356
  core.provide({
310
357
  provide: tokensCommon.COMBINE_REDUCERS,
@@ -317,38 +364,27 @@ const sharedProviders = [
317
364
  useValue: initModuleFederation,
318
365
  }),
319
366
  core.provide({
320
- provide: tokensChildApp.CHILD_APP_GET_RESOLUTION_CONFIG_TOKEN,
321
- useFactory: ({ configs }) => {
322
- const mapping = flatten__default["default"](configs !== null && configs !== void 0 ? configs : []).reduce((map, config) => {
323
- return map.set(config.name, config);
324
- }, new Map());
325
- return (({ name, version, tag = 'latest' }) => {
326
- var _a;
327
- const fromMapping = mapping.get(name);
328
- if (!fromMapping) {
329
- return null;
330
- }
331
- const cfg = fromMapping.byTag[tag];
332
- if (process.env.NODE_ENV === 'development' && tag === 'debug' && !cfg) {
333
- return {
334
- baseUrl: 'http://localhost:4040/',
335
- version: '0.0.0-stub',
336
- };
337
- }
338
- return {
339
- ...cfg,
340
- baseUrl: (_a = cfg.baseUrl) !== null && _a !== void 0 ? _a : fromMapping.baseUrl,
341
- version: version !== null && version !== void 0 ? version : cfg.version,
342
- };
343
- });
344
- },
367
+ provide: tokensChildApp.CHILD_APP_RESOLUTION_CONFIG_MANAGER_TOKEN,
368
+ useClass: ChildAppResolutionConfigManager,
345
369
  deps: {
346
370
  configs: { token: tokensChildApp.CHILD_APP_RESOLUTION_CONFIGS_TOKEN, optional: true },
347
371
  },
348
372
  }),
373
+ core.provide({
374
+ provide: core.commandLineListTokens.resolvePageDeps,
375
+ multi: true,
376
+ useFactory: ({ resolutionConfigManager }) => {
377
+ return async function fallbackResolutionConfigManagerInit() {
378
+ await resolutionConfigManager.init();
379
+ };
380
+ },
381
+ deps: {
382
+ resolutionConfigManager: tokensChildApp.CHILD_APP_RESOLUTION_CONFIG_MANAGER_TOKEN,
383
+ },
384
+ }),
349
385
  core.provide({
350
386
  provide: tokensChildApp.CHILD_APP_RESOLVE_CONFIG_TOKEN,
351
- useFactory: ({ envManager, rootBaseUrl, getResolutionConfig }) => {
387
+ useFactory: ({ envManager, rootBaseUrl, resolutionConfigManager }) => {
352
388
  const rawEnv = envManager.get('CHILD_APP_DEBUG');
353
389
  const debug = new Map();
354
390
  rawEnv === null || rawEnv === void 0 ? void 0 : rawEnv.split(';').reduce((acc, entry) => {
@@ -359,7 +395,10 @@ const sharedProviders = [
359
395
  var _a, _b;
360
396
  const { name, tag = debug.has(name) ? 'debug' : 'latest' } = request;
361
397
  const req = { name, tag, version: request.version };
362
- const config = getResolutionConfig(req);
398
+ const config = resolutionConfigManager.resolve(req);
399
+ if (!config) {
400
+ throw new Error(`Child-app "${name}" with tag "${tag}" has not found`);
401
+ }
363
402
  const { version, baseUrl: configBaseUrl, client, server, css, withoutCss } = config;
364
403
  const baseUrl = (_b = (_a = debug.get(name)) !== null && _a !== void 0 ? _a : configBaseUrl) !== null && _b !== void 0 ? _b : rootBaseUrl;
365
404
  if (!baseUrl) {
@@ -391,7 +430,7 @@ const sharedProviders = [
391
430
  deps: {
392
431
  envManager: tokensCommon.ENV_MANAGER_TOKEN,
393
432
  rootBaseUrl: tokensChildApp.CHILD_APP_RESOLVE_BASE_URL_TOKEN,
394
- getResolutionConfig: tokensChildApp.CHILD_APP_GET_RESOLUTION_CONFIG_TOKEN,
433
+ resolutionConfigManager: tokensChildApp.CHILD_APP_RESOLUTION_CONFIG_MANAGER_TOKEN,
395
434
  },
396
435
  }),
397
436
  core.provide({
@@ -560,23 +599,18 @@ class ServerLoader extends Loader {
560
599
  }
561
600
 
562
601
  class PreloadManager {
563
- constructor({ loader, runner, stateManager, preloadExternalConfig, resolveFullConfig, }) {
602
+ constructor({ loader, runner, stateManager, resolutionConfigManager, resolveFullConfig, }) {
564
603
  this.shouldRunImmediately = false;
565
604
  this.map = new Map();
566
605
  this.preloadMap = new Map();
567
- this.configHasBeenPreloaded = false;
568
606
  this.loader = loader;
569
607
  this.runner = runner;
570
608
  this.stateManager = stateManager;
571
- this.preloadExternalConfig = preloadExternalConfig;
609
+ this.resolutionConfigManager = resolutionConfigManager;
572
610
  this.resolveFullConfig = resolveFullConfig;
573
611
  }
574
612
  async preload(request) {
575
- var _a;
576
- if (!this.configHasBeenPreloaded) {
577
- await ((_a = this.preloadExternalConfig) === null || _a === void 0 ? void 0 : _a.call(this));
578
- this.configHasBeenPreloaded = true;
579
- }
613
+ await this.resolutionConfigManager.init();
580
614
  const config = this.resolveFullConfig(request);
581
615
  const { key } = config;
582
616
  if (this.map.has(key)) {
@@ -815,7 +849,7 @@ const serverProviders = [
815
849
  loader: tokensChildApp.CHILD_APP_LOADER_TOKEN,
816
850
  runner: tokensChildApp.CHILD_APP_COMMAND_LINE_RUNNER_TOKEN,
817
851
  stateManager: tokensChildApp.CHILD_APP_STATE_MANAGER_TOKEN,
818
- preloadExternalConfig: { token: tokensChildApp.CHILD_APP_PRELOAD_EXTERNAL_CONFIG_TOKEN, optional: true },
852
+ resolutionConfigManager: tokensChildApp.CHILD_APP_RESOLUTION_CONFIG_MANAGER_TOKEN,
819
853
  resolveFullConfig: tokensChildApp.CHILD_APP_RESOLVE_CONFIG_TOKEN,
820
854
  },
821
855
  }),
@@ -0,0 +1,14 @@
1
+ import type { ChildAppRequestConfig, CHILD_APP_RESOLUTION_CONFIGS_TOKEN, CHILD_APP_RESOLUTION_CONFIG_MANAGER_TOKEN, ResolutionConfig } from '@tramvai/tokens-child-app';
2
+ declare type Interface = typeof CHILD_APP_RESOLUTION_CONFIG_MANAGER_TOKEN;
3
+ export declare class ChildAppResolutionConfigManager implements Interface {
4
+ private rawConfigs;
5
+ private mapping;
6
+ private hasInitialized;
7
+ private initPromise;
8
+ constructor({ configs }: {
9
+ configs: typeof CHILD_APP_RESOLUTION_CONFIGS_TOKEN | null;
10
+ });
11
+ init(): Promise<void>;
12
+ resolve({ name, version, tag }: ChildAppRequestConfig): ResolutionConfig;
13
+ }
14
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tramvai/module-child-app",
3
- "version": "1.65.1",
3
+ "version": "1.66.0",
4
4
  "description": "Module for child apps",
5
5
  "browser": {
6
6
  "./lib/server.js": "./lib/browser.js",
@@ -30,20 +30,20 @@
30
30
  "@tinkoff/env-validators": "0.0.3",
31
31
  "@tinkoff/module-loader-client": "0.3.25",
32
32
  "@tinkoff/module-loader-server": "0.4.41",
33
- "@tramvai/child-app-core": "1.65.1",
33
+ "@tramvai/child-app-core": "1.66.0",
34
34
  "@tramvai/safe-strings": "0.4.3",
35
- "@tramvai/tokens-child-app": "1.65.1"
35
+ "@tramvai/tokens-child-app": "1.66.0"
36
36
  },
37
37
  "devDependencies": {},
38
38
  "peerDependencies": {
39
39
  "@tinkoff/dippy": "0.7.38",
40
40
  "@tinkoff/utils": "^2.1.2",
41
- "@tramvai/core": "1.65.1",
42
- "@tramvai/state": "1.65.1",
43
- "@tramvai/react": "1.65.1",
44
- "@tramvai/tokens-common": "1.65.1",
45
- "@tramvai/tokens-render": "1.65.1",
46
- "@tramvai/tokens-router": "1.65.1",
41
+ "@tramvai/core": "1.66.0",
42
+ "@tramvai/state": "1.66.0",
43
+ "@tramvai/react": "1.66.0",
44
+ "@tramvai/tokens-common": "1.66.0",
45
+ "@tramvai/tokens-render": "1.66.0",
46
+ "@tramvai/tokens-router": "1.66.0",
47
47
  "react": ">=16.8.0",
48
48
  "react-dom": ">=16.8.0",
49
49
  "object-assign": "^4.1.1",