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