2023-09-05 09:25:47 +02:00
|
|
|
#!/usr/bin/env -S npx nodejsscript
|
2023-10-19 15:01:54 +02:00
|
|
|
import { bundle as bundleDTS } from "dts-bundler";
|
2024-05-22 21:43:49 +02:00
|
|
|
const files= [ "index", "index-with-signals" ];
|
2023-09-05 09:25:47 +02:00
|
|
|
const filesOut= (file, mark= "esm")=> "dist/"+file.replace("index", mark);
|
2023-11-07 10:20:34 +01:00
|
|
|
const css= echo.css`
|
|
|
|
.info{ color: gray; }
|
|
|
|
`;
|
2023-09-05 09:25:47 +02:00
|
|
|
|
2023-10-19 14:23:10 +02:00
|
|
|
$.api("", true)
|
2024-11-22 10:20:59 +01:00
|
|
|
.option("--minify", "Level of minification [ no, full, partial (default) ]")
|
2024-05-22 21:43:49 +02:00
|
|
|
.action(async function main({ minify= "partial" }){
|
2023-10-19 15:01:54 +02:00
|
|
|
for(const file_root of files){
|
|
|
|
const file= file_root+".js";
|
2023-11-07 10:20:34 +01:00
|
|
|
echo("Processing: "+ file);
|
2023-10-19 14:23:10 +02:00
|
|
|
const out= filesOut(file);
|
2023-11-07 10:20:34 +01:00
|
|
|
const esbuild_output= s.$().run([
|
2023-10-19 14:23:10 +02:00
|
|
|
"npx esbuild '::file::'",
|
|
|
|
"--platform=neutral",
|
|
|
|
"--bundle",
|
2024-11-22 10:20:59 +01:00
|
|
|
minifyOption(minify),
|
2023-10-19 14:23:10 +02:00
|
|
|
"--legal-comments=inline",
|
|
|
|
"--packages=external",
|
|
|
|
"--outfile='::out::'"
|
2024-11-22 10:20:59 +01:00
|
|
|
].filter(Boolean).join(" "), { file, out });
|
2023-11-07 10:20:34 +01:00
|
|
|
if(esbuild_output.code)
|
|
|
|
return $.exit(esbuild_output.code, echo(esbuild_output.stderr));
|
|
|
|
echoVariant(esbuild_output.stderr.split("\n")[1].trim()+ " (esbuild)");
|
2023-10-19 14:23:10 +02:00
|
|
|
pipe(
|
|
|
|
f=> f.replace(/^ +/gm, m=> "\t".repeat(m.length/2)),
|
|
|
|
f=> s.echo(f).to(out)
|
|
|
|
)(s.cat(out));
|
2023-11-07 09:53:10 +01:00
|
|
|
|
2023-10-19 15:01:54 +02:00
|
|
|
const file_dts= file_root+".d.ts";
|
2023-10-19 15:28:44 +02:00
|
|
|
const file_dts_out= filesOut(file_dts);
|
2023-11-07 10:20:34 +01:00
|
|
|
echoVariant(file_dts_out);
|
2023-10-19 15:28:44 +02:00
|
|
|
s.echo(bundleDTS(file_dts)).to(file_dts_out);
|
2023-11-07 09:53:10 +01:00
|
|
|
|
|
|
|
await toDDE(out, file_root);
|
2023-10-19 14:23:10 +02:00
|
|
|
}
|
|
|
|
$.exit(0);
|
2023-09-05 09:25:47 +02:00
|
|
|
|
2023-11-07 09:53:10 +01:00
|
|
|
async function toDDE(file, file_root){
|
2023-10-19 14:23:10 +02:00
|
|
|
const name= "dde";
|
2023-11-07 09:53:10 +01:00
|
|
|
const out= filesOut(file_root+".js", name);
|
2023-11-07 10:20:34 +01:00
|
|
|
echoVariant(`${out} (${file} → globalThis.${name})`)
|
2023-10-19 14:23:10 +02:00
|
|
|
|
|
|
|
let content= s.cat(file).toString().split(/export ?{/);
|
|
|
|
content.splice(1, 0, `\nglobalThis.${name}= {`);
|
|
|
|
content[2]= content[2].replace(/,(?!\n)/g, ",\n").replace(/(?<!\n)}/, "\n}").replace(/^(\t*)(.*) as ([^,\n]*)(,?)$/mg, "$1$3: $2$4");
|
|
|
|
s.echo([
|
|
|
|
`//deka-dom-el library is available via global namespace \`${name}\``,
|
|
|
|
"(()=> {",
|
|
|
|
content.join(""),
|
|
|
|
"})();"
|
|
|
|
].join("\n")).to(out);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.parse();
|
2023-11-07 10:20:34 +01:00
|
|
|
|
2024-11-22 10:20:59 +01:00
|
|
|
/** @param {"no"|"full"|"partial"} level */
|
|
|
|
function minifyOption(level= "partial"){
|
|
|
|
if("no"===level) return undefined;
|
|
|
|
if("full"===level) return "--minify";
|
|
|
|
return "--minify-syntax --minify-identifiers";
|
|
|
|
}
|
2023-11-07 10:20:34 +01:00
|
|
|
function echoVariant(name){
|
|
|
|
return echo("%c✓ "+name, css.info+css);
|
|
|
|
}
|