-
Notifications
You must be signed in to change notification settings - Fork 2
/
esbuild.mjs
38 lines (32 loc) · 939 Bytes
/
esbuild.mjs
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
import {config} from 'dotenv';
import esbuild from 'esbuild';
import {existsSync, mkdirSync, writeFileSync} from 'node:fs';
import {join} from 'node:path';
config({
path: process.env.NODE_ENV === 'development' ? `.env.development` : `.env.production`
});
const define = Object.entries(process.env).reduce(
(acc, [key, value]) => ({
...acc,
[`process.env.${key}`]: JSON.stringify(value)
}),
{}
);
const dist = join(process.cwd(), 'dist');
if (!existsSync(dist)) {
mkdirSync(dist);
}
const script = await esbuild.build({
entryPoints: ['src/index.ts'],
outfile: 'dist/index.mjs',
bundle: true,
minify: true,
format: 'esm',
platform: 'node',
write: false,
banner: {
js: "import { createRequire as topLevelCreateRequire } from 'module';\n const require = topLevelCreateRequire(import.meta.url);"
},
define
});
writeFileSync('dist/index.js', `#!/usr/bin/env node\n${script.outputFiles[0].text}`);