-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.js
More file actions
54 lines (47 loc) · 1.5 KB
/
Copy pathbuild.js
File metadata and controls
54 lines (47 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import esbuild from 'esbuild';
import { readFileSync } from 'node:fs';
const watch = process.argv.includes('--watch');
// Version-stamp: read version from package.json, stamp today's build date.
// esbuild `define` replaces the bare identifiers __CLICKSENSE_VERSION__ /
// __CLICKSENSE_BUILD__ at build time; the typeof guard in posthog.js then
// constant-folds to the injected literals.
const pkg = JSON.parse(readFileSync('./package.json', 'utf-8'));
const define = {
__CLICKSENSE_VERSION__: JSON.stringify(pkg.version),
__CLICKSENSE_BUILD__: JSON.stringify(new Date().toISOString().slice(0, 10)),
};
const shared = {
entryPoints: ['src/index.js'],
bundle: true,
minify: true,
sourcemap: true,
define,
};
// ESM — for import in modern projects
await esbuild.build({
...shared,
outfile: 'dist/clicksense.esm.js',
format: 'esm',
});
// IIFE — for <script> tag inclusion, exposes window.ClickSense
await esbuild.build({
...shared,
outfile: 'dist/clicksense.js',
format: 'iife',
globalName: 'ClickSenseLib',
footer: {
// Hoist exports to window for easy script-tag usage:
// <script src="clicksense.js"></script>
// <script>
// new ClickSenseLib.ClickSense({ onCapture: ... })
// </script>
js: '',
},
});
// CJS — for Node/require (test harnesses, SSR)
await esbuild.build({
...shared,
outfile: 'dist/clicksense.cjs.js',
format: 'cjs',
});
console.log('Built: dist/clicksense.js (IIFE), dist/clicksense.esm.js (ESM), dist/clicksense.cjs.js (CJS)');