piral-ng 0.15.0-beta.4808 → 0.15.0-beta.4813

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "piral-ng",
3
- "version": "0.15.0-beta.4808",
3
+ "version": "0.15.0-beta.4813",
4
4
  "description": "Plugin for integrating Angular components in Piral.",
5
5
  "keywords": [
6
6
  "piral",
@@ -79,7 +79,7 @@
79
79
  "@angular/platform-browser": "^14.0.0",
80
80
  "@angular/platform-browser-dynamic": "^14.0.0",
81
81
  "@angular/router": "^14.0.0",
82
- "piral-core": "0.15.0-beta.4808",
82
+ "piral-core": "0.15.0-beta.4813",
83
83
  "rxjs": "^7.3.0"
84
84
  },
85
85
  "peerDependencies": {
@@ -90,5 +90,5 @@
90
90
  "@angular/router": "^2.0.0 || ^4.0.0 || ^5.0.0 || ^6.0.0 || ^7.0.0 || ^8.0.0 || ^9.0.0 || ^10.0.0 || ^11.0.0 || ^12.0.0 || ^13.0.0 || ^14.0.0",
91
91
  "rxjs": "^5.0.0 || ^6.0.0 || ^7.0.0"
92
92
  },
93
- "gitHead": "27e68a7d947210860a3bc6025b6ab569ae2f69fc"
93
+ "gitHead": "2790199e9c9e3792899f3325f0584d97af77e795"
94
94
  }
@@ -1,7 +1,45 @@
1
1
  import type { Subscription } from 'rxjs';
2
2
  import type { ComponentContext, Disposable } from 'piral-core';
3
3
  import { Inject, Injectable, NgZone, OnDestroy, Optional } from '@angular/core';
4
- import { NavigationError, Router, Scroll } from '@angular/router';
4
+ import { NavigationError, NavigationStart, Router, Scroll } from '@angular/router';
5
+ import { ɵBrowserPlatformLocation } from '@angular/common';
6
+
7
+ let skipNavigation = false;
8
+ const noop = function () {};
9
+ const navigateByUrl = Router.prototype.navigateByUrl;
10
+
11
+ // deactivates the usual platform behavior; all these operations are performed via the RoutingService
12
+ // to avoid any conflict, e.g., double-booking URL changes in React and Angular
13
+ ɵBrowserPlatformLocation.prototype.pushState = noop;
14
+ ɵBrowserPlatformLocation.prototype.replaceState = noop;
15
+ ɵBrowserPlatformLocation.prototype.forward = noop;
16
+ ɵBrowserPlatformLocation.prototype.back = noop;
17
+ ɵBrowserPlatformLocation.prototype.historyGo = noop;
18
+
19
+ // required to detect / react to hidden URL change (see #554 for details)
20
+ Router.prototype.navigateByUrl = function (url, extras) {
21
+ skipNavigation = extras?.skipLocationChange ?? false;
22
+ const result = navigateByUrl.call(this, url, extras);
23
+ skipNavigation = false;
24
+ return result;
25
+ };
26
+
27
+ function normalize(url: string) {
28
+ const search = url.indexOf('?');
29
+ const hash = url.indexOf('#');
30
+
31
+ if (search !== -1 || hash !== -1) {
32
+ if (search === -1) {
33
+ return url.substring(0, hash);
34
+ } else if (hash === -1) {
35
+ return url.substring(0, search);
36
+ } else {
37
+ return url.substring(0, Math.min(search, hash));
38
+ }
39
+ }
40
+
41
+ return url;
42
+ }
5
43
 
6
44
  @Injectable()
7
45
  export class RoutingService implements OnDestroy {
@@ -23,34 +61,47 @@ export class RoutingService implements OnDestroy {
23
61
  throw error;
24
62
  };
25
63
 
64
+ const skipIds: Array<number> = [];
26
65
  const nav = this.context.navigation;
27
66
 
28
67
  this.dispose = nav.listen(({ location }) => {
29
68
  const path = location.pathname;
30
69
 
31
70
  if (!this.invalidRoutes.includes(path)) {
32
- const url = path + location.search + location.hash;
33
- this.zone.run(() => this.router.navigateByUrl(url));
71
+ const url = `${path}${location.search}${location.hash}`;
72
+ this.zone.run(() => navigateByUrl.call(this.router, url));
34
73
  }
35
74
  });
36
75
 
37
- this.subscription = this.router.events.subscribe((e: NavigationError | Scroll) => {
76
+ this.subscription = this.router.events.subscribe((e: NavigationError | NavigationStart | Scroll) => {
38
77
  if (e instanceof NavigationError) {
39
- const path = e.url;
78
+ const routerUrl = e.url;
79
+ const path = normalize(routerUrl);
80
+ const locationUrl = nav.url;
40
81
 
41
82
  if (!this.invalidRoutes.includes(path)) {
42
83
  this.invalidRoutes.push(path);
43
84
  }
44
85
 
45
- nav.push(path);
46
- } else if (e.type === 15) {
47
- // consistency check to avoid #535 and other Angular-specific issues
48
- const locationUrl = nav.path;
49
- const routerUrl = e.routerEvent.url;
50
-
51
86
  if (routerUrl !== locationUrl) {
52
87
  nav.push(routerUrl);
53
88
  }
89
+ } else if (e.type === 0 && skipNavigation) {
90
+ skipIds.push(e.id);
91
+ } else if (e.type === 15) {
92
+ const index = skipIds.indexOf(e.routerEvent.id);
93
+
94
+ if (index === -1) {
95
+ // consistency check to avoid #535 and other Angular-specific issues
96
+ const locationUrl = nav.url;
97
+ const routerUrl = e.routerEvent.url;
98
+
99
+ if (routerUrl !== locationUrl) {
100
+ nav.push(routerUrl);
101
+ }
102
+ } else {
103
+ skipIds.splice(index, 1);
104
+ }
54
105
  }
55
106
  });
56
107
  }
@@ -1,5 +1,5 @@
1
1
  import { CommonModule } from '@angular/common';
2
- import { NgModule } from '@angular/core';
2
+ import { NgModule, ɵɵFactoryDeclaration, ɵɵInjectorDeclaration, ɵɵNgModuleDeclaration } from '@angular/core';
3
3
  import { NgExtension } from './NgExtension';
4
4
  import { ResourceUrlPipe } from './ResourceUrlPipe';
5
5
 
@@ -15,4 +15,15 @@ const importsDef = [CommonModule];
15
15
  })
16
16
  export class SharedModule {
17
17
  static props = {};
18
+
19
+ static ɵfac: ɵɵFactoryDeclaration<SharedModule, never>;
20
+
21
+ static ɵmod: ɵɵNgModuleDeclaration<
22
+ SharedModule,
23
+ [typeof NgExtension, typeof ResourceUrlPipe],
24
+ [typeof CommonModule],
25
+ [typeof NgExtension, typeof ResourceUrlPipe]
26
+ >;
27
+
28
+ static ɵinj: ɵɵInjectorDeclaration<SharedModule>;
18
29
  }
package/src/bootstrap.ts CHANGED
@@ -1,28 +1,55 @@
1
1
  import type { BaseComponentProps, ComponentContext, Disposable, PiletApi } from 'piral-core';
2
2
  import type { BehaviorSubject } from 'rxjs';
3
- import type { PrepareBootstrapResult } from './types';
3
+ import type { Type } from '@angular/core';
4
+ import type { NgLazyType, PrepareBootstrapResult } from './types';
5
+ import { createModuleInstance, getModuleInstance, defineModule, findModule, activateModuleInstance } from './module';
6
+ import { getAnnotations, hasSelector } from './utils';
4
7
  import { startup } from './startup';
5
- import { getAnnotations } from './utils';
6
- import { createModuleInstance, getModuleInstance, defineModule } from './module';
7
-
8
- export function prepareBootstrap(moduleOrComponent: any, piral: PiletApi): PrepareBootstrapResult {
9
- const [annotation] = getAnnotations(moduleOrComponent);
10
- const standalone = annotation?.standalone;
11
-
12
- // first way is to directly use a module, which is the legacy way
13
- // second way is to find a previously defined Angular module
14
- if (annotation && annotation.bootstrap) {
15
- // usually contains things like imports, exports, declarations, ...
16
- const [component] = annotation.bootstrap;
17
- annotation.exports = [component];
18
- defineModule(moduleOrComponent);
19
- return [...getModuleInstance(component, standalone, piral), component];
8
+
9
+ export async function prepareBootstrap(
10
+ moduleOrComponent: Type<any> | NgLazyType,
11
+ piral: PiletApi,
12
+ ): Promise<PrepareBootstrapResult> {
13
+ if ('module' in moduleOrComponent && typeof moduleOrComponent.module === 'function') {
14
+ if (!(moduleOrComponent.state.current instanceof Promise)) {
15
+ moduleOrComponent.state.current = moduleOrComponent.module().then((result) => {
16
+ if (typeof result !== 'object' || !('default' in result)) {
17
+ throw new Error('The lazy loaded module does not `default` export a NgModule class.');
18
+ }
19
+
20
+ defineModule(result.default, moduleOrComponent.opts);
21
+ return findModule(result.default);
22
+ });
23
+ }
24
+
25
+ const moduleDef = await moduleOrComponent.state.current;
26
+ const { components } = moduleDef;
27
+ const component = components.find((m) => hasSelector(m, moduleOrComponent.selector));
28
+
29
+ if (!component) {
30
+ throw new Error(`No component matching the selector "${moduleOrComponent.selector}" has been found.`);
31
+ }
32
+
33
+ return [...activateModuleInstance(moduleDef, piral), component];
20
34
  } else {
21
- // usually contains things like selector, template or templateUrl, changeDetection, ...
22
- const result =
23
- getModuleInstance(moduleOrComponent, standalone, piral) ||
24
- createModuleInstance(moduleOrComponent, standalone, piral);
25
- return [...result, moduleOrComponent];
35
+ const [annotation] = getAnnotations(moduleOrComponent);
36
+ const standalone = annotation?.standalone;
37
+
38
+ // first way is to directly use a module, which is the legacy way
39
+ // second way is to find a previously defined Angular module
40
+ if (annotation && annotation.bootstrap) {
41
+ // usually contains things like imports, exports, declarations, ...
42
+ const [component] = annotation.bootstrap;
43
+ annotation.exports = [component];
44
+ defineModule(moduleOrComponent);
45
+ return [...getModuleInstance(component, standalone, piral), component];
46
+ } else {
47
+ // usually contains things like selector, template or templateUrl, changeDetection, ...
48
+ const result =
49
+ getModuleInstance(moduleOrComponent, standalone, piral) ||
50
+ createModuleInstance(moduleOrComponent, standalone, piral);
51
+ return [...result, moduleOrComponent];
52
+ }
26
53
  }
27
54
  }
28
55
 
package/src/converter.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import type { ForeignComponent, BaseComponentProps, Disposable } from 'piral-core';
2
- import type { NgModuleDefiner, PrepareBootstrapResult } from './types';
2
+ import type { Type } from '@angular/core';
3
+ import type { NgLazyType, NgModuleDefiner, PrepareBootstrapResult } from './types';
3
4
  import { BehaviorSubject } from 'rxjs';
4
5
  import { NgExtension } from './NgExtension';
5
6
  import { enqueue } from './queue';
@@ -22,14 +23,10 @@ interface NgState<TProps> {
22
23
 
23
24
  export function createConverter(_: NgConverterOptions = {}): NgConverter {
24
25
  const registry = new Map<any, PrepareBootstrapResult>();
25
- const convert = <TProps extends BaseComponentProps>(component: any): ForeignComponent<TProps> => ({
26
+ const convert = <TProps extends BaseComponentProps>(component: Type<any> | NgLazyType): ForeignComponent<TProps> => ({
26
27
  mount(el, props, ctx, locals: NgState<TProps>) {
27
28
  locals.active = true;
28
29
 
29
- if (!registry.has(component)) {
30
- registry.set(component, prepareBootstrap(component, props.piral));
31
- }
32
-
33
30
  if (!locals.props) {
34
31
  locals.props = new BehaviorSubject(props);
35
32
  }
@@ -39,7 +36,15 @@ export function createConverter(_: NgConverterOptions = {}): NgConverter {
39
36
  }
40
37
 
41
38
  locals.queued = locals.queued.then(() =>
42
- enqueue(() => locals.active && bootstrap(registry.get(component), el, locals.props, ctx)),
39
+ enqueue(async () => {
40
+ if (!registry.has(component)) {
41
+ registry.set(component, await prepareBootstrap(component, props.piral));
42
+ }
43
+
44
+ if (locals.active) {
45
+ bootstrap(registry.get(component), el, locals.props, ctx);
46
+ }
47
+ }),
43
48
  );
44
49
  },
45
50
  update(el, props, ctx, locals: NgState<TProps>) {
@@ -47,7 +52,7 @@ export function createConverter(_: NgConverterOptions = {}): NgConverter {
47
52
  },
48
53
  unmount(el, locals: NgState<TProps>) {
49
54
  locals.active = false;
50
- locals.queued = locals.queued.then((dispose) => enqueue(() => dispose && dispose()));
55
+ locals.queued = locals.queued.then((dispose) => dispose && enqueue(dispose));
51
56
  },
52
57
  });
53
58
  convert.defineModule = defineModule;
package/src/module.ts CHANGED
@@ -89,15 +89,19 @@ function instantiateModule(moduleDef: ModuleDefinition, piral: PiletApi) {
89
89
  return BootstrapModule;
90
90
  }
91
91
 
92
- export function getModuleInstance(component: any, standalone: boolean, piral: PiletApi): ModuleInstanceResult {
92
+ export function activateModuleInstance(moduleDef: ModuleDefinition, piral: PiletApi): ModuleInstanceResult {
93
+ if (!moduleDef.active) {
94
+ moduleDef.active = instantiateModule(moduleDef, piral);
95
+ }
96
+
97
+ return [moduleDef.active, moduleDef.opts];
98
+ }
99
+
100
+ export function getModuleInstance(component: any, standalone: boolean, piral: PiletApi) {
93
101
  const [moduleDef] = availableModules.filter((m) => m.components.includes(component));
94
102
 
95
103
  if (moduleDef) {
96
- if (!moduleDef.active) {
97
- moduleDef.active = instantiateModule(moduleDef, piral);
98
- }
99
-
100
- return [moduleDef.active, moduleDef.opts];
104
+ return activateModuleInstance(moduleDef, piral);
101
105
  }
102
106
 
103
107
  if (process.env.NODE_ENV === 'development') {
@@ -131,12 +135,28 @@ export function createModuleInstance(component: any, standalone: boolean, piral:
131
135
  return getModuleInstance(component, standalone, piral);
132
136
  }
133
137
 
138
+ export function findModule(module: any) {
139
+ return availableModules.find(m => m.module === module);
140
+ }
141
+
134
142
  export function defineModule(module: any, opts: NgOptions = undefined) {
135
143
  const [annotation] = getAnnotations(module);
136
- availableModules.push({
137
- active: undefined,
138
- components: findComponents(annotation.exports),
139
- module,
140
- opts,
141
- });
144
+
145
+ if (annotation) {
146
+ availableModules.push({
147
+ active: undefined,
148
+ components: findComponents(annotation.exports),
149
+ module,
150
+ opts,
151
+ });
152
+ } else if (typeof module === 'function') {
153
+ const state = {
154
+ current: undefined,
155
+ };
156
+
157
+ return (selector: string) => ({
158
+ component: { selector, module, opts, state },
159
+ type: 'ng' as const,
160
+ });
161
+ }
142
162
  }
package/src/startup.ts CHANGED
@@ -3,7 +3,7 @@ import type { NgOptions } from './types';
3
3
  import { enableProdMode, NgModuleRef, NgZone, PlatformRef } from '@angular/core';
4
4
  import { APP_BASE_HREF } from '@angular/common';
5
5
  import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
6
- import { getNgVersion } from './utils';
6
+ import { getId, getNgVersion } from './utils';
7
7
 
8
8
  function getVersionHandler(versions: Record<string, () => void>) {
9
9
  const major = getNgVersion();
@@ -13,15 +13,55 @@ function getVersionHandler(versions: Record<string, () => void>) {
13
13
 
14
14
  const runningModules: Array<[any, NgModuleInt, PlatformRef]> = [];
15
15
 
16
+ function startNew(BootstrapModule: any, context: ComponentContext, ngOptions?: NgOptions) {
17
+ const path = context.publicPath || '/';
18
+ const platform = platformBrowserDynamic([
19
+ { provide: 'Context', useValue: context },
20
+ { provide: APP_BASE_HREF, useValue: path },
21
+ ]);
22
+ const id = getId();
23
+ const zoneIdentifier = `piral-ng:${id}`;
24
+
25
+ // This is a hack, since NgZone doesn't allow you to configure the property that identifies your zone.
26
+ // See:
27
+ // - https://github.com/PlaceMe-SAS/single-spa-angular-cli/issues/33
28
+ // - https://github.com/angular/angular/blob/a14dc2d7a4821a19f20a9547053a5734798f541e/packages/core/src/zone/ng_zone.ts#L144
29
+ // - https://github.com/angular/angular/blob/a14dc2d7a4821a19f20a9547053a5734798f541e/packages/core/src/zone/ng_zone.ts#L257
30
+ // @ts-ignore
31
+ NgZone.isInAngularZone = () => window.Zone.current._properties[zoneIdentifier] === true;
32
+
33
+ return platform
34
+ .bootstrapModule(BootstrapModule, ngOptions)
35
+ .catch((err) => console.log(err))
36
+ .then((instance: NgModuleInt) => {
37
+ if (instance) {
38
+ const zone = instance.injector.get(NgZone);
39
+ // @ts-ignore
40
+ const z = zone?._inner ?? zone?.inner;
41
+
42
+ if (z && '_properties' in z) {
43
+ z._properties[zoneIdentifier] = true;
44
+ }
45
+
46
+ runningModules.push([BootstrapModule, instance, platform]);
47
+ }
48
+
49
+ return instance;
50
+ });
51
+ }
52
+
16
53
  export type NgModuleInt = NgModuleRef<any> & { _destroyed: boolean };
17
54
 
18
55
  export function teardown(BootstrapModule: any) {
19
56
  const runningModuleIndex = runningModules.findIndex(([ref]) => ref === BootstrapModule);
20
57
 
21
58
  if (runningModuleIndex !== -1) {
22
- const [,,platform] = runningModules[runningModuleIndex];
59
+ const [, , platform] = runningModules[runningModuleIndex];
23
60
  runningModules.splice(runningModuleIndex, 1);
24
- platform.destroy();
61
+
62
+ if (!platform.destroyed) {
63
+ platform.destroy();
64
+ }
25
65
  }
26
66
  }
27
67
 
@@ -33,44 +73,16 @@ export function startup(
33
73
  const runningModule = runningModules.find(([ref]) => ref === BootstrapModule);
34
74
 
35
75
  if (runningModule) {
36
- const [, instance] = runningModule;
37
- return Promise.resolve(instance);
38
- } else {
39
- const path = context.publicPath || '/';
40
- const platform = platformBrowserDynamic([
41
- { provide: 'Context', useValue: context },
42
- { provide: APP_BASE_HREF, useValue: path },
43
- ]);
44
- const id = Math.random().toString(36);
45
- const zoneIdentifier = `piral-ng:${id}`;
46
-
47
- // This is a hack, since NgZone doesn't allow you to configure the property that identifies your zone.
48
- // See:
49
- // - https://github.com/PlaceMe-SAS/single-spa-angular-cli/issues/33
50
- // - https://github.com/angular/angular/blob/a14dc2d7a4821a19f20a9547053a5734798f541e/packages/core/src/zone/ng_zone.ts#L144
51
- // - https://github.com/angular/angular/blob/a14dc2d7a4821a19f20a9547053a5734798f541e/packages/core/src/zone/ng_zone.ts#L257
52
- // @ts-ignore
53
- NgZone.isInAngularZone = () => window.Zone.current._properties[zoneIdentifier] === true;
54
-
55
- return platform
56
- .bootstrapModule(BootstrapModule, ngOptions)
57
- .catch((err) => console.log(err))
58
- .then((instance: NgModuleInt) => {
59
- if (instance) {
60
- const zone = instance.injector.get(NgZone);
61
- // @ts-ignore
62
- const z = zone?._inner ?? zone?.inner;
63
-
64
- if (z && '_properties' in z) {
65
- z._properties[zoneIdentifier] = true;
66
- }
67
-
68
- runningModules.push([BootstrapModule, instance, platform]);
69
- }
76
+ const [, instance, platform] = runningModule;
70
77
 
71
- return instance;
72
- });
78
+ if (platform.destroyed) {
79
+ teardown(BootstrapModule);
80
+ } else {
81
+ return Promise.resolve(instance);
82
+ }
73
83
  }
84
+
85
+ return startNew(BootstrapModule, context, ngOptions);
74
86
  }
75
87
 
76
88
  if (process.env.NODE_ENV === 'development') {
package/src/types.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import type { PlatformRef, NgModuleRef } from '@angular/core';
2
2
  import type { ForeignComponent } from 'piral-core';
3
+ import type { Type } from '@angular/core';
3
4
 
4
5
  declare module 'piral-core/lib/types/custom' {
5
6
  interface PiletCustomApi extends PiletNgApi {}
@@ -23,6 +24,24 @@ export type PrepareBootstrapResult = [...ModuleInstanceResult, any];
23
24
 
24
25
  export type NgModuleInt = NgModuleRef<any> & { _destroyed: boolean };
25
26
 
27
+ /**
28
+ * Gives you the ability to use a component from a lazy loaded module.
29
+ */
30
+ export interface NgComponentLoader {
31
+ /**
32
+ * Uses a component from a lazy loaded module.
33
+ * @param selector The selector defined for the component to load.
34
+ */
35
+ (selector: string): NgComponent;
36
+ }
37
+
38
+ export interface NgLazyType {
39
+ selector: string;
40
+ module: () => Promise<{ default: Type<any> }>;
41
+ opts: NgOptions;
42
+ state: any;
43
+ }
44
+
26
45
  /**
27
46
  * Represents the interface implemented by a module definer function.
28
47
  */
@@ -32,14 +51,21 @@ export interface NgModuleDefiner {
32
51
  * @param ngModule The module to use for running Angular.
33
52
  * @param opts The options to pass when bootstrapping.
34
53
  */
35
- (module: any, opts?: NgOptions): void;
54
+ <T>(module: Type<T>, opts?: NgOptions): void;
55
+ /**
56
+ * Defines the module to lazy load for bootstrapping the Angular pilet.
57
+ * @param getModule The module lazy loader to use for running Angular.
58
+ * @param opts The options to pass when bootstrapping.
59
+ * @returns The module ID to be used to reference components.
60
+ */
61
+ <T>(getModule: () => Promise<{ default: Type<T> }>, opts?: NgOptions): NgComponentLoader;
36
62
  }
37
63
 
38
64
  export interface NgComponent {
39
65
  /**
40
66
  * The component root.
41
67
  */
42
- component: any;
68
+ component: Type<any> | NgLazyType;
43
69
  /**
44
70
  * The type of the Angular component.
45
71
  */
@@ -62,7 +88,7 @@ export interface PiletNgApi {
62
88
  * @param component The component root.
63
89
  * @returns The Piral Ng component.
64
90
  */
65
- fromNg(component: any): NgComponent;
91
+ fromNg<T>(component: Type<T>): NgComponent;
66
92
  /**
67
93
  * Angular component for displaying extensions of the given name.
68
94
  */
package/src/utils.ts CHANGED
@@ -12,6 +12,10 @@ export interface NgAnnotation {
12
12
  selector: string;
13
13
  }
14
14
 
15
+ export function getId() {
16
+ return Math.random().toString(36);
17
+ }
18
+
15
19
  export function getNgVersion() {
16
20
  return VERSION.major || VERSION.full.split('.')[0];
17
21
  }
@@ -39,6 +43,11 @@ export function getAnnotations(component: any): Array<NgAnnotation> {
39
43
  return annotations || [];
40
44
  }
41
45
 
46
+ export function hasSelector(component: any, selector: string) {
47
+ const [annotation] = getAnnotations(component);
48
+ return annotation && annotation.selector === selector;
49
+ }
50
+
42
51
  export function findComponents(exports: Array<any>): Array<any> {
43
52
  const components = [];
44
53