-
Notifications
You must be signed in to change notification settings - Fork 283
/
Copy pathbundle-cjs.mjs
executable file
·64 lines (57 loc) · 1.96 KB
/
bundle-cjs.mjs
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
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env node
import { dirname, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
import * as esbuild from 'esbuild';
import getPackageVersion from './get-package-version.mjs';
import packageJson from '../package.json' with { type: 'json' };
// import.meta.dirname is not available before Node 20
const __dirname = dirname(fileURLToPath(import.meta.url));
const sdkEntrypoint = resolve(__dirname, '../src/index.ts');
const emojiEntrypoint = resolve(__dirname, '../src/plugins/Emojis/index.ts');
const mp3EncoderEntrypoint = resolve(__dirname, '../src/plugins/encoders/mp3.ts');
const experimentalEntrypoint = resolve(__dirname, '../src/experimental/index.ts');
const outDir = resolve(__dirname, '../dist');
// Those dependencies are distributed as ES modules, and cannot be externalized
// in our CJS bundle. We convert them to CJS and bundle them instead.
const bundledDeps = [
'hast-util-find-and-replace',
'unist-builder',
'unist-util-visit',
'react-markdown',
'remark-gfm',
];
const deps = Object.keys({
...packageJson.dependencies,
...packageJson.peerDependencies,
});
const external = deps.filter((dep) => !bundledDeps.includes(dep));
/** @type esbuild.BuildOptions */
const cjsBundleConfig = {
entryPoints: [
sdkEntrypoint,
emojiEntrypoint,
mp3EncoderEntrypoint,
experimentalEntrypoint,
],
bundle: true,
format: 'cjs',
target: 'es2020',
external,
outdir: outDir,
outExtension: { '.js': '.cjs' },
sourcemap: 'linked',
};
// We build two CJS bundles: for browser and for node. The latter one can be
// used e.g. during SSR (although it makes little sence to SSR chat, but still
// nice for import not to break on server).
const bundles = ['browser', 'node'].map((platform) =>
esbuild.build({
...cjsBundleConfig,
entryNames: `[dir]/[name].${platform}`,
platform,
define: {
'process.env.STREAM_CHAT_REACT_VERSION': JSON.stringify(getPackageVersion()),
},
}),
);
await Promise.all(bundles);