build: enfore tree shaking with esbuild
[e-mobility-charging-stations-simulator.git] / bundle.js
1 /* eslint-disable n/no-unpublished-import */
2 import { env } from 'node:process'
3
4 import chalk from 'chalk'
5 import { build } from 'esbuild'
6 import { clean } from 'esbuild-plugin-clean'
7 import { copy } from 'esbuild-plugin-copy'
8
9 const isDevelopmentBuild = env.BUILD === 'development'
10 const sourcemap = !!isDevelopmentBuild
11 console.info(chalk.green(`Building in ${isDevelopmentBuild ? 'development' : 'production'} mode`))
12 console.time('Build time')
13 await build({
14 entryPoints: ['./src/start.ts', './src/charging-station/ChargingStationWorker.ts'],
15 bundle: true,
16 platform: 'node',
17 format: 'esm',
18 external: [
19 '@mikro-orm/*',
20 'ajv',
21 'ajv-formats',
22 'basic-ftp',
23 'chalk',
24 'date-fns',
25 'http-status-codes',
26 'logform',
27 'mnemonist',
28 'mongodb',
29 'node:*',
30 'poolifier',
31 'rambda',
32 'tar',
33 'winston',
34 'winston/*',
35 'winston-daily-rotate-file',
36 'ws'
37 ],
38 treeShaking: true,
39 minify: true,
40 sourcemap,
41 entryNames: '[name]',
42 outdir: './dist',
43 plugins: [
44 clean({
45 patterns: [
46 './dist/*',
47 '!./dist/assets',
48 './dist/assets/*.json',
49 './dist/assets/json-schemas',
50 './dist/assets/station-templates',
51 './dist/assets/ui-protocol',
52 './dist/assets/configs-docker'
53 ]
54 }),
55 copy({
56 assets: [
57 {
58 from: ['./src/assets/config.json'],
59 to: ['./assets']
60 },
61 {
62 from: ['./src/assets/idtags!(-template)*.json'],
63 to: ['./assets']
64 },
65 {
66 from: ['./src/assets/json-schemas/**/*.json'],
67 to: ['./assets/json-schemas']
68 },
69 {
70 from: ['./src/assets/station-templates/**/*.json'],
71 to: ['./assets/station-templates']
72 },
73 {
74 from: ['./src/assets/configs-docker/*.json'],
75 to: ['./assets/configs-docker']
76 }
77 ]
78 })
79 ]
80 })
81 console.timeEnd('Build time')