Plain is headed towards 1.0! Subscribe for development updates →

plain.esbuild

Build JavaScript files with esbuild.

gitignore

**/assets/**/*.esbuilt.*
 1import os
 2import subprocess
 3
 4
 5def esbuild(input_path, output_path, *, minify=True):
 6    # Ensure the directory for the output file exists
 7    output_dir = os.path.dirname(output_path)
 8    if not os.path.exists(output_dir):
 9        os.makedirs(output_dir)
10
11    print(f"Building {os.path.relpath(input_path)} to {os.path.relpath(output_path)}")
12
13    cmd = [
14        "npx",
15        "esbuild",
16        input_path,
17        "--bundle",
18        f"--outfile={output_path}",
19        "--sourcemap",
20        "--platform=browser",
21    ]
22
23    if minify:
24        cmd.append("--minify")
25
26    result = subprocess.run(cmd)
27
28    return result.returncode == 0
29
30
31def get_esbuilt_path(input_path):
32    # Rename .esbuild. to .esbuilt. in the output filename
33    base_name = os.path.basename(input_path)
34    base_name = base_name.replace(".esbuild.", ".esbuilt.")
35    return os.path.join(os.path.dirname(input_path), base_name)