build: refine rollup configuration
[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({
5388ef55
JB
81 tsconfig: './tsconfig.build.json',
82 compilerOptions: {
83 sourceMap: sourcemap
84 }
34a0cfab 85 }),
b40c4b06 86 del({
78cdf6bc 87 targets: ['./lib/*']
34a0cfab 88 }),
209917a7
JB
89 Boolean(isAnalyzeBuild) && analyze(),
90 Boolean(isDocumentationBuild) && command('pnpm typedoc')
b40c4b06
JB
91 ]
92 },
93 {
94 input: './lib/dts/index.d.ts',
78cdf6bc 95 output: [{ format: 'esm', file: './lib/index.d.ts' }],
b40c4b06
JB
96 external: [
97 'node:async_hooks',
98 'node:cluster',
99 'node:events',
100 'node:perf_hooks',
101 'node:worker_threads'
102 ],
103 plugins: [
104 dts(),
105 del({
78cdf6bc 106 targets: ['./lib/dts'],
b40c4b06 107 hook: 'buildEnd'
096c02a7 108 }),
209917a7 109 Boolean(isAnalyzeBuild) && analyze()
b40c4b06
JB
110 ]
111 }
5d4b2a88 112])