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