summaryrefslogtreecommitdiff
path: root/esbuild.config.js
blob: 4adb86a098a5c01f52141409bf5d67379d1d9234 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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',
    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: isProduction,
        sourcemap: true,
        target: 'es2020',
        outfile: paths.outJs,
        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(), buildCss()]);
    await copyAssets();
    await copyHtml();
}

build().catch((error) => {
    console.error(error);
    process.exit(1);
});