Skip to content

Commit

Permalink
Standalone and independently versioned browser-destinations (#1276)
Browse files Browse the repository at this point in the history
This patch makes each browser destinations accessible through NPM by making it standalone and independently versioned. This will let customers install the destinations from NPM and have it packaged into their main app bundle through webpack or other bundlers, thus eliminating the reliance on CDN and a lot of concurrent network requests.
  • Loading branch information
zikaari authored Jul 11, 2023
1 parent c82a96b commit 0c2a21e
Show file tree
Hide file tree
Showing 480 changed files with 1,923 additions and 841 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ jobs:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

- name: Build
run: NODE_ENV=production yarn lerna run build --scope=@segment/browser-destinations --include-dependencies --stream
run: NODE_ENV=production yarn build:browser-destinations && yarn browser build-web

- name: Run Saucelabs Tests
working-directory: packages/browser-destinations-integration-tests
Expand Down
2 changes: 1 addition & 1 deletion docs/create.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ With this minimal configuration, the destination can connect to the Segment App'

```js
import type { Settings } from './generated-types'
import type { BrowserDestinationDefinition } from '../../lib/browser-destinations'
import type { BrowserDestinationDefinition } from '@segment/browser-destination-runtime/types'
import { browserDestination } from '../../runtime/shim'

// Declare global to access your client
Expand Down
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"packages": ["packages/*"],
"packages": ["packages/*", "packages/browser-destinations/destinations/*"],
"npmClient": "yarn",
"version": "independent",
"useWorkspaces": true,
Expand Down
10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"license": "MIT",
"workspaces": {
"packages": [
"packages/*"
"packages/*",
"packages/browser-destinations/destinations/*"
]
},
"engines": {
Expand All @@ -17,10 +18,11 @@
"cli-internal": "yarn workspace @segment/actions-cli-internal",
"core": "yarn workspace @segment/actions-core",
"bootstrap": "lerna bootstrap",
"build": "./bin/run generate:types && lerna run build --stream --ignore @segment/actions-cli-internal",
"build": "./bin/run generate:types && lerna run build --concurrency 1 --stream --ignore @segment/actions-cli-internal && yarn browser build-web",
"build:browser-destinations": "yarn lerna run build --concurrency 1 --scope=@segment/destinations-manifest --include-dependencies --stream && yarn browser build-web",
"types": "./bin/run generate:types",
"validate": "./bin/run validate",
"lint": "eslint '**/*.ts' --cache",
"lint": "ls -d ./packages/* | xargs -I {} eslint '{}/**/*.ts' --cache",
"subscriptions": "yarn workspace @segment/destination-subscriptions",
"test": "lerna run test --stream",
"test-partners": "lerna run test --stream --ignore @segment/actions-core --ignore @segment/actions-cli --ignore @segment/ajv-human-errors",
Expand Down Expand Up @@ -68,7 +70,7 @@
"timers-browserify": "^2.0.12",
"ts-jest": "^27.0.0",
"ts-node": "^9.1.1",
"typescript": "^4.1.3",
"typescript": "4.3.5",
"ws": "^8.5.0"
},
"resolutions": {
Expand Down
66 changes: 66 additions & 0 deletions packages/browser-destination-runtime/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
{
"name": "@segment/browser-destination-runtime",
"version": "0.0.0",
"scripts": {
"build": "yarn build:esm && yarn build:cjs",
"build:esm": "tsc --outDir ./dist/esm",
"build:cjs": "tsc --module commonjs --outDir ./dist/cjs"
},
"exports": {
".": {
"require": "./dist/cjs/index.js",
"default": "./dist/esm/index.js"
},
"./load-script": {
"require": "./dist/cjs/load-script.js",
"default": "./dist/esm/load-script.js"
},
"./plugin": {
"require": "./dist/cjs/plugin.js",
"default": "./dist/esm/plugin.js"
},
"./resolve-when": {
"require": "./dist/cjs/resolve-when.js",
"default": "./dist/esm/resolve-when.js"
},
"./shim": {
"require": "./dist/cjs/shim.js",
"default": "./dist/esm/shim.js"
},
"./types": {
"require": "./dist/cjs/types.js",
"default": "./dist/esm/types.js"
}
},
"typesVersions": {
"*": {
"*": [
"dist/esm/index.d.ts"
],
"load-script": [
"dist/esm/load-script.d.ts"
],
"plugin": [
"dist/esm/plugin.d.ts"
],
"resolve-when": [
"dist/esm/resolve-when.d.ts"
],
"shim": [
"dist/esm/shim.d.ts"
],
"types": [
"dist/esm/types.d.ts"
]
}
},
"dependencies": {
"@segment/actions-core": "^3.70.0"
},
"devDependencies": {
"@segment/analytics-next": "*"
},
"peerDependencies": {
"@segment/analytics-next": "*"
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BrowserDestinationDefinition } from 'src/lib/browser-destinations'
import { BrowserDestinationDefinition } from '../types'
import { generatePlugins } from '../plugin'

describe('generatePlugins', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { Analytics, Context, Plugin } from '@segment/analytics-next'
import type { JSONObject } from '@segment/actions-core'
import { transform } from '@segment/actions-core/mapping-kit'
import { parseFql, validate } from '@segment/destination-subscriptions'
import { ActionInput, BrowserDestinationDefinition, Subscription } from '../lib/browser-destinations'
import { ActionInput, BrowserDestinationDefinition, Subscription } from './types'
import { loadScript } from './load-script'
import { resolveWhen } from './resolve-when'

Expand Down
16 changes: 16 additions & 0 deletions packages/browser-destination-runtime/src/shim.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import type { BrowserDestinationDefinition, PluginFactory, Subscription } from './types'

export function browserDestination<S, C>(definition: BrowserDestinationDefinition<S, C>): PluginFactory {
const factory = (async (settings: S & { subscriptions?: Subscription[] }) => {
const plugin = await import(
/* webpackChunkName: "actions-plugin" */
/* webpackMode: "lazy-once" */
'./plugin'
)
return plugin.generatePlugins(definition, settings, settings.subscriptions || [])
}) as unknown as PluginFactory

factory.pluginName = definition.name

return factory
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import type {
BaseActionDefinition,
ExecuteInput,
JSONLikeObject,
GlobalSetting
GlobalSetting,
JSONValue
} from '@segment/actions-core'

export type ActionInput<Settings, Payload> = ExecuteInput<Settings, Payload> & {
Expand Down Expand Up @@ -61,3 +62,8 @@ export interface Subscription {
subscribe: string
mapping: JSONLikeObject
}

export interface PluginFactory {
(settings: JSONValue): Plugin | Plugin[] | Promise<Plugin | Plugin[]>
pluginName: string
}
9 changes: 9 additions & 0 deletions packages/browser-destination-runtime/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"module": "esnext",
"removeComments": false,
"baseUrl": "."
},
"exclude": ["dist"]
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import page from '../pageobjects/page'
import { expect } from 'expect'
import { listDestinations } from '../server/utils'

// '688' is actions-core, we don't want to test it as a destination here
// actions-plugins is just a shared chunk, we don't want to test it as a destination here
const allDestinations = listDestinations()
.map((el) => el.dirPath)
.filter((el) => el !== '688')
.filter((el) => el !== 'actions-plugin')

describe('Bundles are capable of being parsed and loaded without errors', () => {
for (const destination of allDestinations) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "@segment/analytics-browser-actions-adobe-target",
"version": "0.0.0",
"license": "MIT",
"main": "./dist/cjs",
"module": "./dist/esm",
"scripts": {
"build": "yarn build:esm && yarn build:cjs",
"build-es": "tsc",
"build:cjs": "tsc --module commonjs --outDir ./dist/cjs",
"build:esm": "tsc --outDir ./dist/esm"
},
"typings": "./dist/esm",
"dependencies": {
"@segment/browser-destination-runtime": "^0.0.0"
},
"peerDependencies": {
"@segment/analytics-next": "*"
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Analytics, Context } from '@segment/analytics-next'
import adobeTarget, { destination } from '../index'
import { Subscription } from '../../../lib/browser-destinations'
import { Subscription } from '@segment/browser-destination-runtime/types'

describe('Adobe Target Web', () => {
test('can load ATJS', async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Settings } from './generated-types'
import type { BrowserDestinationDefinition } from '../../lib/browser-destinations'
import { browserDestination } from '../../runtime/shim'
import type { BrowserDestinationDefinition } from '@segment/browser-destination-runtime/types'
import { browserDestination } from '@segment/browser-destination-runtime/shim'
import { Adobe } from './types'
import { initScript } from './init-script'

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Analytics, Context } from '@segment/analytics-next'
import adobeTarget, { destination } from '../../index'
import { Subscription } from '../../../../lib/browser-destinations'
import { Subscription } from '@segment/browser-destination-runtime/types'

describe('Adobe Target Web', () => {
describe('#track', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { BrowserActionDefinition } from '../../../lib/browser-destinations'
import type { BrowserActionDefinition } from '@segment/browser-destination-runtime/types'
import type { Settings } from '../generated-types'
import type { Payload } from './generated-types'
import { Adobe } from '../types'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Analytics, Context } from '@segment/analytics-next'
import adobeTarget, { destination } from '../../index'
import { Subscription } from '../../../../lib/browser-destinations'
import { Subscription } from '@segment/browser-destination-runtime/types'

describe('Adobe Target Web', () => {
describe('#page', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { BrowserActionDefinition } from '../../../lib/browser-destinations'
import type { BrowserActionDefinition } from '@segment/browser-destination-runtime/types'
import type { Settings } from '../generated-types'
import type { Payload } from './generated-types'
import { Adobe } from '../types'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Analytics, Context } from '@segment/analytics-next'
import adobeTarget, { destination } from '../../index'
import { Subscription } from '../../../../lib/browser-destinations'
import { Subscription } from '@segment/browser-destination-runtime/types'

describe('Adobe Target Web', () => {
describe('#identify', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { BrowserActionDefinition } from '../../../lib/browser-destinations'
import type { BrowserActionDefinition } from '@segment/browser-destination-runtime/types'
import type { Settings } from '../generated-types'
import { Adobe } from '../types'
import type { Payload } from './generated-types'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "../../tsconfig.build.json",
"compilerOptions": {
"rootDir": "./src",
"baseUrl": "."
},
"include": ["src"],
"exclude": ["dist", "**/__tests__"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "@segment/analytics-browser-actions-amplitude-plugins",
"version": "0.0.0",
"license": "MIT",
"main": "./dist/cjs",
"module": "./dist/esm",
"scripts": {
"build": "yarn build:esm && yarn build:cjs",
"build:cjs": "tsc --module commonjs --outDir ./dist/cjs",
"build:esm": "tsc --outDir ./dist/esm"
},
"typings": "./dist/esm",
"dependencies": {
"@segment/browser-destination-runtime": "^0.0.0"
},
"peerDependencies": {
"@segment/analytics-next": "*"
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Settings } from './generated-types'
import type { BrowserDestinationDefinition } from '../../lib/browser-destinations'
import { browserDestination } from '../../runtime/shim'
import type { BrowserDestinationDefinition } from '@segment/browser-destination-runtime/types'
import { browserDestination } from '@segment/browser-destination-runtime/shim'
import sessionId from './sessionId'

export const destination: BrowserDestinationDefinition<Settings, {}> = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Analytics, Context, Plugin } from '@segment/analytics-next'
import browserPluginsDestination from '../..'
import { Subscription } from '../../../../lib/browser-destinations'
import { Subscription } from '@segment/browser-destination-runtime/types'
import jar from 'js-cookie'

expect.extend({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable @typescript-eslint/no-unsafe-call */
import { UniversalStorage } from '@segment/analytics-next'
import type { BrowserActionDefinition } from '../../../lib/browser-destinations'
import type { BrowserActionDefinition } from '@segment/browser-destination-runtime/types'
import type { Settings } from '../generated-types'
import type { Payload } from './generated-types'

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "../../tsconfig.build.json",
"compilerOptions": {
"rootDir": "./src",
"baseUrl": "."
},
"include": ["src"],
"exclude": ["dist", "**/__tests__"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "@segment/analytics-browser-actions-braze-cloud-plugins",
"version": "0.0.0",
"license": "MIT",
"main": "./dist/cjs",
"module": "./dist/esm",
"scripts": {
"build": "yarn build:esm && yarn build:cjs",
"build:cjs": "tsc --module commonjs --outDir ./dist/cjs",
"build:esm": "tsc --outDir ./dist/esm"
},
"typings": "./dist/esm",
"dependencies": {
"@segment/analytics-browser-actions-braze": "^0.0.0",
"@segment/browser-destination-runtime": "^0.0.0"
},
"peerDependencies": {
"@segment/analytics-next": "*"
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Settings } from './generated-types'
import type { BrowserDestinationDefinition } from '../../lib/browser-destinations'
import { browserDestination } from '../../runtime/shim'
import debouncePlugin from '../braze/debounce'
import type { BrowserDestinationDefinition } from '@segment/browser-destination-runtime/types'
import { browserDestination } from '@segment/browser-destination-runtime/shim'
import debouncePlugin from '@segment/analytics-browser-actions-braze/debounce'

export const destination: BrowserDestinationDefinition<Settings, unknown> = {
name: 'Braze Cloud Mode (Actions)',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "../../tsconfig.build.json",
"compilerOptions": {
"rootDir": "./src",
"baseUrl": "."
},
"include": ["src"],
"exclude": ["dist", "**/__tests__"]
}
Loading

0 comments on commit 0c2a21e

Please sign in to comment.