refactor: avoid direct process usage
[poolifier.git] / rollup.config.mjs
1 import * as os from 'node:os'
2 import { env } from 'node:process'
3 import { dts } from 'rollup-plugin-dts'
4 import terser from '@rollup/plugin-terser'
5 import typescript from '@rollup/plugin-typescript'
6 import analyze from 'rollup-plugin-analyzer'
7 import command from 'rollup-plugin-command'
8 import del from 'rollup-plugin-delete'
9 import { defineConfig } from 'rollup'
10
11 const availableParallelism = () => {
12 let availableParallelism = 1
13 try {
14 availableParallelism = os.availableParallelism()
15 } catch {
16 const cpus = os.cpus()
17 if (Array.isArray(cpus) && cpus.length > 0) {
18 availableParallelism = cpus.length
19 }
20 }
21 return availableParallelism
22 }
23
24 const isDevelopmentBuild = env.BUILD === 'development'
25 const isAnalyzeBuild = env.ANALYZE
26 const isDocumentationBuild = env.DOCUMENTATION
27
28 const maxWorkers = Math.floor(availableParallelism() / 2)
29
30 export default defineConfig([
31 {
32 input: './src/index.ts',
33 strictDeprecations: true,
34 output: [
35 {
36 format: 'cjs',
37 ...(isDevelopmentBuild && {
38 dir: './lib',
39 sourcemap: true,
40 preserveModules: true,
41 preserveModulesRoot: './src'
42 }),
43 ...(!isDevelopmentBuild && {
44 file: './lib/index.js',
45 sourcemap: true,
46 plugins: [terser({ maxWorkers })]
47 })
48 },
49 {
50 format: 'esm',
51 ...(isDevelopmentBuild && {
52 dir: './lib',
53 sourcemap: true,
54 entryFileNames: '[name].mjs',
55 chunkFileNames: '[name]-[hash].mjs',
56 preserveModules: true,
57 preserveModulesRoot: './src'
58 }),
59 ...(!isDevelopmentBuild && {
60 file: './lib/index.mjs',
61 sourcemap: true,
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({
78 tsconfig: './tsconfig.build.json'
79 }),
80 del({
81 targets: ['./lib/*']
82 }),
83 isAnalyzeBuild && analyze(),
84 isDocumentationBuild && command('pnpm typedoc')
85 ]
86 },
87 {
88 input: './lib/dts/index.d.ts',
89 output: [{ format: 'esm', file: './lib/index.d.ts' }],
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({
100 targets: ['./lib/dts'],
101 hook: 'buildEnd'
102 }),
103 isAnalyzeBuild && analyze()
104 ]
105 }
106 ])