-
Notifications
You must be signed in to change notification settings - Fork 237
feat(react): add slim entrypoint for tree-shakeable usage without posthog-js runtime dependency #3284
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
Open
dustinbyrne
wants to merge
6
commits into
main
Choose a base branch
from
fix/react-treeshake
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
feat(react): add slim entrypoint for tree-shakeable usage without posthog-js runtime dependency #3284
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
066956c
feat(react): add slim entrypoint for tree-shakeable usage without pos…
dustinbyrne ca41c04
fix(react): use dedicated slim PostHogProvider to avoid undefined-as-…
dustinbyrne 54fdc05
fix(react): improve test assertions and remove unnecessary optional c…
dustinbyrne e620e58
fix(react): memoize slim provider context value and add explanatory c…
dustinbyrne 90a8038
chore(react): prettier
dustinbyrne 9780874
test: verify posthog-js not imported directly by slim bundle
dustinbyrne File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| { | ||
| "name": "@posthog/react-slim", | ||
| "private": true, | ||
| "main": "../dist/umd/slim/index.js", | ||
| "module": "../dist/esm/slim/index.js", | ||
| "types": "../dist/types/slim/index.d.ts" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| { | ||
| "name": "@posthog/react-slim", | ||
| "private": true, | ||
| "main": "../dist/umd/slim/index.js", | ||
| "module": "../dist/esm/slim/index.js", | ||
| "types": "../dist/types/slim/index.d.ts" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| import { readFileSync } from 'fs' | ||
| import { resolve } from 'path' | ||
|
|
||
| /** | ||
| * Guard against the slim bundle accidentally pulling in a runtime posthog-js | ||
| * dependency. The slim entrypoint's whole purpose is to avoid shipping the | ||
| * posthog-js runtime so consumers can bring their own client instance. | ||
| * | ||
| * Today this works because every shared module (hooks, components, helpers) | ||
| * only reaches PostHogContext.ts — never the full PostHogProvider.tsx — and | ||
| * PostHogProvider.tsx's posthog-js import is type-only. But if someone | ||
| * accidentally adds a runtime import, Rollup's `external` config means | ||
| * `posthog-js` would appear as a bare import/require in the output instead of | ||
| * being bundled, making it easy to grep for. | ||
| */ | ||
| describe('slim bundle', () => { | ||
| const distRoot = resolve(__dirname, '../../dist') | ||
|
|
||
| it('ESM slim bundle has no runtime posthog-js imports', () => { | ||
| const content = readFileSync(resolve(distRoot, 'esm/slim/index.js'), 'utf-8') | ||
| const matches = content.match(/['"]posthog-js['"]/g) | ||
| expect(matches).toBeNull() | ||
| }) | ||
|
|
||
| it('UMD slim bundle has no runtime posthog-js imports', () => { | ||
| const content = readFileSync(resolve(distRoot, 'umd/slim/index.js'), 'utf-8') | ||
| const matches = content.match(/['"]posthog-js['"]/g) | ||
| expect(matches).toBeNull() | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,17 @@ | ||
| import posthogJs, { BootstrapConfig } from 'posthog-js' | ||
| import type { PostHog } from 'posthog-js' | ||
| import type { BootstrapConfig } from 'posthog-js' | ||
| import { createContext } from 'react' | ||
| import { getDefaultPostHogInstance } from './posthog-default' | ||
|
|
||
| export type PostHog = typeof posthogJs | ||
| export type { PostHog } | ||
|
|
||
| // The getter defers evaluation so that the full bundle's setDefaultPostHogInstance() | ||
| // call (which runs after module evaluation) has already executed by the time React | ||
| // accesses the default value. In the slim bundle no default is set, so client will | ||
| // be undefined — users must always provide a <PostHogProvider client={…}>. | ||
| export const PostHogContext = createContext<{ client: PostHog; bootstrap?: BootstrapConfig }>({ | ||
| client: posthogJs, | ||
| get client() { | ||
| return getDefaultPostHogInstance() as PostHog | ||
| }, | ||
| bootstrap: undefined, | ||
| }) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| import React, { useMemo } from 'react' | ||
| import type { PostHog } from 'posthog-js' | ||
| import { PostHogContext } from './PostHogContext' | ||
|
|
||
| /** | ||
| * Slim PostHogProvider for use with @posthog/react/slim. | ||
| * | ||
| * Only accepts a pre-initialized `client` instance. Does not support | ||
| * `apiKey`/`options` props since the slim bundle has no posthog-js runtime. | ||
| */ | ||
| export function PostHogProvider({ client, children }: { client: PostHog; children?: React.ReactNode }) { | ||
| const value = useMemo(() => ({ client, bootstrap: client.config?.bootstrap }), [client]) | ||
| return <PostHogContext.Provider value={value}>{children}</PostHogContext.Provider> | ||
| } |
29 changes: 26 additions & 3 deletions
29
packages/react/src/context/__tests__/PostHogContext.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
packages/react/src/context/__tests__/PostHogProviderSlim.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| import * as React from 'react' | ||
| import { render } from '@testing-library/react' | ||
| import { PostHogProvider } from '../PostHogProviderSlim' | ||
| import { PostHogContext } from '../PostHogContext' | ||
| import type { PostHog } from 'posthog-js' | ||
|
|
||
| // Do NOT call setDefaultPostHogInstance — this simulates the slim bundle | ||
| // where no default instance exists. | ||
|
|
||
| let contextClient: PostHog | undefined | ||
|
|
||
| function ClientConsumer() { | ||
| const { client } = React.useContext(PostHogContext) | ||
| contextClient = client | ||
| return <div>consumed</div> | ||
| } | ||
|
|
||
| describe('PostHogProvider (slim)', () => { | ||
| afterEach(() => { | ||
| contextClient = undefined | ||
| }) | ||
|
|
||
| it('renders children', () => { | ||
| const client = { config: {} } as unknown as PostHog | ||
| const { getByText } = render( | ||
| <PostHogProvider client={client}> | ||
| <div>Hello</div> | ||
| </PostHogProvider> | ||
| ) | ||
| expect(getByText('Hello')).toBeTruthy() | ||
| }) | ||
|
|
||
| it('provides the exact client instance via context', () => { | ||
| const client = { config: {} } as unknown as PostHog | ||
| render( | ||
| <PostHogProvider client={client}> | ||
| <ClientConsumer /> | ||
| </PostHogProvider> | ||
| ) | ||
| expect(contextClient).toBe(client) | ||
| }) | ||
|
|
||
| it('provides bootstrap from client config', () => { | ||
| const bootstrap = { featureFlags: { 'test-flag': true } } | ||
| const client = { config: { bootstrap } } as unknown as PostHog | ||
|
|
||
| function BootstrapConsumer() { | ||
| const { bootstrap: ctx } = React.useContext(PostHogContext) | ||
| return <div data-testid="bootstrap">{JSON.stringify(ctx)}</div> | ||
| } | ||
|
|
||
| const { getByTestId } = render( | ||
| <PostHogProvider client={client}> | ||
| <BootstrapConsumer /> | ||
| </PostHogProvider> | ||
| ) | ||
| expect(JSON.parse(getByTestId('bootstrap').textContent!)).toEqual(bootstrap) | ||
| }) | ||
| }) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.