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