build: improve number of workers computation
[e-mobility-charging-stations-simulator.git] / rollup.config.mjs
1 /* eslint-disable n/no-unpublished-import */
2 import * as os from 'node:os';
3
4 import json from '@rollup/plugin-json';
5 import terser from '@rollup/plugin-terser';
6 import typescript from '@rollup/plugin-typescript';
7 import { copy } from '@web/rollup-plugin-copy';
8 import analyze from 'rollup-plugin-analyzer';
9 import del from 'rollup-plugin-delete';
10
11 const availableParallelism = () => {
12 // eslint-disable-next-line no-shadow
13 let availableParallelism = 1;
14 try {
15 availableParallelism = os.availableParallelism();
16 } catch {
17 const numberOfCpus = os.cpus();
18 if (Array.isArray(numberOfCpus) && numberOfCpus.length > 0) {
19 availableParallelism = numberOfCpus.length;
20 }
21 }
22 return availableParallelism;
23 };
24
25 const isDevelopmentBuild = process.env.BUILD === 'development';
26 const isAnalyzeBuild = process.env.ANALYZE;
27
28 export default {
29 input: ['src/start.ts', 'src/charging-station/ChargingStationWorker.ts'],
30 strictDeprecations: true,
31 output: [
32 {
33 dir: 'dist',
34 format: 'esm',
35 sourcemap: !!isDevelopmentBuild,
36 plugins: [terser({ maxWorkers: Math.floor(availableParallelism() / 2) })],
37 },
38 ],
39 external: [
40 '@mikro-orm/core',
41 '@mikro-orm/reflection',
42 'ajv',
43 'ajv-formats',
44 'basic-ftp',
45 'chalk',
46 'http-status-codes',
47 'just-clone',
48 'just-merge',
49 'mnemonist/lru-map-with-delete.js',
50 'mnemonist/queue.js',
51 'moment',
52 'mongodb',
53 'node:async_hooks',
54 'node:crypto',
55 'node:events',
56 'node:fs',
57 'node:http',
58 'node:path',
59 'node:perf_hooks',
60 'node:stream',
61 'node:url',
62 'node:util',
63 'node:worker_threads',
64 'poolifier',
65 'tar',
66 'winston',
67 'winston-daily-rotate-file',
68 'winston/lib/winston/transports/index.js',
69 'ws',
70 ],
71 plugins: [
72 json(),
73 typescript({
74 tsconfig: 'tsconfig.json',
75 compilerOptions: {
76 sourceMap: !!isDevelopmentBuild,
77 },
78 }),
79 del({
80 targets: [
81 'dist/*',
82 '!dist/assets',
83 'dist/assets/*.json',
84 'dist/assets/json-schemas',
85 'dist/assets/station-templates',
86 'dist/assets/ui-protocol',
87 ],
88 }),
89 copy({
90 rootDir: 'src',
91 patterns: 'assets/**/*.json',
92 exclude: [
93 'assets/config-template.json',
94 'assets/*config[-_.]*.json',
95 'assets/idtags-template.json',
96 'assets/authorization-tags-template.json',
97 'assets/ui-protocol/**/*',
98 ],
99 }),
100 isAnalyzeBuild && analyze(),
101 ],
102 };