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
6 changes: 6 additions & 0 deletions .changeset/core-surveys-subpath-old-metro.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@posthog/core': patch
'posthog-react-native': patch
---

Fix `Unable to resolve module @posthog/core/surveys` when bundling posthog-react-native on React Native 0.71–0.78 without Metro package exports. posthog-react-native 4.47.0 and later also pick up the fix by updating `@posthog/core`.
1 change: 1 addition & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
},
"files": [
"dist",
"surveys",
"src",
"!src/__tests__"
],
Expand Down
22 changes: 22 additions & 0 deletions packages/core/src/__tests__/subpath-stubs.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { readFileSync } from 'node:fs'
import { join } from 'node:path'

const packageRoot = join(__dirname, '..', '..')
const readJson = (path: string): any => JSON.parse(readFileSync(join(packageRoot, path), 'utf8'))

// Metro without package exports (React Native < 0.79 by default) resolves `@posthog/core/surveys`
// as a folder, so the stub must point at the same files as the `./surveys` export.
describe('subpath stubs', () => {
it('surveys/package.json matches the ./surveys export', () => {
const exported = readJson('package.json').exports['./surveys']
const stub = readJson('surveys/package.json')

expect(join('surveys', stub.main)).toBe(join(exported.require))
expect(join('surveys', stub.module)).toBe(join(exported.import))
expect(join('surveys', stub.types)).toBe(join(exported.types))
})

it('ships the surveys stub', () => {
expect(readJson('package.json').files).toContain('surveys')
})
})
6 changes: 6 additions & 0 deletions packages/core/surveys/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"private": true,
"main": "../dist/surveys/index.js",
"module": "../dist/surveys/index.mjs",
"types": "../dist/surveys/index.d.ts"
}
28 changes: 28 additions & 0 deletions packages/react-native/test/core-subpath-stubs.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { existsSync, readdirSync, readFileSync, statSync } from 'node:fs'
import { join } from 'node:path'

const srcDir = join(__dirname, '..', 'src')
const coreDir = join(__dirname, '..', '..', 'core')

const sourceFiles = (dir: string): string[] =>
readdirSync(dir).flatMap((name) => {
const path = join(dir, name)
return statSync(path).isDirectory() ? sourceFiles(path) : /\.(ts|tsx)$/.test(name) ? [path] : []
})

// Metro without package exports (React Native < 0.79 by default) can only resolve
// `@posthog/core/<subpath>` through a real `<subpath>/package.json` in @posthog/core.
describe('@posthog/core subpath imports', () => {
it('each has a resolvable stub in @posthog/core', () => {
const subpaths = new Set(
sourceFiles(srcDir).flatMap((file) =>
[...readFileSync(file, 'utf8').matchAll(/['"]@posthog\/core\/([\w/-]+)['"]/g)].map((match) => match[1])
)
)

expect(subpaths.size).toBeGreaterThan(0)
for (const subpath of subpaths) {
expect(existsSync(join(coreDir, subpath, 'package.json')), `@posthog/core/${subpath}`).toBe(true)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Stub existence does not prove resolution A future core subpath can have a package.json but still point to missing files or be absent from the published package. This test would pass while older Metro versions fail to resolve the import. Check resolution against the built, packed core package instead of checking file existence alone.

Prompt To Fix With AI
This is a comment left during a code review.
Path: packages/react-native/test/core-subpath-stubs.spec.ts
Line: 25

Comment:
**Stub existence does not prove resolution** A future core subpath can have a `package.json` but still point to missing files or be absent from the published package. This test would pass while older Metro versions fail to resolve the import. Check resolution against the built, packed core package instead of checking file existence alone.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

}
})
})
Loading