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