PostHog / posthog-js

Send usage data from your web app or site to PostHog, with autocapture.
https://posthog.com/docs/libraries/js
Other
282 stars 120 forks source link

Generate non-minified dist files #741

Open hansottowirtz opened 1 year ago

hansottowirtz commented 1 year ago

I'm trying to debug PostHog, but the minified files make it very hard to do so. Is it possible to generate separate .js and .min.js files? Or even consider not minifying the files at all, because with modern bundlers, generating minified files shouldn't be necessary anymore.

pauldambra commented 1 year ago

Hey @hansottowirtz not everyone includes posthog through a bundler, so we need to keep a minified output (at least for the snippet).

What are you trying to debug?

hansottowirtz commented 1 year ago

Hi @pauldambra, I'm trying to debug an issue that was caused by moving from useEffect to useMemo in the PostHogProvider recently. The error occurs only in SSR. The stack trace looks like this:

web:dev: ReferenceError: window is not defined
web:dev:     at e.value (webpack-internal:///(sc_client)/../../node_modules/posthog-js/dist/es.js:18:69606)
web:dev:     at new e (webpack-internal:///(sc_client)/../../node_modules/posthog-js/dist/es.js:18:68114)
web:dev:     at e.value (webpack-internal:///(sc_client)/../../node_modules/posthog-js/dist/es.js:18:87377)
web:dev:     at hr (webpack-internal:///(sc_client)/../../node_modules/posthog-js/dist/es.js:18:84325)
web:dev:     at sr.init (webpack-internal:///(sc_client)/../../node_modules/posthog-js/dist/es.js:18:105955)
web:dev:     at eval (webpack-internal:///(sc_client)/./src/shared/PosthogProvider.tsx:36:80)
web:dev:     at Object.useMemo (webpack-internal:///(sc_client)/../../node_modules/next/dist/compiled/react-dom-experimental/cjs/react-dom-server.edge.development.js:8297:19)

I have prettified the file now, which helps. But just having a non-minified file would be much nicer. We also use yarn patch or patch-package quite often if we don't have time to wait for a PR to be accepted, but as it uses diffs, it doesn't work so well with minified files.

hansottowirtz commented 1 year ago

I've fixed the issue by not creating PostHog in SSR environments like before. However the point about the minified files still stands 🙂

pauldambra commented 1 year ago

Because the snippet historically uses the minified version we couldn't start creating *.min.js since that would mean we start delivering non-minified js to people.

So, we'd need the reverse I guess a posthog.max.js 🤣

I guess we'd need to check whether there's a standard to follow here that we could offer without breaking the snippet 🤔

jonsherrard commented 11 months ago

@hansottowirtz I have the same issue, but static generation works on my local machine, and then fails on Vercel deployments. Any clues?