Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pre-bundle using esbuild #239

Merged
merged 1 commit into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
node_modules
dist
bundle
build
.DS_Store

# Yarn related files
Expand Down
13 changes: 12 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
]
},
"scripts": {
"build": "tsc",
"build": "node scripts/build.js",
"watch": "tsc --watch",
"lint": "eslint src",
"format": "prettier --write src && eslint --fix src",
Expand All @@ -39,6 +39,16 @@
},
"contributors": [],
"license": "Apache-2.0",
"nativeDependencies": {
"better-sqlite3": "*",
"@json2csv/plainjs": "*",
"@json2csv/transforms": "*",
"@napi-rs/clipboard": "*",
"@napi-rs/keyring": "*",
"@node-rs/argon2": "*",
"playwright-core": "*",
"node-mac-auth": "*"
},
"devDependencies": {
"@types/async": "^3.2.24",
"@types/better-sqlite3": "^7.6.9",
Expand All @@ -50,6 +60,7 @@
"@typescript-eslint/parser": "^7.7.0",
"@yao-pkg/pkg": "^5.11.5",
"chai": "^4.4.1",
"esbuild": "^0.21.2",
"eslint": "^8.57.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-import": "^2.29.1",
Expand Down
81 changes: 81 additions & 0 deletions scripts/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#!/usr/bin/env node

const os = require('os');
const fs = require('fs');
const path = require('path');
const process = require('process');
const childProcess = require('child_process');
const esbuild = require('esbuild');
const packageJSON = require('../package.json');

const platform = os.platform();

/* eslint-disable no-console */
async function main(argv = process.argv) {
argv = argv.slice(2);
const projectRoot = path.join(__dirname, '..');
const buildPath = path.join(projectRoot, 'build');
const distPath = path.join(projectRoot, 'dist');
const gitPath = process.env.GIT_DIR ?? path.join(projectRoot, '.git');
await fs.promises.rm(distPath, {
recursive: true,
force: true,
});
const buildArgs = ['-p', './tsconfig.build.json', ...argv];
console.error('Running tsc:');
console.error(['tsc', ...buildArgs].join(' '));
childProcess.execFileSync('tsc', buildArgs, {
stdio: ['inherit', 'inherit', 'inherit'],
windowsHide: true,
encoding: 'utf-8',
shell: platform === 'win32' ? true : false,
});
// This collects the build metadata and adds it to the build folder so that dynamic imports to it will resolve correctly.
let gitHead = process.env.COMMIT_HASH;
if (gitHead == null) {
gitHead = await fs.promises.readFile(path.join(gitPath, 'HEAD'), 'utf-8');
if (gitHead.startsWith('ref: ')) {
const refPath = gitHead.slice(5).trim();
gitHead = await fs.promises
.readFile(path.join(gitPath, refPath), 'utf-8')
.then((ref) => ref.trim());
}
}
const buildJSON = {
versionMetadata: {
version: packageJSON.version,
commitHash: gitHead,
},
};
console.error('Writing build metadata (build.json):');
console.error(buildJSON);
await fs.promises.writeFile(
path.join(buildPath, 'build.json'),
JSON.stringify(buildJSON, null, 2),
);
// This specifies import paths that is left as an external require
// This is kept to packages that have a native binding
const externalDependencies = Object.keys(packageJSON.nativeDependencies ?? {});
const esbuildOptions = {
entryPoints: [
path.join(buildPath, 'index.js'),
],
sourceRoot: buildPath,
bundle: true,
platform: 'node',
outdir: distPath,
external: externalDependencies,
treeShaking: true,
// External source map for debugging
sourcemap: true,
// Minify and keep the original names
minify: true,
keepNames: true,
};
console.error('Running esbuild:');
console.error(esbuildOptions);
await esbuild.build(esbuildOptions);
}
/* eslint-enable no-console */

void main();
9 changes: 9 additions & 0 deletions tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"rootDir": "./src",
"noEmit": false,
"stripInternal": true
},
"exclude": ["./documentation/**/*", "./scripts/**/*"]
}
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
// "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */
// "sourceMap": true, /* Create source map files for emitted JavaScript files. */
// "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. */
"outDir": "dist" /* Specify an output folder for all emitted files. */,
"outDir": "build" /* Specify an output folder for all emitted files. */,
// "removeComments": true, /* Disable emitting comments. */
// "noEmit": true, /* Disable emitting files from a compilation. */
// "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */
Expand Down
Loading