piral-ng
Version:
Plugin for integrating Angular components in Piral.
80 lines • 3.47 kB
JavaScript
import { APP_BASE_HREF } from '@angular/common';
import { createApplication } from '@angular/platform-browser';
import { ɵresetCompiledComponents as reset, } from '@angular/core';
import { CoreRoutingService } from './CoreRoutingService';
import { contextName, piralName } from './constants';
import { CONTEXT, PIRAL } from './injection';
function isLazyLoader(thing) {
// Since both possible inputs, i.e., both loader functions and Angular components, are technically functions, we:
// a) check the number of args - a loader fn is expected to have no parameters.
// b) check for Angular's component metadata. Loader functions don't have those.
return (typeof thing === 'function' && thing.length === 0 && !(thing.hasOwnProperty('ɵcmp') || thing.hasOwnProperty('ɵfac')));
}
export * from './injection';
export function createConverter(options) {
const update = (ref, props) => {
if (ref) {
const ct = ref.componentType;
if (ct?.ɵcmp?.inputs?.Props) {
ref.setInput('Props', props);
}
}
};
let app = undefined;
return (component) => ({
type: 'html',
component: {
mount(element, props, ctx, locals) {
if (!app) {
const { piral } = props;
app = createApplication({
...options,
providers: [
...options.providers,
CoreRoutingService,
{ provide: APP_BASE_HREF, useValue: ctx.publicPath },
{ provide: contextName, useValue: ctx },
{ provide: CONTEXT, useValue: ctx },
{ provide: piralName, useValue: piral },
{ provide: PIRAL, useValue: piral },
],
});
piral.on('unload-pilet', (ev) => {
if (ev.name === piral.meta.name && typeof reset === 'function') {
// pretty much a cleanup step for Angular.
reset();
}
});
}
locals.active = true;
app
.then((appRef) => {
if (isLazyLoader(component)) {
const lazyComponent = component();
return lazyComponent.then((componentExports) => [appRef, componentExports.default]);
}
return [appRef, component];
})
.then(([appRef, component]) => {
if (locals.active) {
const ref = appRef.bootstrap(component, element);
// Start the routing service.
appRef.injector.get(CoreRoutingService);
update(ref, props);
locals.component = ref;
}
});
},
update(_1, props, _2, locals) {
update(locals.component, props);
},
unmount(element, locals) {
locals.active = false;
locals.component?.destroy();
locals.component = undefined;
element.remove();
},
},
});
}
//# sourceMappingURL=standalone.js.map