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