|
| 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