| 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 | preserveModules: true, |
| 41 | preserveModulesRoot: './src' |
| 42 | }), |
| 43 | ...(!isDevelopmentBuild && { |
| 44 | file: './lib/index.js', |
| 45 | plugins: [terser({ maxWorkers })] |
| 46 | }), |
| 47 | ...(sourcemap && { |
| 48 | sourcemap |
| 49 | }) |
| 50 | }, |
| 51 | { |
| 52 | format: 'esm', |
| 53 | ...(isDevelopmentBuild && { |
| 54 | dir: './lib', |
| 55 | entryFileNames: '[name].mjs', |
| 56 | chunkFileNames: '[name]-[hash].mjs', |
| 57 | preserveModules: true, |
| 58 | preserveModulesRoot: './src' |
| 59 | }), |
| 60 | ...(!isDevelopmentBuild && { |
| 61 | file: './lib/index.mjs', |
| 62 | plugins: [terser({ maxWorkers })] |
| 63 | }), |
| 64 | ...(sourcemap && { |
| 65 | sourcemap |
| 66 | }) |
| 67 | } |
| 68 | ], |
| 69 | external: [ |
| 70 | 'node:async_hooks', |
| 71 | 'node:cluster', |
| 72 | 'node:crypto', |
| 73 | 'node:events', |
| 74 | 'node:fs', |
| 75 | 'node:os', |
| 76 | 'node:perf_hooks', |
| 77 | 'node:worker_threads' |
| 78 | ], |
| 79 | plugins: [ |
| 80 | typescript({ |
| 81 | tsconfig: './tsconfig.build.json', |
| 82 | compilerOptions: { |
| 83 | sourceMap: sourcemap |
| 84 | } |
| 85 | }), |
| 86 | del({ |
| 87 | targets: ['./lib/*'] |
| 88 | }), |
| 89 | isAnalyzeBuild && analyze(), |
| 90 | isDocumentationBuild && command('pnpm typedoc') |
| 91 | ] |
| 92 | }, |
| 93 | { |
| 94 | input: './lib/dts/index.d.ts', |
| 95 | output: [{ format: 'esm', file: './lib/index.d.ts' }], |
| 96 | external: [ |
| 97 | 'node:async_hooks', |
| 98 | 'node:cluster', |
| 99 | 'node:events', |
| 100 | 'node:perf_hooks', |
| 101 | 'node:worker_threads' |
| 102 | ], |
| 103 | plugins: [ |
| 104 | dts(), |
| 105 | del({ |
| 106 | targets: ['./lib/dts'], |
| 107 | hook: 'buildEnd' |
| 108 | }), |
| 109 | isAnalyzeBuild && analyze() |
| 110 | ] |
| 111 | } |
| 112 | ]) |