-
Notifications
You must be signed in to change notification settings - Fork 0
/
esbuild.mjs
56 lines (50 loc) · 1.63 KB
/
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import * as esbuild from 'esbuild';
import pckg from './package.json' with {type: 'json'};
import { execSync } from 'child_process';
/**
* Plugin to inject build metadata.
* @returns {esbuild.Plugin}
*/
function buildMetadataPlugin() {
return {
name: 'build-metadata',
setup(build) {
let buildDate = new Date().toISOString();
buildDate = buildDate.substring(0, buildDate.indexOf('T'));
let gitHash = 'unknown';
try {
gitHash = execSync('git rev-parse --short HEAD').toString().trim();
} catch (error) {
console.warn('Could not retrieve git hash:', error.message);
}
const buildMetadata = {
buildDate, gitHash, version: pckg.version,
};
// Define a namespace for replacing metadata.
build.onResolve({ filter: /BUILD_METADATA/ }, args => ({
path: args.path,
namespace: 'build-metadata'
}));
// Provide the actual metadata content.
build.onLoad({ filter: /.*/, namespace: 'build-metadata' }, () => ({
contents: JSON.stringify(buildMetadata),
loader: 'json'
}));
}
};
}
const isBuild = process.argv[2] === 'build';
const ctx = await esbuild.context({
entryPoints: ['src/index.mjs'],
bundle: true,
outfile: 'public/bundle.js',
plugins: [buildMetadataPlugin()],
write: isBuild,
minify: isBuild,
});
if (!isBuild) {
ctx.serve({ port: 5000, servedir: 'public/' });
} else {
await ctx.rebuild();
await ctx.dispose();
}