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 { | |
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 | { |
78cdf6bc | 34 | input: './src/index.ts', |
b40c4b06 JB |
35 | strictDeprecations: true, |
36 | output: [ | |
37 | { | |
38 | format: 'cjs', | |
b272ff6e JB |
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 | }), | |
d8067870 JB |
51 | ...(sourcemap && { |
52 | sourcemap | |
b40c4b06 JB |
53 | }) |
54 | }, | |
55 | { | |
56 | format: 'esm', | |
b272ff6e JB |
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 | }), | |
d8067870 JB |
69 | ...(sourcemap && { |
70 | sourcemap | |
b40c4b06 JB |
71 | }) |
72 | } | |
73 | ], | |
e4ec185f | 74 | external: [/^node:*/], |
b40c4b06 JB |
75 | plugins: [ |
76 | typescript({ | |
5388ef55 JB |
77 | tsconfig: './tsconfig.build.json', |
78 | compilerOptions: { | |
d8067870 | 79 | sourceMap: sourcemap |
5388ef55 | 80 | } |
34a0cfab | 81 | }), |
b40c4b06 | 82 | del({ |
78cdf6bc | 83 | targets: ['./lib/*'] |
34a0cfab | 84 | }), |
b272ff6e JB |
85 | isAnalyzeBuild && analyze(), |
86 | isDocumentationBuild && command('pnpm typedoc') | |
b40c4b06 JB |
87 | ] |
88 | }, | |
89 | { | |
90 | input: './lib/dts/index.d.ts', | |
b272ff6e | 91 | strictDeprecations: true, |
78cdf6bc | 92 | output: [{ format: 'esm', file: './lib/index.d.ts' }], |
e4ec185f | 93 | external: [/^node:*/], |
b40c4b06 JB |
94 | plugins: [ |
95 | dts(), | |
96 | del({ | |
78cdf6bc | 97 | targets: ['./lib/dts'], |
b40c4b06 | 98 | hook: 'buildEnd' |
096c02a7 | 99 | }), |
b272ff6e | 100 | isAnalyzeBuild && analyze() |
b40c4b06 JB |
101 | ] |
102 | } | |
5d4b2a88 | 103 | ]) |