Skip to content
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
5 changes: 5 additions & 0 deletions .changeset/busy-bobcats-greet.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@posthog/nextjs-config': minor
---

use webpack plugin
5 changes: 5 additions & 0 deletions .changeset/legal-snails-wonder.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@posthog/webpack-plugin': major
---

initial release
5 changes: 5 additions & 0 deletions .changeset/tough-badgers-jam.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@posthog/core': patch
---

export logger creation
1 change: 1 addition & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ module.exports = {
'packages/react-native/**',
'packages/node/**',
'packages/web/**',
'packages/webpack-plugin/**',
'packages/rollup-plugin/**',
'examples/**',
'playground/**',
Expand Down
1 change: 1 addition & 0 deletions .github/ISSUE_TEMPLATE/bug_report.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ _Please describe._
- [ ] @posthog/nextjs-config
- [ ] @posthog/nuxt
- [ ] @posthog/rollup-plugin
- [ ] @posthog/webpack-plugin

## Additional context

Expand Down
1 change: 1 addition & 0 deletions .github/ISSUE_TEMPLATE/feature_request.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ _Please describe._
- [ ] @posthog/nextjs-config
- [ ] @posthog/nuxt
- [ ] @posthog/rollup-plugin
- [ ] @posthog/webpack-plugin

## Additional context

Expand Down
1 change: 1 addition & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ jobs:
- name: "@posthog/nextjs-config"
- name: "@posthog/nuxt"
- name: "@posthog/rollup-plugin"
- name: "@posthog/webpack-plugin"

steps:
- name: Checkout the repository
Expand Down
2 changes: 2 additions & 0 deletions examples/example-nextjs/next.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ export default withPostHogConfig(nextConfig, {
host: process.env.NEXT_PUBLIC_POSTHOG_API_HOST!,
cliBinaryPath: process.env.POSTHOG_CLI_PATH, // Optional
logLevel: 'debug',
cliBinaryPath: process.env.POSTHOG_CLI_PATH,
sourcemaps: {
project: 'example-nextjs',
version: packageJson.version,
deleteAfterUpload: true,
},
})
469 changes: 245 additions & 224 deletions examples/example-nextjs/pnpm-lock.yaml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/core/src/posthog-core-stateless.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { SimpleEventEmitter } from './eventemitter'
import { getFeatureFlagValue, normalizeFlagsResponse } from './featureFlagUtils'
import { gzipCompress, isGzipSupported } from './gzip'
import { createLogger } from './logger'
import {
PostHogFlagsResponse,
PostHogCoreOptions,
Expand Down Expand Up @@ -31,6 +30,7 @@ import {
RetriableOptions,
safeSetTimeout,
STRING_FORMAT,
createLogger,
} from './utils'
import { uuidv7 } from './vendor/uuidv7'

Expand Down
1 change: 1 addition & 0 deletions packages/core/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export * from './number-utils'
export * from './string-utils'
export * from './type-utils'
export * from './promise-queue'
export * from './logger'

export const STRING_FORMAT = 'utf8'

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Logger } from './types'
import { Logger } from '../types'

// We want to make sure to get the original console methods as soon as possible
type ConsoleLike = {
Expand Down Expand Up @@ -53,6 +53,8 @@ export const _createLogger = (
return logger
}

export function createLogger(prefix: string, maybeCall: (fn: () => void) => void) {
const passThrough = (fn: () => void) => fn()

export function createLogger(prefix: string, maybeCall: (fn: () => void) => void = passThrough) {
return _createLogger(prefix, maybeCall, createConsole())
}
3 changes: 2 additions & 1 deletion packages/nextjs-config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@
"dependencies": {
"@posthog/cli": "catalog:",
"semver": "^7.7.2",
"@posthog/core": "workspace:*"
"@posthog/core": "workspace:*",
"@posthog/webpack-plugin": "workspace:*"
},
"keywords": [
"posthog",
Expand Down
77 changes: 9 additions & 68 deletions packages/nextjs-config/src/config.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,14 @@
import type { NextConfig } from 'next'
import { SourcemapWebpackPlugin } from './webpack-plugin'
import { PosthogWebpackPlugin, PluginConfig, resolveConfig, ResolvedPluginConfig } from '@posthog/webpack-plugin'
import { hasCompilerHook, isTurbopackEnabled, processSourceMaps } from './utils'
import { resolveBinaryPath, LogLevel } from '@posthog/core/process'

type NextFuncConfig = (phase: string, { defaultConfig }: { defaultConfig: NextConfig }) => NextConfig
type NextAsyncConfig = (phase: string, { defaultConfig }: { defaultConfig: NextConfig }) => Promise<NextConfig>
type UserProvidedConfig = NextConfig | NextFuncConfig | NextAsyncConfig

export type PostHogNextConfig = {
cliBinaryPath?: string
personalApiKey: string
envId: string
host?: string
logLevel?: LogLevel
sourcemaps?: {
enabled?: boolean
project?: string
version?: string
deleteAfterUpload?: boolean
}
}

export type PostHogNextConfigComplete = {
cliBinaryPath: string
personalApiKey: string
envId: string
host: string
logLevel: LogLevel
sourcemaps: {
enabled: boolean
project?: string
version?: string
deleteAfterUpload: boolean
}
}

export function withPostHogConfig(userNextConfig: UserProvidedConfig, posthogConfig: PostHogNextConfig): NextConfig {
const posthogNextConfigComplete = resolvePostHogConfig(posthogConfig)
const sourceMapEnabled = posthogNextConfigComplete.sourcemaps.enabled
export function withPostHogConfig(userNextConfig: UserProvidedConfig, posthogConfig: PluginConfig): NextConfig {
const resolvedConfig = resolveConfig(posthogConfig)
const sourceMapEnabled = resolvedConfig.sourcemaps.enabled
const isCompilerHookSupported = hasCompilerHook()
const turbopackEnabled = isTurbopackEnabled()
if (turbopackEnabled && !isCompilerHookSupported) {
Expand All @@ -54,8 +25,8 @@ export function withPostHogConfig(userNextConfig: UserProvidedConfig, posthogCon
...userConfig,
distDir,
productionBrowserSourceMaps: sourceMapEnabled,
webpack: withWebpackConfig(userWebPackConfig, posthogNextConfigComplete, distDir),
compiler: withCompilerConfig(userCompilerConfig, posthogNextConfigComplete),
webpack: withWebpackConfig(userWebPackConfig, resolvedConfig),
compiler: withCompilerConfig(userCompilerConfig, resolvedConfig),
}
}
}
Expand All @@ -79,46 +50,16 @@ function resolveUserConfig(
}
}

function resolvePostHogConfig(posthogProvidedConfig: PostHogNextConfig): PostHogNextConfigComplete {
const { personalApiKey, envId, host, logLevel, sourcemaps = {} } = posthogProvidedConfig
const cliBinaryPath =
posthogProvidedConfig.cliBinaryPath ??
resolveBinaryPath('posthog-cli', { path: process.env.PATH ?? '', cwd: __dirname })

return {
cliBinaryPath,
personalApiKey,
envId,
host: host ?? 'https://us.i.posthog.com',
logLevel: logLevel ?? 'info',
sourcemaps: {
enabled: sourcemaps.enabled ?? process.env.NODE_ENV == 'production',
project: sourcemaps.project,
version: sourcemaps.version,
deleteAfterUpload: sourcemaps.deleteAfterUpload ?? true,
},
}
}

function withWebpackConfig(
userWebpackConfig: NextConfig['webpack'],
posthogConfig: PostHogNextConfigComplete,
distDir: string | undefined
) {
function withWebpackConfig(userWebpackConfig: NextConfig['webpack'], posthogConfig: ResolvedPluginConfig) {
const defaultWebpackConfig = userWebpackConfig || ((config: any) => config)
const sourceMapEnabled = posthogConfig.sourcemaps.enabled
return (config: any, options: any) => {
const turbopackEnabled = isTurbopackEnabled()
const webpackConfig = defaultWebpackConfig(config, options)
if (sourceMapEnabled) {
if (options.isServer) {
webpackConfig.devtool = 'source-map'
}
if (!turbopackEnabled) {
webpackConfig.plugins = webpackConfig.plugins || []
webpackConfig.plugins.push(
new SourcemapWebpackPlugin(posthogConfig, options.isServer, options.nextRuntime, distDir)
)
webpackConfig.plugins.push(new PosthogWebpackPlugin(posthogConfig))
}
}
return webpackConfig
Expand All @@ -127,7 +68,7 @@ function withWebpackConfig(

function withCompilerConfig(
userCompilerConfig: NextConfig['compiler'],
posthogConfig: PostHogNextConfigComplete
posthogConfig: ResolvedPluginConfig
): NextConfig['compiler'] {
const sourceMapEnabled = posthogConfig.sourcemaps.enabled
const turbopackEnabled = isTurbopackEnabled()
Expand Down
5 changes: 3 additions & 2 deletions packages/nextjs-config/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import nextPackage from 'next/package.json' with { type: 'json' }
import semver from 'semver'
import { PostHogNextConfigComplete } from './config'

import { spawnLocal } from '@posthog/core/process'
import { ResolvedPluginConfig } from '@posthog/webpack-plugin'

export function getNextJsVersion(): string {
return nextPackage.version
Expand All @@ -12,7 +13,7 @@ export function hasCompilerHook(): boolean {
return semver.gte(nextJsVersion, '15.4.1')
}

export async function processSourceMaps(posthogOptions: PostHogNextConfigComplete, directory: string) {
export async function processSourceMaps(posthogOptions: ResolvedPluginConfig, directory: string) {
const cliOptions = []
cliOptions.push('sourcemap', 'process')
cliOptions.push('--directory', directory)
Expand Down
63 changes: 0 additions & 63 deletions packages/nextjs-config/src/webpack-plugin.ts

This file was deleted.

48 changes: 48 additions & 0 deletions packages/webpack-plugin/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
"name": "@posthog/webpack-plugin",
"version": "0.0.0",
"description": "Webpack plugin for Posthog 🦔",
"repository": {
"type": "git",
"url": "https://github.com/PostHog/posthog-js.git",
"directory": "packages/webpack-plugin"
},
"author": {
"name": "PostHog",
"email": "[email protected]",
"url": "https://posthog.com"
},
"main": "./dist/index.js",
"module": "./dist/index.mjs",
"types": "./dist/index.d.ts",
"dependencies": {
"@posthog/cli": "catalog:",
"@posthog/core": "workspace:*"
},
"files": [
"dist",
"src"
],
"devDependencies": {
"@posthog-tooling/tsconfig-base": "workspace:*",
"@rslib/core": "catalog:",
"jest": "catalog:",
"webpack": "^5.102.1"
},
"scripts": {
"clean": "rimraf dist",
"lint": "eslint src",
"lint:fix": "eslint src --fix",
"build": "rslib build",
"test:unit": "jest --passWithNoTests",
"dev": "rslib build --watch",
"package": "pnpm pack --out $PACKAGE_DEST/%s.tgz"
},
"exports": {
".": {
"require": "./dist/index.js",
"import": "./dist/index.mjs",
"types": "./dist/index.d.ts"
}
}
}
8 changes: 8 additions & 0 deletions packages/webpack-plugin/rslib.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { defineConfig } from '@rslib/core'

export default defineConfig({
dts: true,
bundle: false,
syntax: 'es6',
lib: [{ format: 'esm' }, { format: 'cjs' }],
})
Loading
Loading