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