Skip to content

Commit fbe39cc

Browse files
committed
chore: optimise build script
1 parent 7490d3f commit fbe39cc

File tree

1 file changed

+40
-38
lines changed

1 file changed

+40
-38
lines changed

local-tests/build.mjs

Lines changed: 40 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import * as esbuild from 'esbuild';
22
import { nodeExternalsPlugin } from 'esbuild-node-externals';
3-
import fs from 'fs';
43
import { fileURLToPath } from 'url';
54

65
const ALLOW_LIST = [
@@ -11,54 +10,57 @@ const ALLOW_LIST = [
1110
'secp256k1',
1211
];
1312

13+
const getPath = (relativePath) =>
14+
fileURLToPath(new URL(relativePath, import.meta.url));
15+
16+
/**
17+
* Common esbuild configuration options.
18+
* @param {string} entry - Entry file path.
19+
* @param {string} outfile - Output file path.
20+
* @param {string} [globalName] - Optional global name for the bundle.
21+
* @returns {esbuild.BuildOptions} Esbuild configuration object.
22+
*/
23+
const createBuildConfig = (entry, outfile, globalName) => ({
24+
entryPoints: [getPath(entry)],
25+
outfile: getPath(outfile),
26+
bundle: true,
27+
plugins: [
28+
nodeExternalsPlugin({
29+
allowList: ALLOW_LIST,
30+
}),
31+
],
32+
platform: 'node',
33+
target: 'esnext',
34+
format: 'esm',
35+
inject: [getPath('./shim.mjs')],
36+
mainFields: ['module', 'main'],
37+
...(globalName ? { globalName } : {}),
38+
});
39+
1440
/**
15-
* Build the CLI enabled version of Tinny.
16-
* @returns {Promise<void>} A promise that resolves when the build is complete.
41+
* Builds the CLI-enabled version of Tinny.
1742
*/
1843
export const build = async () => {
19-
await esbuild.build({
20-
entryPoints: [fileURLToPath(new URL('./test.ts', import.meta.url))],
21-
outfile: fileURLToPath(new URL('./build/test.mjs', import.meta.url)),
22-
bundle: true,
23-
plugins: [
24-
nodeExternalsPlugin({
25-
allowList: ALLOW_LIST,
26-
}),
27-
],
28-
platform: 'node',
29-
target: 'esnext',
30-
format: 'esm',
31-
inject: [fileURLToPath(new URL('./shim.mjs', import.meta.url))],
32-
mainFields: ['module', 'main'],
33-
});
44+
await esbuild.build(createBuildConfig('./test.ts', './build/test.mjs'));
3445
};
3546

3647
/**
37-
* Bundle Tinny to be a standalone package.
48+
* Bundles Tinny as a standalone package.
3849
*/
3950
export const bundle = async () => {
40-
await esbuild.build({
41-
entryPoints: [fileURLToPath(new URL('./index.ts', import.meta.url))],
42-
outfile: fileURLToPath(new URL('./index.js', import.meta.url)),
43-
bundle: true,
44-
globalName: 'tinnySdk',
45-
plugins: [
46-
nodeExternalsPlugin({
47-
allowList: ALLOW_LIST,
48-
}),
49-
],
50-
platform: 'node',
51-
target: 'esnext',
52-
format: 'esm',
53-
inject: [fileURLToPath(new URL('./shim.mjs', import.meta.url))],
54-
mainFields: ['module', 'main'],
55-
});
51+
await esbuild.build(
52+
createBuildConfig('./index.ts', './index.js', 'tinnySdk')
53+
);
5654
};
5755

5856
// Go!
5957
(async () => {
6058
const start = Date.now();
61-
await build();
62-
await bundle();
63-
console.log(`[build.mjs] 🚀 Build time: ${Date.now() - start}ms`);
59+
try {
60+
await build();
61+
await bundle();
62+
console.log(`[build.mjs] 🚀 Build time: ${Date.now() - start}ms`);
63+
} catch (error) {
64+
console.error(`[build.mjs] ❌ Build failed:`, error);
65+
}
6466
})();

0 commit comments

Comments
 (0)