Skip to content

Commit 3121f3a

Browse files
build(audience): add tsup.cdn.js for single-file IIFE CDN bundle
- Produces dist/cdn/imtbl-audience.global.js as a minified, source-mapped IIFE. - Inlines @imtbl/audience-core via noExternal so the bundle has no runtime dep resolution. - Substitutes __SDK_VERSION__ at build time; dev builds get 0.0.0-local for visibility. - Uses clean: false so it runs alongside tsup.config.js without wiping dist/browser and dist/node. - Not yet wired into pnpm build — next commit. Refs SDK-115
1 parent 3d63b6e commit 3121f3a

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

packages/audience/sdk/tsup.cdn.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// @ts-check
2+
import { defineConfig } from 'tsup';
3+
import { replace } from 'esbuild-plugin-replace';
4+
import pkg from './package.json' with { type: 'json' };
5+
6+
/**
7+
* CDN bundle config for @imtbl/audience.
8+
*
9+
* Produces a single-file IIFE at dist/cdn/imtbl-audience.global.js that
10+
* studios load via <script>. Bundles audience-core directly (no runtime
11+
* dependency resolution), attaches globals via the src/cdn.ts side-effect
12+
* entry point, and minifies for size.
13+
*
14+
* Runs alongside the main ESM build configured in tsup.config.js — they
15+
* share the dist/ directory so `clean: false` is required to avoid wiping
16+
* the ESM output. The package.json build script runs transpile (ESM/CJS)
17+
* first, then transpile:cdn, then typegen, so the order-dependent dist/
18+
* state is well-defined.
19+
*
20+
* The esbuild replace plugin substitutes '__SDK_VERSION__' with the
21+
* actual package version at build time. Both @imtbl/audience and
22+
* @imtbl/audience-core use '__SDK_VERSION__' as a placeholder in their
23+
* source (config.ts / context.ts) so the real version gets stamped
24+
* into context.libraryVersion on every message. Without this replacement
25+
* the CDN bundle ships the literal placeholder and the backend 400s.
26+
*
27+
* Local dev builds have pkg.version === '0.0.0' because the real version
28+
* is only written by `pnpm nx release version` in the publish workflow.
29+
* We substitute '0.0.0-local' in that case so a hand-carried local build
30+
* shows up as obviously non-production in context.libraryVersion logs.
31+
*/
32+
export default defineConfig({
33+
entry: { 'imtbl-audience': 'src/cdn.ts' },
34+
format: ['iife'],
35+
outDir: 'dist/cdn',
36+
outExtension: () => ({ js: '.global.js' }),
37+
minify: true,
38+
sourcemap: true,
39+
clean: false,
40+
target: 'es2018',
41+
platform: 'browser',
42+
dts: false,
43+
// IIFE has no runtime module resolution, so every dep (including third-party
44+
// npm deps) must be inlined. Can't reuse BUNDLED_WORKSPACE_DEPS from the sibling
45+
// tsup.config.js — that list is workspace-only and would leave third-party deps
46+
// as externals, breaking the <script>-tag use case.
47+
noExternal: [/.*/],
48+
esbuildPlugins: [
49+
replace({
50+
__SDK_VERSION__: pkg.version === '0.0.0' ? '0.0.0-local' : pkg.version,
51+
}),
52+
],
53+
});

0 commit comments

Comments
 (0)