summaryrefslogtreecommitdiff
path: root/packages/backend/build.js
blob: 52ca09b7a8438cb06e047024e7a163a93038581d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import fs from 'node:fs';
import { fileURLToPath } from 'node:url';
import { dirname, join } from 'node:path';
import { build } from 'esbuild';
import { swcPlugin } from 'esbuild-plugin-swc';

const _filename = fileURLToPath(import.meta.url);
const _dirname = dirname(_filename);
const _package = JSON.parse(fs.readFileSync(_dirname + '/package.json', 'utf-8'));

const resolveTsPathsPlugin = {
	name: 'resolve-ts-paths',
	setup(build) {
		build.onResolve({ filter: /^\.{1,2}\/.*\.js$/ }, (args) => {
			if (args.importer) {
				const absPath = join(args.resolveDir, args.path);
				const tsPath = absPath.slice(0, -3) + '.ts';
				if (fs.existsSync(tsPath)) return { path: tsPath };
				const tsxPath = absPath.slice(0, -3) + '.tsx';
				if (fs.existsSync(tsxPath)) return { path: tsxPath };
			}
		});
	},
};

const externalIpaddrPlugin = {
	name: 'external-ipaddr',
	setup(build) {
		build.onResolve({ filter: /^ipaddr\.js$/ }, (args) => {
			return { path: args.path, external: true };
		});
	},
};

/** @type {import('esbuild').BuildOptions} */
const options = {
	entryPoints: ['./src/boot/entry.ts'],
	minify: true,
	keepNames: true,
	bundle: true,
	outdir: './built/boot',
	target: 'node22',
	platform: 'node',
	format: 'esm',
	sourcemap: 'linked',
	packages: 'external',
	banner: {
		js: 'import { createRequire as topLevelCreateRequire } from "module";' +
			'import ___url___ from "url";' +
			'const require = topLevelCreateRequire(import.meta.url);' +
			'const __filename = ___url___.fileURLToPath(import.meta.url);' +
			'const __dirname = ___url___.fileURLToPath(new URL(".", import.meta.url));',
	},
	plugins: [
		externalIpaddrPlugin,
		resolveTsPathsPlugin,
		swcPlugin({
			jsc: {
				parser: {
					syntax: 'typescript',
					decorators: true,
					dynamicImport: true,
				},
				transform: {
					legacyDecorator: true,
					decoratorMetadata: true,
				},
				experimental: {
					keepImportAssertions: true,
				},
				baseUrl: join(_dirname, 'src'),
				paths: {
					'@/*': ['*'],
				},
				target: 'esnext',
				keepClassNames: true,
			},
		}),
		externalIpaddrPlugin,
	],
	// external: [
	// 	'slacc-*',
	// 	'class-transformer',
	// 	'class-validator',
	// 	'@sentry/*',
	// 	'@nestjs/websockets/socket-module',
	// 	'@nestjs/microservices/microservices-module',
	// 	'@nestjs/microservices',
	// 	'@napi-rs/canvas-win32-x64-msvc',
	// 	'mock-aws-s3',
	// 	'aws-sdk',
	// 	'nock',
	// 	'sharp',
	// 	'jsdom',
	// 	're2',
	// 	'@napi-rs/canvas',
	// ],
};

const args = process.argv.slice(2).map(arg => arg.toLowerCase());

if (!args.includes('--no-clean')) {
	fs.rmSync('./built', { recursive: true, force: true });
}

await buildSrc();

async function buildSrc() {
	console.log(`[${_package.name}] start building...`);

	await build(options)
		.then(() => {
			console.log(`[${_package.name}] build succeeded.`);
		})
		.catch((err) => {
			process.stderr.write(err.stderr || err.message || err);
			process.exit(1);
		});

	console.log(`[${_package.name}] finish building.`);
}