import * as esbuild from 'esbuild';

const srcDir = 'src/wled_controller/static';
const outDir = `${srcDir}/dist`;

const watch = process.argv.includes('--watch');

/** @type {esbuild.BuildOptions} */
const jsOpts = {
    entryPoints: [`${srcDir}/js/app.ts`],
    bundle: true,
    format: 'iife',
    outfile: `${outDir}/app.bundle.js`,
    minify: true,
    sourcemap: true,
    target: ['es2020'],
    logLevel: 'info',
};

/** @type {esbuild.BuildOptions} */
const cssOpts = {
    entryPoints: [`${srcDir}/css/all.css`],
    bundle: true,
    outdir: outDir,
    outbase: `${srcDir}/css`,
    minify: true,
    sourcemap: true,
    logLevel: 'info',
    loader: { '.woff2': 'file' },
    assetNames: '[name]',
    entryNames: 'app.bundle',
};

if (watch) {
    const jsCtx = await esbuild.context(jsOpts);
    const cssCtx = await esbuild.context(cssOpts);
    await jsCtx.watch();
    await cssCtx.watch();
    console.log('Watching for changes...');
} else {
    await esbuild.build(jsOpts);
    await esbuild.build(cssOpts);
}
