Skip to content

Commit 1445889

Browse files
author
Corentin Mors
authored
Pre-bundle using esbuild (#239)
This will allow usage of ESM and smaller builds.
1 parent 586ea01 commit 1445889

File tree

6 files changed

+432
-88
lines changed

6 files changed

+432
-88
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
node_modules
33
dist
44
bundle
5+
build
56
.DS_Store
67

78
# Yarn related files

package.json

+12-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
]
1919
},
2020
"scripts": {
21-
"build": "tsc",
21+
"build": "node scripts/build.js",
2222
"watch": "tsc --watch",
2323
"lint": "eslint src",
2424
"format": "prettier --write src && eslint --fix src",
@@ -39,6 +39,16 @@
3939
},
4040
"contributors": [],
4141
"license": "Apache-2.0",
42+
"nativeDependencies": {
43+
"better-sqlite3": "*",
44+
"@json2csv/plainjs": "*",
45+
"@json2csv/transforms": "*",
46+
"@napi-rs/clipboard": "*",
47+
"@napi-rs/keyring": "*",
48+
"@node-rs/argon2": "*",
49+
"playwright-core": "*",
50+
"node-mac-auth": "*"
51+
},
4252
"devDependencies": {
4353
"@types/async": "^3.2.24",
4454
"@types/better-sqlite3": "^7.6.9",
@@ -50,6 +60,7 @@
5060
"@typescript-eslint/parser": "^7.7.0",
5161
"@yao-pkg/pkg": "^5.11.5",
5262
"chai": "^4.4.1",
63+
"esbuild": "^0.21.2",
5364
"eslint": "^8.57.0",
5465
"eslint-config-prettier": "^9.1.0",
5566
"eslint-plugin-import": "^2.29.1",

scripts/build.js

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#!/usr/bin/env node
2+
3+
const os = require('os');
4+
const fs = require('fs');
5+
const path = require('path');
6+
const process = require('process');
7+
const childProcess = require('child_process');
8+
const esbuild = require('esbuild');
9+
const packageJSON = require('../package.json');
10+
11+
const platform = os.platform();
12+
13+
/* eslint-disable no-console */
14+
async function main(argv = process.argv) {
15+
argv = argv.slice(2);
16+
const projectRoot = path.join(__dirname, '..');
17+
const buildPath = path.join(projectRoot, 'build');
18+
const distPath = path.join(projectRoot, 'dist');
19+
const gitPath = process.env.GIT_DIR ?? path.join(projectRoot, '.git');
20+
await fs.promises.rm(distPath, {
21+
recursive: true,
22+
force: true,
23+
});
24+
const buildArgs = ['-p', './tsconfig.build.json', ...argv];
25+
console.error('Running tsc:');
26+
console.error(['tsc', ...buildArgs].join(' '));
27+
childProcess.execFileSync('tsc', buildArgs, {
28+
stdio: ['inherit', 'inherit', 'inherit'],
29+
windowsHide: true,
30+
encoding: 'utf-8',
31+
shell: platform === 'win32' ? true : false,
32+
});
33+
// This collects the build metadata and adds it to the build folder so that dynamic imports to it will resolve correctly.
34+
let gitHead = process.env.COMMIT_HASH;
35+
if (gitHead == null) {
36+
gitHead = await fs.promises.readFile(path.join(gitPath, 'HEAD'), 'utf-8');
37+
if (gitHead.startsWith('ref: ')) {
38+
const refPath = gitHead.slice(5).trim();
39+
gitHead = await fs.promises
40+
.readFile(path.join(gitPath, refPath), 'utf-8')
41+
.then((ref) => ref.trim());
42+
}
43+
}
44+
const buildJSON = {
45+
versionMetadata: {
46+
version: packageJSON.version,
47+
commitHash: gitHead,
48+
},
49+
};
50+
console.error('Writing build metadata (build.json):');
51+
console.error(buildJSON);
52+
await fs.promises.writeFile(
53+
path.join(buildPath, 'build.json'),
54+
JSON.stringify(buildJSON, null, 2),
55+
);
56+
// This specifies import paths that is left as an external require
57+
// This is kept to packages that have a native binding
58+
const externalDependencies = Object.keys(packageJSON.nativeDependencies ?? {});
59+
const esbuildOptions = {
60+
entryPoints: [
61+
path.join(buildPath, 'index.js'),
62+
],
63+
sourceRoot: buildPath,
64+
bundle: true,
65+
platform: 'node',
66+
outdir: distPath,
67+
external: externalDependencies,
68+
treeShaking: true,
69+
// External source map for debugging
70+
sourcemap: true,
71+
// Minify and keep the original names
72+
minify: true,
73+
keepNames: true,
74+
};
75+
console.error('Running esbuild:');
76+
console.error(esbuildOptions);
77+
await esbuild.build(esbuildOptions);
78+
}
79+
/* eslint-enable no-console */
80+
81+
void main();

tsconfig.build.json

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"extends": "./tsconfig.json",
3+
"compilerOptions": {
4+
"rootDir": "./src",
5+
"noEmit": false,
6+
"stripInternal": true
7+
},
8+
"exclude": ["./documentation/**/*", "./scripts/**/*"]
9+
}

tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
// "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */
4949
// "sourceMap": true, /* Create source map files for emitted JavaScript files. */
5050
// "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If `declaration` is true, also designates a file that bundles all .d.ts output. */
51-
"outDir": "dist" /* Specify an output folder for all emitted files. */,
51+
"outDir": "build" /* Specify an output folder for all emitted files. */,
5252
// "removeComments": true, /* Disable emitting comments. */
5353
// "noEmit": true, /* Disable emitting files from a compilation. */
5454
// "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */

0 commit comments

Comments
 (0)