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

refactor(web): transformation of build-bundler into a common es-bundling command 📜 #10265

Merged
merged 6 commits into from
Jan 8, 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
126 changes: 126 additions & 0 deletions common/web/es-bundling/src/common-bundle.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
import fs from 'fs';
import esbuild from 'esbuild';
import { esmConfiguration, iifeConfiguration } from './configuration.mjs';
import { prepareTslibTreeshaking } from './tslibTreeshaking.mjs';

let FORMAT = 'iife';
let MINIFY = false;

let sourceFromArgs;
let destFromArgs;
let profilePath;

function doHelp(errCode?: number) {
console.log(`
Summary:
Uses esbuild to generate bundled-JS according to common, repo-wide KeymanWeb-oriented settings.

Usage:
common-bundle.mjs <input-file> --outDir <out-file> [options...]

Parameters:
<input-file>: Fully-bundled and compiled JS file to be wrapped.
<out-file>: Specifies the destination path for the wrapped output.

Options:
--help Shows this script's documentation
--format=<format> Sets the format type to use for the generated bundle. Should be
'iife' or 'esm'.

If not specified, 'iife' will be used.
--minify Enables minification.
--profile=<out-file> Generates an associated filesize profile at the specified path.
` );
process.exit(errCode || 0);
}

if(process.argv.length > 2) {
for(let i = 2; i < process.argv.length; i++) {
const arg = process.argv[i];

switch(arg) {
case '--help':
doHelp();
break;
case '--format': // bc TS uses this exact flag. esbuild... uses sourcemap (in the JS config)
let input = process.argv[++i];
switch(input) {
case 'iife':
case 'esm':
FORMAT = input;
break;
default:
console.error(`Invalid bundling format specified: ${input}. Must be 'iife' or 'esm'.`);
doHelp(1);
break;
}
break;
case '--minify':
MINIFY = true;
break;
case '--out':
destFromArgs = process.argv[++i];
break;
case '--profile':
profilePath = process.argv[++i];
break;
default:
if(!sourceFromArgs) {
sourceFromArgs = arg;
} else {
doHelp(1);
}
}
}
} else {
// Not enough args; display help + abort.
doHelp(1);
}
jahorton marked this conversation as resolved.
Show resolved Hide resolved

function fileSpecError(type: 'input' | 'output', file?: string) {
console.error(`${file ? 'Invalid' : 'No'} ${type} file ${file ? `(${file})`: ''} has been specified; aborting.`);
console.log();
doHelp(1);
}

function checkFileSpec(file: string, type: 'input' | 'output') {
if(!file) {
fileSpecError(type);
} else {
const ext = file.substring(file.lastIndexOf('.'));
switch(ext) {
case '.js':
case '.mjs':
case '.ts':
case '.mts':
break;
default:
fileSpecError(type, file);
}
}
}

checkFileSpec(sourceFromArgs, 'input');
checkFileSpec(destFromArgs, 'output');

const sourceFile = sourceFromArgs;
const destFile = destFromArgs;

// And now to start the actual bundling config + operation.
const baseConfig = FORMAT == 'iife' ? iifeConfiguration : esmConfiguration;

const config: esbuild.BuildOptions = {
...baseConfig,
entryPoints: [sourceFile],
outfile: destFile,
minify: MINIFY,
metafile: !!profilePath
};

await prepareTslibTreeshaking(config);
const results = await esbuild.build(config);

if(results.metafile) {
let filesizeProfile = await esbuild.analyzeMetafile(results.metafile, { verbose: true });
fs.writeFileSync(profilePath, filesizeProfile);
}
1 change: 1 addition & 0 deletions common/web/es-bundling/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"extends": "../../../tsconfig.base.json",
"compilerOptions": {
"allowSyntheticDefaultImports": true,
"baseUrl": "./",
"outDir": "build/",
"tsBuildInfoFile": "build/tsconfig.tsbuildinfo",
Expand Down
72 changes: 0 additions & 72 deletions common/web/lm-worker/build-bundler.js

This file was deleted.

8 changes: 6 additions & 2 deletions common/web/lm-worker/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ WORKER_OUTPUT_FILENAME=build/lib/worker-main.js
INTERMEDIATE=./build/intermediate
LIB=./build/lib

bundle_cmd="node ../es-bundling/build/common-bundle.mjs"

################################ Main script ################################

builder_describe \
Expand All @@ -38,14 +40,16 @@ builder_describe \

builder_describe_outputs \
configure /node_modules \
build /common/web/lm-worker/build/lib/worker-main.wrapped.min.js
build /common/web/lm-worker/$LIB/worker-main.wrapped.min.js

builder_parse "$@"

function do_build() {
# Build worker with tsc first
tsc -b $builder_verbose || builder_die "Could not build worker."
node build-bundler.js

$bundle_cmd build/obj/worker-main.js --out $INTERMEDIATE/worker-main.js
$bundle_cmd build/obj/worker-main.js --out $INTERMEDIATE/worker-main.min.js --minify --profile build/filesize-profile.log

EXT_FLAGS=
if builder_has_option --ci; then
Expand Down