Skip to content

Commit 4d53400

Browse files
letstriValerii Striletsizadoesdev
authored
refactor: add unbuild for sdk (#96)
* refactor: add unbuild for sdk * refactor: update sdk * fix: remove useEffect dependency --------- Co-authored-by: Valerii Strilets <[email protected]> Co-authored-by: Hyteq <[email protected]>
1 parent 1ca7c2f commit 4d53400

File tree

15 files changed

+650
-482
lines changed

15 files changed

+650
-482
lines changed

bun.lock

Lines changed: 467 additions & 394 deletions
Large diffs are not rendered by default.

packages/sdk/build.config.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { readFile, writeFile } from 'node:fs/promises';
2+
import { defineBuildConfig } from 'unbuild';
3+
4+
export default defineBuildConfig({
5+
name: '@databuddy/sdk',
6+
entries: [
7+
'./src/core/index.ts',
8+
'./src/react/index.ts',
9+
'./src/vue/index.ts',
10+
],
11+
externals: ['react', 'react-dom', 'vue'],
12+
declaration: true,
13+
hooks: {
14+
'build:done': async () => {
15+
const file = await readFile('./dist/react/index.mjs', 'utf-8');
16+
await writeFile('./dist/react/index.mjs', `'use client';\n\n${file}`);
17+
},
18+
},
19+
});

packages/sdk/package.json

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,58 @@
11
{
22
"name": "@databuddy/sdk",
3-
"version": "1.4.2",
4-
"main": "dist/index.js",
5-
"types": "dist/index.d.ts",
6-
"module": "dist/index.js",
3+
"version": "2.0.0",
4+
"description": "Official Databuddy Analytics SDK",
5+
"main": "./dist/core/index.mjs",
6+
"types": "./dist/core/index.d.ts",
7+
"license": "MIT",
8+
"private": false,
9+
"scripts": {
10+
"build": "unbuild"
11+
},
712
"devDependencies": {
813
"@types/node": "^20.0.0",
9-
"typescript": "catalog:"
14+
"@vitejs/plugin-react": "^5.0.0",
15+
"react": "18.0.0",
16+
"typescript": "catalog:",
17+
"unbuild": "^3.6.1",
18+
"vue": "3.0.0",
19+
"vue-sfc-transformer": "^0.1.16"
20+
},
21+
"peerDependencies": {
22+
"react": ">=18",
23+
"vue": ">=3"
24+
},
25+
"peerDependenciesMeta": {
26+
"react": {
27+
"optional": true
28+
},
29+
"vue": {
30+
"optional": true
31+
}
1032
},
1133
"exports": {
1234
".": {
13-
"types": "./dist/index.d.ts",
14-
"import": "./dist/index.js",
15-
"require": "./dist/index.js",
16-
"default": "./dist/index.js"
35+
"types": "./dist/core/index.d.ts",
36+
"import": "./dist/core/index.mjs"
37+
},
38+
"./react": {
39+
"types": "./dist/react/index.d.ts",
40+
"import": "./dist/react/index.mjs"
41+
},
42+
"./vue": {
43+
"types": "./dist/vue/index.d.ts",
44+
"import": "./dist/vue/index.mjs"
1745
}
1846
},
19-
"description": "Official Databuddy Analytics SDK",
2047
"files": [
2148
"dist"
2249
],
2350
"keywords": [
2451
"analytics",
2552
"tracking",
2653
"databuddy",
27-
"sdk"
28-
],
29-
"license": "MIT",
30-
"private": false,
31-
"scripts": {
32-
"build": "tsc --project tsconfig.json"
33-
}
54+
"sdk",
55+
"react",
56+
"vue"
57+
]
3458
}

packages/sdk/src/Databuddy.tsx

Lines changed: 0 additions & 52 deletions
This file was deleted.

packages/sdk/src/core/index.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export * from './script';
2+
export * from './tracker';
3+
export * from './types';
4+
5+
import { Databuddy as DatabuddyReact } from '../react/Databuddy';
6+
7+
/**
8+
* @deprecated Use Databuddy from `@databuddy/sdk/react` instead
9+
*/
10+
export const Databuddy = DatabuddyReact;

packages/sdk/src/core/script.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { version } from '../../package.json';
2+
import type { DatabuddyConfig } from './types';
3+
4+
const INJECTED_SCRIPT_ATTRIBUTE = 'data-databuddy-injected';
5+
6+
export function isScriptInjected() {
7+
return !!document.querySelector(`script[${INJECTED_SCRIPT_ATTRIBUTE}]`);
8+
}
9+
10+
export function createScript({
11+
scriptUrl,
12+
sdkVersion,
13+
...props
14+
}: DatabuddyConfig) {
15+
const script = document.createElement('script');
16+
17+
script.src = scriptUrl || 'https://cdn.databuddy.cc/databuddy.js';
18+
script.async = true;
19+
script.crossOrigin = 'anonymous';
20+
script.setAttribute(INJECTED_SCRIPT_ATTRIBUTE, 'true');
21+
script.setAttribute('data-sdk-version', sdkVersion || version);
22+
23+
for (const [key, value] of Object.entries(props)) {
24+
const dataKey = `data-${key.replace(/([A-Z])/g, '-$1').toLowerCase()}`;
25+
26+
script.setAttribute(dataKey, String(value));
27+
}
28+
29+
return script;
30+
}
Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,3 @@ export function trackError(
103103
): Promise<void> {
104104
return track('error', { message, ...properties });
105105
}
106-
107-
export default {
108-
track,
109-
clear,
110-
flush,
111-
trackError,
112-
};

packages/sdk/src/index.ts

Lines changed: 0 additions & 3 deletions
This file was deleted.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { createScript, isScriptInjected } from '../core/script';
2+
import type { DatabuddyConfig } from '../core/types';
3+
4+
/**
5+
* <Databuddy /> component for Next.js/React apps
6+
* Injects the databuddy.js script with all config as data attributes
7+
* Usage: <Databuddy clientId="..." trackScreenViews trackPerformance ... />
8+
*/
9+
export function Databuddy(props: DatabuddyConfig) {
10+
// Only inject script on client-side and if not already injected
11+
if (typeof window !== 'undefined' && !props.disabled && !isScriptInjected()) {
12+
const script = createScript(props);
13+
document.head.appendChild(script);
14+
}
15+
16+
return null;
17+
}

0 commit comments

Comments
 (0)