Skip to content

Commit

Permalink
Merge pull request #172 from arturovt/refactor/tree-shake
Browse files Browse the repository at this point in the history
refactor: tree-shake injection token names
  • Loading branch information
NetanelBasal authored Jan 1, 2025
2 parents 2c379a4 + ff51f57 commit 5a61e98
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
14 changes: 10 additions & 4 deletions projects/ngneat/helipopper/config/src/inject-tippy.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import { inject } from '@angular/core';

import { TIPPY_REF, type TippyInstance } from './tippy.types';
import { TIPPY_REF, TippyErrorCode, type TippyInstance } from './tippy.types';

export function injectTippyRef(): TippyInstance {
const instance = inject(TIPPY_REF, { optional: true });

if (instance) {
return instance;
if (!instance) {
if (typeof ngDevMode !== 'undefined' && ngDevMode) {
throw new Error(
'tp is not provided in the current context or on one of its ancestors'
);
} else {
throw new Error(`[tp]: ${TippyErrorCode.TippyNotProvided}`);
}
}

throw new Error('tp is not provided in the current context or on one of its ancestors');
return instance;
}
24 changes: 21 additions & 3 deletions projects/ngneat/helipopper/config/src/tippy.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import type { Instance, Props } from 'tippy.js';
import { ElementRef, InjectionToken } from '@angular/core';
import type { ResolveViewRef, ViewOptions } from '@ngneat/overview';

export const enum TippyErrorCode {
TippyNotProvided = 1,
}

export interface CreateOptions extends Partial<TippyProps>, ViewOptions {
variation: string;
preserveView: boolean;
Expand Down Expand Up @@ -35,8 +39,22 @@ export type TippyConfig = Partial<ExtendedTippyProps>;

export type TippyLoader = () => typeof tippy | Promise<{ default: typeof tippy }>;

export const TIPPY_LOADER = new InjectionToken<TippyLoader>('TIPPY_LOADER');
export const TIPPY_LOADER = new InjectionToken<TippyLoader>(
typeof ngDevMode !== 'undefined' && ngDevMode ? 'TIPPY_LOADER' : ''
);

export const TIPPY_REF = /* @__PURE__ */ new InjectionToken<TippyInstance>('TIPPY_REF');
export const TIPPY_REF = /* @__PURE__ */ new InjectionToken<TippyInstance>(
typeof ngDevMode !== 'undefined' && ngDevMode ? 'TIPPY_REF' : ''
);

export const TIPPY_CONFIG = new InjectionToken<TippyConfig>('Tippy config');
export const TIPPY_CONFIG = new InjectionToken<TippyConfig>(
typeof ngDevMode !== 'undefined' && ngDevMode ? 'TIPPY_CONFIG' : ''
);

/** @internal */
declare global {
// Indicates whether the application is operating in development mode.
// `ngDevMode` is a global flag set by Angular CLI.
// https://github.com/angular/angular-cli/blob/9b883fe28862c96720c7899b431174e9b47ad7e4/packages/angular/build/src/tools/esbuild/application-code-bundle.ts#L604
const ngDevMode: boolean;
}

0 comments on commit 5a61e98

Please sign in to comment.