summaryrefslogtreecommitdiff
path: root/esbuild.config.js
diff options
context:
space:
mode:
Diffstat (limited to 'esbuild.config.js')
-rw-r--r--esbuild.config.js75
1 files changed, 46 insertions, 29 deletions
diff --git a/esbuild.config.js b/esbuild.config.js
index cb83e51..3d01486 100644
--- a/esbuild.config.js
+++ b/esbuild.config.js
@@ -1,24 +1,43 @@
import esbuild from 'esbuild';
import fs from 'fs-extra';
-const production = process.env.NODE_ENV === 'production';
+const isProduction = process.env.NODE_ENV === 'production';
-async function buildJS() {
- await esbuild.build({
- entryPoints: ['src/ts/script.ts'],
+const paths = {
+ distDir: 'dist',
+ assetsDir: 'src/assets',
+ jsEntry: 'src/ts/script.ts',
+ cssEntry: 'src/css/style.css',
+ componentsHtml: 'src/index.html',
+ demoHtml: '.demo/index.html',
+ outJs: 'dist/bundle.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: production,
+ minify: isProduction,
sourcemap: true,
target: 'es2020',
- outfile: 'dist/bundle.js',
+ outfile: paths.outJs,
+ logLevel: 'info',
});
}
-async function buildCSS() {
- await esbuild.build({
- entryPoints: ['src/css/style.css'],
+function buildCss() {
+ return esbuild.build({
+ entryPoints: [paths.cssEntry],
bundle: true,
- minify: production,
+ minify: isProduction,
sourcemap: true,
loader: {
'.css': 'css',
@@ -26,34 +45,32 @@ async function buildCSS() {
'.png': 'file',
'.svg': 'file',
},
- outfile: 'dist/bundle.css',
+ outfile: paths.outCss,
+ logLevel: 'info',
});
}
async function copyAssets() {
- await fs.copy('src/assets', 'dist', {
- overwrite: true,
- });
+ // Copy into dist root so URLs like /img/* keep working.
+ await fs.copy(paths.assetsDir, paths.distDir, { overwrite: true });
}
-async function processHTML() {
- let html = await fs.readFile('src/index.html', 'utf8');
- await fs.writeFile('dist/index.html', html);
-}
+async function copyHtml() {
+ await fs.copyFile(paths.componentsHtml, paths.outComponentsHtml);
-async function clean() {
- await fs.remove('dist');
- await fs.ensureDir('dist');
+ // Optional demo page.
+ if (await fs.pathExists(paths.demoHtml)) {
+ await fs.copyFile(paths.demoHtml, paths.outDemoHtml);
+ }
}
async function build() {
- try {
- await clean();
- await Promise.all([buildJS(), buildCSS(), copyAssets()]);
- await processHTML();
- } catch (err) {
- process.exit(1);
- }
+ await cleanDist();
+ await Promise.all([buildJavaScript(), buildCss(), copyAssets()]);
+ await copyHtml();
}
-build();
+build().catch((error) => {
+ console.error(error);
+ process.exit(1);
+});