docs: generate documentation
[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
d8067870 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',
d35e5717
JB
40 entryFileNames: '[name].cjs',
41 chunkFileNames: '[name]-[hash].cjs',
b40c4b06 42 preserveModules: true,
78cdf6bc 43 preserveModulesRoot: './src'
b40c4b06
JB
44 }),
45 ...(!isDevelopmentBuild && {
d35e5717 46 file: './lib/index.cjs',
b40c4b06 47 plugins: [terser({ maxWorkers })]
d8067870
JB
48 }),
49 ...(sourcemap && {
50 sourcemap
b40c4b06
JB
51 })
52 },
53 {
54 format: 'esm',
55 ...(isDevelopmentBuild && {
78cdf6bc 56 dir: './lib',
b40c4b06 57 entryFileNames: '[name].mjs',
efaeaba6 58 chunkFileNames: '[name]-[hash].mjs',
b40c4b06 59 preserveModules: true,
78cdf6bc 60 preserveModulesRoot: './src'
b40c4b06
JB
61 }),
62 ...(!isDevelopmentBuild && {
78cdf6bc 63 file: './lib/index.mjs',
b40c4b06 64 plugins: [terser({ maxWorkers })]
d8067870
JB
65 }),
66 ...(sourcemap && {
67 sourcemap
b40c4b06
JB
68 })
69 }
70 ],
e4ec185f 71 external: [/^node:*/],
b40c4b06
JB
72 plugins: [
73 typescript({
5388ef55
JB
74 tsconfig: './tsconfig.build.json',
75 compilerOptions: {
d8067870 76 sourceMap: sourcemap
5388ef55 77 }
34a0cfab 78 }),
b40c4b06 79 del({
78cdf6bc 80 targets: ['./lib/*']
34a0cfab 81 }),
209917a7
JB
82 Boolean(isAnalyzeBuild) && analyze(),
83 Boolean(isDocumentationBuild) && command('pnpm typedoc')
b40c4b06
JB
84 ]
85 },
86 {
87 input: './lib/dts/index.d.ts',
78cdf6bc 88 output: [{ format: 'esm', file: './lib/index.d.ts' }],
e4ec185f 89 external: [/^node:*/],
b40c4b06
JB
90 plugins: [
91 dts(),
92 del({
78cdf6bc 93 targets: ['./lib/dts'],
b40c4b06 94 hook: 'buildEnd'
096c02a7 95 }),
209917a7 96 Boolean(isAnalyzeBuild) && analyze()
b40c4b06
JB
97 ]
98 }
5d4b2a88 99])