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