forked from manzt/quak
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.ts
78 lines (72 loc) · 2.21 KB
/
build.ts
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
/**
* @module
*
* This module contains a custom build and development script for the
* TypeScript source code in `quak`.
*
* @example
* ```sh
* $ deno run -A scripts/build.ts
* $ deno run -A scripts/build.ts --watch
* $ deno run -A scripts/build.ts --bundle
* ```
*
* It has two build modes:
*
* 1.) Unbundled (default) - Resolves the importmap in `deno.json`
* to external URLs (e.g., `d3` -> `https://esm.sh/d3@7`). This
* means the resulting bundle is much smaller and doesn't require
* downloading third-party dependencies locally.
*
* 2.) Bundled - Resolves the importmap in `deno.json` using Deno
* to download and install dependencies.
*
* For now, the former is preferred because it is a much smaller and
* simple for esbuild. If there is a compelling "offline" usecase, we
* can consider the latter as a default.
*/
/// <reference lib="deno.ns" />
import * as esbuild from "npm:[email protected]";
import { denoPlugins } from "jsr:@luca/esbuild-deno-loader@^0.10.3";
import { mapImports } from "./npm-specifier-to-cdn-url.mjs";
let root = new URL("..", import.meta.url);
// Hack: use importmap to make esbuild aliases.
// This relies on import-map resolving to HTTP urls.
let denoJson = await Deno
.readTextFile(new URL("deno.json", root))
.then(JSON.parse);
let options: esbuild.BuildOptions = {
alias: mapImports(denoJson.imports),
entryPoints: ["./lib/widget.ts"],
outfile: "./src/quak/widget.js",
bundle: true,
format: "esm",
sourcemap: "inline",
loader: { ".css": "text" },
logLevel: "info",
};
if (Deno.args.includes("--watch")) {
let ctx = await esbuild.context(options);
await ctx.watch();
} else {
if (Deno.args.includes("--bundle")) {
// Remove the importmap aliases and defer to Deno
// loader to download and bundle deps locally
delete options.alias;
delete options.sourcemap;
options.minify = true;
options.plugins = [...denoPlugins({
importMapURL: new URL("deno.json", root).href,
})];
}
await esbuild.build(options);
await Deno.copyFile(
new URL("src/quak/widget.js", root),
new URL("cli/src/static/widget.js", root),
);
console.log(
`\n Copied src/quak/%cwidget.js to %c${"cli/src/static/widget.js"}`,
"font-weight: bold",
"color: cyan",
);
}