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