feat(browser): Tree shake additional extensions#3165
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
Size Change: -66.2 kB (-0.99%) Total Size: 6.64 MB
ℹ️ View Unchanged
|
e5b6d39 to
cf4e2c4
Compare
cf4e2c4 to
b09724d
Compare
918b72e to
570817b
Compare
…, logs, experiments Move 6 extension classes from hard-coded construction to conditional construction via __extensionClasses. Each class now implements the Extension interface and is only instantiated when included in the bundle configuration. Survey enums are converted from TypeScript enums to const objects for better tree-shaking. Extension bundles are added for each new tree-shakable class.
These were previously available _before_ `init()` had been called. Though edge-case-y, this ensures full backwards compatibility.
adcfed2 to
13f9506
Compare
This is a backwards compatibility fix. In the full bundle, consumers will resolve TreeShakeable<T> to T, i.e., the type will always be present. When importing the slim bundle, the type is augmented such that TreeShakeable<T> becomes T | undefined, indicating that a tree shakeable type may not be present at runtime (i.e., it's not been included in the bundle).
Adds a small rollup plugin to rename relative module references to 'posthog-js'. This is correct due to the fact that we use rollup-plugin-dts to combine all our type files by entrypoint, but it does not support relative modules.
4320966 to
7cec965
Compare
| /** | ||
| * Augmented by the slim bundle entry point to mark tree-shakeable extensions | ||
| * as optional. Empty by default (full bundle), meaning extensions are guaranteed. | ||
| */ | ||
| export interface TreeShakeableConfig {} | ||
|
|
||
| /** | ||
| * For the full bundle (default), resolves to T (extensions guaranteed present). | ||
| * For the slim bundle (augmented with { optional: true }), resolves to T | undefined. | ||
| */ | ||
| export type TreeShakeable<T> = 'optional' extends keyof TreeShakeableConfig ? T | undefined : T |
There was a problem hiding this comment.
I noticed our Vercel build was failing because it expected posthog.surveys to always be defined. In the full bundle it always is defined, but the type checker still requires a null check. Having thought about it some more, it doesn't seem reasonable for us to be making a breaking change in the type checking.
I came up with this solution. This allows us to mark properties as always defined in a full bundle, and optionally defined in a slim bundle. It defaults to being defined, so there's no risk of breaking backwards compatibility with types.
|
@PostHog/team-surveys I need your approval to merge this. |
lucasheriques
left a comment
There was a problem hiding this comment.
looks good on the surveys side 🙏
Problem
Six classes are always constructed in the PostHog constructor regardless of whether they're needed: Surveys, Toolbar, PostHogExceptions, Conversations, Logs, and WebExperiments. This means they can't be tree-shaken out of slim/custom bundles.
This is PR 2/3
Changes
Makes all six classes conditionally constructed via
__extensionClasses, matching the pattern already used for autocapture, session recording, heatmaps, etc.import typein posthog-core.ts. Classes are only referenced through the__extensionClassesconfig.surveys?,toolbar?,exceptions?,conversations?,logs?,experiments?— all use optional chaining at call sites (~15 locations).getSurveys,getActiveMatchingSurveys,canRenderSurvey, etc.) call the error callback when the surveys module isn't loaded.captureExceptionreturns early.loadToolbarreturns false._onRemoteConfigfully unified: Removes the remaining hardcoded dispatch calls — everything now goes throughthis.extensions.forEach(ext => ext.onRemoteConfig?.(config)).implement Extensionwith appropriateinitialize()methods.enumdeclarations inposthog-surveys-types.tsconverted toas constobjects with type aliases. Enums compile to IIFEs that bundlers can't tree-shake; const objects are plain property access.SurveysExtensions,ToolbarExtensions,ExperimentsExtensions,ConversationsExtensions,LogsExtensions, andErrorTrackingExtensionsnow includesexceptions.Bundle size
Release info Sub-libraries affected
Libraries affected
Checklist
If releasing new changes
pnpm changesetto generate a changeset file