refactor: avoid direct process usage
[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
660940b0 27
d5fdc57b 28const maxWorkers = Math.floor(availableParallelism() / 2)
509e904b 29
5d4b2a88 30export default defineConfig([
b40c4b06 31 {
78cdf6bc 32 input: './src/index.ts',
b40c4b06
JB
33 strictDeprecations: true,
34 output: [
35 {
36 format: 'cjs',
37 ...(isDevelopmentBuild && {
78cdf6bc 38 dir: './lib',
b40c4b06
JB
39 sourcemap: true,
40 preserveModules: true,
78cdf6bc 41 preserveModulesRoot: './src'
b40c4b06
JB
42 }),
43 ...(!isDevelopmentBuild && {
78cdf6bc 44 file: './lib/index.js',
890ed48b 45 sourcemap: true,
b40c4b06
JB
46 plugins: [terser({ maxWorkers })]
47 })
48 },
49 {
50 format: 'esm',
51 ...(isDevelopmentBuild && {
78cdf6bc 52 dir: './lib',
b40c4b06
JB
53 sourcemap: true,
54 entryFileNames: '[name].mjs',
efaeaba6 55 chunkFileNames: '[name]-[hash].mjs',
b40c4b06 56 preserveModules: true,
78cdf6bc 57 preserveModulesRoot: './src'
b40c4b06
JB
58 }),
59 ...(!isDevelopmentBuild && {
78cdf6bc 60 file: './lib/index.mjs',
890ed48b 61 sourcemap: true,
b40c4b06
JB
62 plugins: [terser({ maxWorkers })]
63 })
64 }
65 ],
66 external: [
67 'node:async_hooks',
68 'node:cluster',
69 'node:crypto',
70 'node:events',
71 'node:fs',
72 'node:os',
73 'node:perf_hooks',
74 'node:worker_threads'
75 ],
76 plugins: [
77 typescript({
890ed48b 78 tsconfig: './tsconfig.build.json'
34a0cfab 79 }),
b40c4b06 80 del({
78cdf6bc 81 targets: ['./lib/*']
34a0cfab 82 }),
b40c4b06
JB
83 isAnalyzeBuild && analyze(),
84 isDocumentationBuild && command('pnpm typedoc')
85 ]
86 },
87 {
88 input: './lib/dts/index.d.ts',
78cdf6bc 89 output: [{ format: 'esm', file: './lib/index.d.ts' }],
b40c4b06
JB
90 external: [
91 'node:async_hooks',
92 'node:cluster',
93 'node:events',
94 'node:perf_hooks',
95 'node:worker_threads'
96 ],
97 plugins: [
98 dts(),
99 del({
78cdf6bc 100 targets: ['./lib/dts'],
b40c4b06 101 hook: 'buildEnd'
096c02a7
JB
102 }),
103 isAnalyzeBuild && analyze()
b40c4b06
JB
104 ]
105 }
5d4b2a88 106])