import path from "node:path"; import webpack from "webpack"; /** * @param basePath - * @param silent - * @returns true if the bundle was created */ export async function bundle(basePath: string, silent: boolean): Promise { if (!silent) { console.log("Bundling started"); } let res: boolean; try { const options = (await import(path.join(basePath, "webpack.config.js"))) as { default: webpack.Configuration }; res = await new Promise((resolve, reject) => { webpack(options.default, (err, stats) => { if (err) { console.error(err.stack ?? err); reject(err); return; } if (!stats) { const err = new Error("No stats returned from webpack"); console.error(err); reject(err); return; } const info = stats.toJson(); if (stats.hasErrors()) { console.error(info.errors); reject(new Error(info.errors?.map(error => error.message).join("\n"))); } if (stats.hasWarnings()) { console.warn(info.warnings); } resolve(true); }); }); } catch (e) { console.error(e); res = false; } if (!silent) { console.log("Bundling done"); } return res; }