import esbuild from 'esbuild'; import fs from 'fs-extra'; const isProduction = process.env.NODE_ENV === 'production'; const paths = { distDir: 'dist', assetsDir: 'src/assets', jsEntry: 'src/ts/script.ts', editorEntry: 'src/ts/editor-standalone.ts', cssEntry: 'src/css/style.css', componentsHtml: 'src/index.html', demoHtml: '.demo/index.html', outJs: 'dist/bundle.js', outEditor: 'dist/adelie-editor.js', outCss: 'dist/bundle.css', outComponentsHtml: 'dist/index.html', outDemoHtml: 'dist/demo.html', }; async function cleanDist() { await fs.remove(paths.distDir); await fs.ensureDir(paths.distDir); } function buildJavaScript() { return esbuild.build({ entryPoints: [paths.jsEntry], bundle: true, minify: isProduction, sourcemap: true, target: 'es2020', outfile: paths.outJs, logLevel: 'info', }); } function buildEditor() { return esbuild.build({ entryPoints: [paths.editorEntry], bundle: true, minify: isProduction, sourcemap: true, target: 'es2020', format: 'iife', globalName: 'adelieEditor', outfile: paths.outEditor, logLevel: 'info', }); } function buildCss() { return esbuild.build({ entryPoints: [paths.cssEntry], bundle: true, minify: isProduction, sourcemap: true, loader: { '.css': 'css', '.woff2': 'file', '.png': 'file', '.svg': 'file', }, outfile: paths.outCss, logLevel: 'info', }); } async function copyAssets() { // Copy into dist root so URLs like /img/* keep working. await fs.copy(paths.assetsDir, paths.distDir, { overwrite: true }); } async function copyHtml() { await fs.copyFile(paths.componentsHtml, paths.outComponentsHtml); // Optional demo page. if (await fs.pathExists(paths.demoHtml)) { await fs.copyFile(paths.demoHtml, paths.outDemoHtml); } } async function build() { await cleanDist(); await Promise.all([buildJavaScript(), buildEditor(), buildCss()]); await copyAssets(); await copyHtml(); } build().catch((error) => { console.error(error); process.exit(1); });