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