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