feat: A flag (--save-metafiles) to save esbuild metafiles

- Useful for analysing bundle size
This commit is contained in:
Suraj Shetty 2023-06-23 09:45:25 +05:30
parent e4ff09ad6a
commit fe25fe1db1
3 changed files with 31 additions and 1 deletions

View file

@ -60,6 +60,11 @@ const argv = yargs
type: "boolean", type: "boolean",
description: "Run build command for apps", description: "Run build command for apps",
}) })
.option("save-metafiles", {
type: "boolean",
description:
"Saves esbuild metafiles for built assets. Useful for analyzing bundle size. More info: https://esbuild.github.io/api/#metafile",
})
.example("node esbuild --apps frappe,erpnext", "Run build only for frappe and erpnext") .example("node esbuild --apps frappe,erpnext", "Run build only for frappe and erpnext")
.example( .example(
"node esbuild --files frappe/website.bundle.js,frappe/desk.bundle.js", "node esbuild --files frappe/website.bundle.js,frappe/desk.bundle.js",
@ -401,6 +406,13 @@ async function write_assets_json(metafile) {
await fs.promises.writeFile(assets_json_path, JSON.stringify(new_assets_json, null, 4)); await fs.promises.writeFile(assets_json_path, JSON.stringify(new_assets_json, null, 4));
await update_assets_json_in_cache(); await update_assets_json_in_cache();
if (argv["save-metafiles"]) {
// use current timestamp in readable formate as a suffix for filename
let current_timestamp = new Date().getTime();
const metafile_name = `meta-${current_timestamp}.json`;
await fs.promises.writeFile(`${metafile_name}`, JSON.stringify(metafile));
log(`Saved metafile as ${metafile_name}`);
}
return { return {
new_assets_json, new_assets_json,
prev_assets_json, prev_assets_json,

View file

@ -229,6 +229,7 @@ def bundle(
verbose=False, verbose=False,
skip_frappe=False, skip_frappe=False,
files=None, files=None,
save_metafiles=False,
): ):
"""concat / minify js files""" """concat / minify js files"""
setup() setup()
@ -248,6 +249,9 @@ def bundle(
command += " --run-build-command" command += " --run-build-command"
if save_metafiles:
command += " --save-metafiles"
check_node_executable() check_node_executable()
frappe_app_path = frappe.get_app_path("frappe", "..") frappe_app_path = frappe.get_app_path("frappe", "..")
frappe.commands.popen(command, cwd=frappe_app_path, env=get_node_env(), raise_err=True) frappe.commands.popen(command, cwd=frappe_app_path, env=get_node_env(), raise_err=True)

View file

@ -31,6 +31,12 @@ EXTRA_ARGS_CTX = {"ignore_unknown_options": True, "allow_extra_args": True}
@click.option( @click.option(
"--force", is_flag=True, default=False, help="Force build assets instead of downloading available" "--force", is_flag=True, default=False, help="Force build assets instead of downloading available"
) )
@click.option(
"--save-metafiles",
is_flag=True,
default=False,
help="Saves esbuild metafiles for built assets. Useful for analyzing bundle size. More info: https://esbuild.github.io/api/#metafile",
)
def build( def build(
app=None, app=None,
apps=None, apps=None,
@ -38,6 +44,7 @@ def build(
production=False, production=False,
verbose=False, verbose=False,
force=False, force=False,
save_metafiles=False,
): ):
"Compile JS and CSS source files" "Compile JS and CSS source files"
from frappe.build import bundle, download_frappe_assets from frappe.build import bundle, download_frappe_assets
@ -62,7 +69,14 @@ def build(
if production: if production:
mode = "production" mode = "production"
bundle(mode, apps=apps, hard_link=hard_link, verbose=verbose, skip_frappe=skip_frappe) bundle(
mode,
apps=apps,
hard_link=hard_link,
verbose=verbose,
skip_frappe=skip_frappe,
save_metafiles=save_metafiles,
)
@click.command("watch") @click.command("watch")