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