Skip to content
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

fix: improve ES6 bundling #1542

Merged
merged 9 commits into from
Nov 20, 2024
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: 4 additions & 1 deletion .github/workflows/es-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ on:

jobs:
ssr:
name: Cypress
name: Build and check ES5/ES6 support
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
Expand All @@ -21,3 +21,6 @@ jobs:

- name: Run es-check to check if our ie11 bundle is ES5 compatible
run: npx [email protected] es5 dist/array.full.es5.js

- name: Run es-check to check if our main bundle is ES6 compatible
run: npx [email protected] es6 dist/array.full.js
19 changes: 18 additions & 1 deletion rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,24 @@ const plugins = (es5) => [
[
'@babel/preset-env',
{
targets: es5 ? 'defaults, IE 11' : '>0.5%, last 2 versions, Firefox ESR, not dead',
targets: es5
? [
'> 0.5%, last 2 versions, Firefox ESR, not dead',
'chrome > 62',
'firefox > 59',
'ios_saf >= 6.1',
'opera > 50',
'safari > 12',
'IE 11',
]
: [
'> 0.5%, last 2 versions, Firefox ESR, not dead',
'chrome > 62',
'firefox > 59',
'ios_saf >= 10.3',
'opera > 50',
'safari > 12',
],
},
],
],
Expand Down
66 changes: 30 additions & 36 deletions src/__tests__/posthog-core.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,30 @@ import { defaultPostHog } from './helpers/posthog-instance'
import type { PostHogConfig } from '../types'
import { uuidv7 } from '../uuidv7'

const mockReferrerGetter = jest.fn()
const mockURLGetter = jest.fn()
jest.mock('../utils/globals', () => {
const orig = jest.requireActual('../utils/globals')
return {
...orig,
document: {
...orig.document,
createElement: (...args: any[]) => orig.document.createElement(...args),
get referrer() {
return mockReferrerGetter?.()
},
get URL() {
return mockURLGetter?.()
},
},
get location() {
const url = mockURLGetter?.()
return {
href: url,
toString: () => url,
}
},
}
})

describe('posthog core', () => {
const mockURL = jest.fn()
const mockReferrer = jest.fn()

beforeAll(() => {
// Mock getters using Object.defineProperty
Object.defineProperty(document, 'URL', {
get: mockURL,
})
Object.defineProperty(document, 'referrer', {
get: mockReferrer,
})
Object.defineProperty(window, 'location', {
get: () => ({
href: mockURL(),
toString: () => mockURL(),
}),
configurable: true,
})
})

beforeEach(() => {
mockReferrerGetter.mockReturnValue('https://referrer.com')
mockURLGetter.mockReturnValue('https://example.com')
mockReferrer.mockReturnValue('https://referrer.com')
mockURL.mockReturnValue('https://example.com')
console.error = jest.fn()
})

Expand Down Expand Up @@ -111,7 +105,7 @@ describe('posthog core', () => {
it("should send referrer info with the event's properties", () => {
// arrange
const token = uuidv7()
mockReferrerGetter.mockReturnValue('https://referrer.example.com/some/path')
mockReferrer.mockReturnValue('https://referrer.example.com/some/path')
const { posthog, beforeSendMock } = setup({
token,
persistence_name: token,
Expand All @@ -132,14 +126,14 @@ describe('posthog core', () => {
it('should not update the referrer within the same session', () => {
// arrange
const token = uuidv7()
mockReferrerGetter.mockReturnValue('https://referrer1.example.com/some/path')
mockReferrer.mockReturnValue('https://referrer1.example.com/some/path')
const { posthog: posthog1 } = setup({
token,
persistence_name: token,
person_profiles: 'always',
})
posthog1.capture(eventName, eventProperties)
mockReferrerGetter.mockReturnValue('https://referrer2.example.com/some/path')
mockReferrer.mockReturnValue('https://referrer2.example.com/some/path')
const { posthog: posthog2, beforeSendMock } = setup({
token,
persistence_name: token,
Expand All @@ -163,14 +157,14 @@ describe('posthog core', () => {
it('should use the new referrer in a new session', () => {
// arrange
const token = uuidv7()
mockReferrerGetter.mockReturnValue('https://referrer1.example.com/some/path')
mockReferrer.mockReturnValue('https://referrer1.example.com/some/path')
const { posthog: posthog1 } = setup({
token,
persistence_name: token,
person_profiles: 'always',
})
posthog1.capture(eventName, eventProperties)
mockReferrerGetter.mockReturnValue('https://referrer2.example.com/some/path')
mockReferrer.mockReturnValue('https://referrer2.example.com/some/path')
const { posthog: posthog2, beforeSendMock: beforeSendMock2 } = setup({
token,
persistence_name: token,
Expand All @@ -194,7 +188,7 @@ describe('posthog core', () => {
it('should use $direct when there is no referrer', () => {
// arrange
const token = uuidv7()
mockReferrerGetter.mockReturnValue('')
mockReferrer.mockReturnValue('')
const { posthog, beforeSendMock } = setup({
token,
persistence_name: token,
Expand All @@ -217,7 +211,7 @@ describe('posthog core', () => {
it('should not send campaign params as null if there are no non-null ones', () => {
// arrange
const token = uuidv7()
mockURLGetter.mockReturnValue('https://www.example.com/some/path')
mockURL.mockReturnValue('https://www.example.com/some/path')
const { posthog, beforeSendMock } = setup({
token,
persistence_name: token,
Expand All @@ -234,7 +228,7 @@ describe('posthog core', () => {
it('should send present campaign params, and nulls for others', () => {
// arrange
const token = uuidv7()
mockURLGetter.mockReturnValue('https://www.example.com/some/path?utm_source=source')
mockURL.mockReturnValue('https://www.example.com/some/path?utm_source=source')
const { posthog, beforeSendMock } = setup({
token,
persistence_name: token,
Expand Down
Loading