build(deps-dev): bump @types/express
[poolifier.git] / rollup.config.mjs
CommitLineData
d5fdc57b 1import * as os from 'node:os'
4a6421b5 2import { env } from 'node:process'
b40c4b06 3import { dts } from 'rollup-plugin-dts'
8f98d75d
JB
4import terser from '@rollup/plugin-terser'
5import typescript from '@rollup/plugin-typescript'
f45e4999 6import analyze from 'rollup-plugin-analyzer'
5ea22628 7import command from 'rollup-plugin-command'
eae1fc25 8import del from 'rollup-plugin-delete'
5d4b2a88 9import { defineConfig } from 'rollup'
660940b0 10
d5fdc57b
JB
11const availableParallelism = () => {
12 let availableParallelism = 1
13 try {
14 availableParallelism = os.availableParallelism()
15 } catch {
562a4037
JB
16 const cpus = os.cpus()
17 if (Array.isArray(cpus) && cpus.length > 0) {
18 availableParallelism = cpus.length
d5fdc57b
JB
19 }
20 }
21 return availableParallelism
22}
23
4a6421b5
JB
24const isDevelopmentBuild = env.BUILD === 'development'
25const isAnalyzeBuild = env.ANALYZE
26const isDocumentationBuild = env.DOCUMENTATION
f8496b22 27const sourcemap = env.SOURCEMAP !== 'false'
660940b0 28
d5fdc57b 29const maxWorkers = Math.floor(availableParallelism() / 2)
509e904b 30
5d4b2a88 31export default defineConfig([
b40c4b06 32 {
78cdf6bc 33 input: './src/index.ts',
b40c4b06
JB
34 strictDeprecations: true,
35 output: [
36 {
37 format: 'cjs',
38 ...(isDevelopmentBuild && {
78cdf6bc 39 dir: './lib',
b40c4b06 40 preserveModules: true,
78cdf6bc 41 preserveModulesRoot: './src'
b40c4b06
JB
42 }),
43 ...(!isDevelopmentBuild && {
78cdf6bc 44 file: './lib/index.js',
b40c4b06 45 plugins: [terser({ maxWorkers })]
f8496b22
JB
46 }),
47 ...(sourcemap && {
48 sourcemap
b40c4b06
JB
49 })
50 },
51 {
52 format: 'esm',
53 ...(isDevelopmentBuild && {
78cdf6bc 54 dir: './lib',
b40c4b06 55 entryFileNames: '[name].mjs',
efaeaba6 56 chunkFileNames: '[name]-[hash].mjs',
b40c4b06 57 preserveModules: true,
78cdf6bc 58 preserveModulesRoot: './src'
b40c4b06
JB
59 }),
60 ...(!isDevelopmentBuild && {
78cdf6bc 61 file: './lib/index.mjs',
b40c4b06 62 plugins: [terser({ maxWorkers })]
f8496b22
JB
63 }),
64 ...(sourcemap && {
65 sourcemap
b40c4b06
JB
66 })
67 }
68 ],
69 external: [
70 'node:async_hooks',
71 'node:cluster',
72 'node:crypto',
73 'node:events',
74 'node:fs',
75 'node:os',
76 'node:perf_hooks',
77 'node:worker_threads'
78 ],
79 plugins: [
80 typescript({
890ed48b 81 tsconfig: './tsconfig.build.json'
34a0cfab 82 }),
b40c4b06 83 del({
78cdf6bc 84 targets: ['./lib/*']
34a0cfab 85 }),
b40c4b06
JB
86 isAnalyzeBuild && analyze(),
87 isDocumentationBuild && command('pnpm typedoc')
88 ]
89 },
90 {
91 input: './lib/dts/index.d.ts',
78cdf6bc 92 output: [{ format: 'esm', file: './lib/index.d.ts' }],
b40c4b06
JB
93 external: [
94 'node:async_hooks',
95 'node:cluster',
96 'node:events',
97 'node:perf_hooks',
98 'node:worker_threads'
99 ],
100 plugins: [
101 dts(),
102 del({
78cdf6bc 103 targets: ['./lib/dts'],
b40c4b06 104 hook: 'buildEnd'
096c02a7
JB
105 }),
106 isAnalyzeBuild && analyze()
b40c4b06
JB
107 ]
108 }
5d4b2a88 109])