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