UNPKG

ngx-lottie

Version:

<h1 align="center"> <img src="https://raw.githubusercontent.com/ngx-lottie/ngx-lottie/refs/heads/master/docs/assets/logo.png"> </h1>

103 lines (96 loc) 4.01 kB
import * as i0 from '@angular/core'; import { InjectionToken, makeStateKey, inject, TransferState, provideAppInitializer, NgModule, makeEnvironmentProviders } from '@angular/core'; import { join } from 'node:path'; import { transformAnimationFilenameToKey } from 'ngx-lottie'; import { readFile } from 'fs'; /** A simple cache used to store the serialized animation data. */ const cache = new Map(); function readFileWithAnimationData(path) { return cache.has(path) ? Promise.resolve(cache.get(path)) : new Promise((resolve, reject) => { readFile(path, (error, buffer) => { if (error) { reject(error); } else { const data = buffer.toString(); cache.set(path, data); resolve(data); } }); }); } const LOTTIE_SERVER_OPTIONS = new InjectionToken('LottieServerOptions'); function readAndTransferAnimationData(transferState, animations, pathsToAnimations) { const sources = []; for (let i = 0, length = animations.length; i < length; i++) { const path = pathsToAnimations[i]; const source = readFileWithAnimationData(path) .then(animationData => { transferAnimationData(transferState, animations[i], animationData); }) .catch(error => { if (typeof ngDevMode !== 'undefined' && ngDevMode) { console.error(`Failed to read the following file ${path}. Error: `, error); } // This is empty since we don't have to do anything if the file was failed to read. }); sources.push(source); } return sources; } function transferAnimationData(state, animation, animationData) { animation = transformAnimationFilenameToKey(animation); const key = makeStateKey(animation); if (state.hasKey(key)) { return; } state.set(key, JSON.parse(animationData)); } async function appInitializer() { const options = inject(LOTTIE_SERVER_OPTIONS); const transferState = inject(TransferState); const pathsToAnimations = resolveLottiePaths(options); const sources = readAndTransferAnimationData(transferState, options.preloadAnimations.animations, pathsToAnimations); await Promise.all(sources); } function resolveLottiePaths({ preloadAnimations }) { const { folder, animations } = preloadAnimations; const path = join(process.cwd(), folder); return animations.map(animation => join(path, animation)); } class LottieServerModule { static forRoot(options) { return { ngModule: LottieServerModule, providers: [ { provide: LOTTIE_SERVER_OPTIONS, useValue: options, }, provideAppInitializer(appInitializer), ], }; } /** @nocollapse */ static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.0.2", ngImport: i0, type: LottieServerModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule }); } /** @nocollapse */ static { this.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "22.0.2", ngImport: i0, type: LottieServerModule }); } /** @nocollapse */ static { this.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "22.0.2", ngImport: i0, type: LottieServerModule }); } } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.0.2", ngImport: i0, type: LottieServerModule, decorators: [{ type: NgModule }] }); function provideLottieServerOptions(options) { return makeEnvironmentProviders([ { provide: LOTTIE_SERVER_OPTIONS, useValue: options, }, provideAppInitializer(appInitializer), ]); } /** * Generated bundle index. Do not edit. */ export { LottieServerModule, provideLottieServerOptions }; //# sourceMappingURL=ngx-lottie-server.mjs.map