Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: tree-shake injection token names #172

Merged
merged 1 commit into from
Jan 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
Loading