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