From d54b6a65ef290b53cebb49125a06ac581eceec6c Mon Sep 17 00:00:00 2001 From: Jan Andrle Date: Sat, 15 Mar 2025 11:45:39 +0100 Subject: [PATCH] :zap: bs/build --- bs/dev/.build.js | 76 +++++++++++++++++++++--------------------------- 1 file changed, 33 insertions(+), 43 deletions(-) diff --git a/bs/dev/.build.js b/bs/dev/.build.js index ac2f13d..63b3087 100644 --- a/bs/dev/.build.js +++ b/bs/dev/.build.js @@ -1,4 +1,5 @@ #!/usr/bin/env -S npx nodejsscript +import { analyzeMetafileSync, buildSync as esbuildSync } from "esbuild"; const css= echo.css` .info{ color: gray; } `; @@ -8,8 +9,7 @@ export function build({ files, filesOut, minify= "partial", iife= true, types= t const file= file_root+".js"; echo(`Processing ${file} (minified: ${minify})`); const out= filesOut(file); - const esbuild_output= buildEsbuild({ file, out, minify }); - echoVariant(esbuild_output.stderr.split("\n")[1].trim()); + esbuild({ file, out, minify }); if(types){ const file_dts= file_root+".d.ts"; @@ -31,14 +31,13 @@ export function build({ files, filesOut, minify= "partial", iife= true, types= t const name= "DDE"; const out= filesOut(file_root+".js", fileMark); - const params= [ - "--format=iife", - "--global-name="+name, - ]; - const dde_output= buildEsbuild({ file, out, minify, params }); - echoVariant(`${out} (${name})`) + const params= { + format: "iife", + globalName: name + }; + esbuild({ file, out, minify, params }); - if(!types) return dde_output; + if(!types) return; const file_dts= file_root+".d.ts"; const file_dts_out= filesOut(file_dts, fileMark); echoVariant(file_dts_out, true); @@ -48,8 +47,6 @@ export function build({ files, filesOut, minify= "partial", iife= true, types= t entry: file_dts, }) echoVariant(file_dts_out); - - return dde_output; } } export function buildDts({ bundle, entry, name }){ @@ -64,46 +61,39 @@ export function buildDts({ bundle, entry, name }){ ].filter(Boolean).join(" "), { out, entry }); return dts_b_g_output; } -class ErrorEsbuild extends Error{ - constructor({ code, stderr }){ - super(stderr); - this.code= code; - this.stderr= stderr; - } -} -function buildEsbuild({ file, out, minify= "partial", params= [] }){ - try { - return esbuild({ file, out, minify, params }); - } catch(e){ - if(e instanceof ErrorEsbuild) - return $.exit(e.code, echo(e.stderr)); - throw e; - } -} -export function esbuild({ file, out, minify= "partial", params= [] }){ - const esbuild_output= s.$().run([ - "npx esbuild '::file::'", - "--platform=neutral", - "--bundle", - minifyOption(minify), - "--legal-comments=inline", - "--packages=external", - ...params, - "--outfile='::out::'" - ].filter(Boolean).join(" "), { file, out }); - if(esbuild_output.code) - throw new ErrorEsbuild(esbuild_output); +export function esbuild({ file, out, minify= "partial", params= {} }){ + const esbuild_output= esbuildSync({ + entryPoints: [file], + outfile: out, + platform: "neutral", + bundle: true, + legalComments: "inline", + packages: "external", + metafile: true, + ...minifyOption(minify), + ...params + }); pipe( f=> f.replace(/^ +/gm, m=> "\t".repeat(m.length/2)), f=> s.echo(f).to(out) )(s.cat(out)); + + echoVariant(metaToLineStatus(esbuild_output.metafile, out)); return esbuild_output; } /** @param {"no"|"full"|"partial"} level */ function minifyOption(level= "partial"){ - if("no"===level) return undefined; - if("full"===level) return "--minify"; - return "--minify-syntax --minify-identifiers"; + if("no"===level) return { minify: false }; + if("full"===level) return { minify: true }; + return { minifySyntax: true, minifyIdentifiers: true }; +} +function metaToLineStatus(meta, file){ + const status= meta.outputs[file]; + if(!status) return `? ${file}: unknown`; + const { bytes }= status; + const kbytes= bytes/1024; + const kbytesR= kbytes.toFixed(2); + return `${file}: ${kbytesR} KiB`; } function echoVariant(name, todo= false){ if(todo) return echo.use("-R", "~ "+name);