Merge dependabot/npm_and_yarn/examples/typescript/http-client-pool/poolifier-2.6...
[poolifier.git] / rollup.config.mjs
1 import * as os from 'node:os'
2 import { dts } from 'rollup-plugin-dts'
3 import terser from '@rollup/plugin-terser'
4 import typescript from '@rollup/plugin-typescript'
5 import analyze from 'rollup-plugin-analyzer'
6 import command from 'rollup-plugin-command'
7 import del from 'rollup-plugin-delete'
8
9 const availableParallelism = () => {
10 let availableParallelism = 1
11 try {
12 availableParallelism = os.availableParallelism()
13 } catch {
14 const cpus = os.cpus()
15 if (Array.isArray(cpus) && cpus.length > 0) {
16 availableParallelism = cpus.length
17 }
18 }
19 return availableParallelism
20 }
21
22 const isDevelopmentBuild = process.env.BUILD === 'development'
23 const isAnalyzeBuild = process.env.ANALYZE
24 const isDocumentationBuild = process.env.DOCUMENTATION
25
26 const maxWorkers = Math.floor(availableParallelism() / 2)
27
28 export default [
29 {
30 input: 'src/index.ts',
31 strictDeprecations: true,
32 output: [
33 {
34 format: 'cjs',
35 ...(isDevelopmentBuild && {
36 dir: 'lib',
37 sourcemap: true,
38 preserveModules: true,
39 preserveModulesRoot: 'src'
40 }),
41 ...(!isDevelopmentBuild && {
42 file: 'lib/index.js',
43 plugins: [terser({ maxWorkers })]
44 })
45 },
46 {
47 format: 'esm',
48 ...(isDevelopmentBuild && {
49 dir: 'lib',
50 sourcemap: true,
51 entryFileNames: '[name].mjs',
52 preserveModules: true,
53 preserveModulesRoot: 'src'
54 }),
55 ...(!isDevelopmentBuild && {
56 file: 'lib/index.mjs',
57 plugins: [terser({ maxWorkers })]
58 })
59 }
60 ],
61 external: [
62 'node:async_hooks',
63 'node:cluster',
64 'node:crypto',
65 'node:events',
66 'node:fs',
67 'node:os',
68 'node:perf_hooks',
69 'node:worker_threads'
70 ],
71 plugins: [
72 typescript({
73 tsconfig: isDevelopmentBuild
74 ? 'tsconfig.development.json'
75 : 'tsconfig.production.json'
76 }),
77 del({
78 targets: ['lib/*']
79 }),
80 isAnalyzeBuild && analyze(),
81 isDocumentationBuild && command('pnpm typedoc')
82 ]
83 },
84 {
85 input: './lib/dts/index.d.ts',
86 output: [{ format: 'esm', file: 'lib/index.d.ts' }],
87 external: [
88 'node:async_hooks',
89 'node:cluster',
90 'node:events',
91 'node:perf_hooks',
92 'node:worker_threads'
93 ],
94 plugins: [
95 dts(),
96 del({
97 targets: ['lib/dts'],
98 hook: 'buildEnd'
99 }),
100 isAnalyzeBuild && analyze()
101 ]
102 }
103 ]