chore: v3.1.25
[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 {
15 availableParallelism = os.availableParallelism()
16 } catch {
562a4037
JB
17 const cpus = os.cpus()
18 if (Array.isArray(cpus) && cpus.length > 0) {
19 availableParallelism = cpus.length
d5fdc57b
JB
20 }
21 }
22 return availableParallelism
23}
24
4a6421b5 25const isDevelopmentBuild = env.BUILD === 'development'
b272ff6e
JB
26const isAnalyzeBuild = Boolean(env.ANALYZE)
27const isDocumentationBuild = Boolean(env.DOCUMENTATION)
d8067870 28const sourcemap = env.SOURCEMAP !== 'false'
660940b0 29
d5fdc57b 30const maxWorkers = Math.floor(availableParallelism() / 2)
509e904b 31
5d4b2a88 32export default defineConfig([
b40c4b06 33 {
78cdf6bc 34 input: './src/index.ts',
b40c4b06
JB
35 strictDeprecations: true,
36 output: [
37 {
38 format: 'cjs',
b272ff6e
JB
39 ...(isDevelopmentBuild
40 ? {
41 dir: './lib',
42 entryFileNames: '[name].cjs',
43 chunkFileNames: '[name]-[hash].cjs',
44 preserveModules: true,
45 preserveModulesRoot: './src'
46 }
47 : {
48 file: './lib/index.cjs',
49 plugins: [terser({ maxWorkers })]
50 }),
d8067870
JB
51 ...(sourcemap && {
52 sourcemap
b40c4b06
JB
53 })
54 },
55 {
56 format: 'esm',
b272ff6e
JB
57 ...(isDevelopmentBuild
58 ? {
59 dir: './lib',
60 entryFileNames: '[name].mjs',
61 chunkFileNames: '[name]-[hash].mjs',
62 preserveModules: true,
63 preserveModulesRoot: './src'
64 }
65 : {
66 file: './lib/index.mjs',
67 plugins: [terser({ maxWorkers })]
68 }),
d8067870
JB
69 ...(sourcemap && {
70 sourcemap
b40c4b06
JB
71 })
72 }
73 ],
e4ec185f 74 external: [/^node:*/],
b40c4b06
JB
75 plugins: [
76 typescript({
5388ef55
JB
77 tsconfig: './tsconfig.build.json',
78 compilerOptions: {
d8067870 79 sourceMap: sourcemap
5388ef55 80 }
34a0cfab 81 }),
b40c4b06 82 del({
78cdf6bc 83 targets: ['./lib/*']
34a0cfab 84 }),
b272ff6e
JB
85 isAnalyzeBuild && analyze(),
86 isDocumentationBuild && command('pnpm typedoc')
b40c4b06
JB
87 ]
88 },
89 {
90 input: './lib/dts/index.d.ts',
b272ff6e 91 strictDeprecations: true,
78cdf6bc 92 output: [{ format: 'esm', file: './lib/index.d.ts' }],
e4ec185f 93 external: [/^node:*/],
b40c4b06
JB
94 plugins: [
95 dts(),
96 del({
78cdf6bc 97 targets: ['./lib/dts'],
b40c4b06 98 hook: 'buildEnd'
096c02a7 99 }),
b272ff6e 100 isAnalyzeBuild && analyze()
b40c4b06
JB
101 ]
102 }
5d4b2a88 103])