-
Notifications
You must be signed in to change notification settings - Fork 17
/
build.js
executable file
·128 lines (115 loc) · 4.13 KB
/
build.js
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/usr/bin/env node
import esbuild from "esbuild";
import copy from "esbuild-plugin-copy";
import { sassPlugin } from "esbuild-sass-plugin";
import fs from "fs";
import path from "path";
import { cockpitPoEsbuildPlugin } from "./pkg/lib/cockpit-po-plugin.js";
import { cockpitRsyncEsbuildPlugin } from "./pkg/lib/cockpit-rsync-plugin.js";
import { cleanPlugin } from "./pkg/lib/esbuild-cleanup-plugin.js";
import { cockpitCompressPlugin } from "./pkg/lib/esbuild-compress-plugin.js";
const production = process.env.NODE_ENV === "production";
const watchMode = process.env.ESBUILD_WATCH === "true" || false;
/* List of directories to use when resolving import statements */
const nodePaths = ["pkg/lib"];
const outdir = "dist";
// Obtain package name from package.json
const packageJson = JSON.parse(fs.readFileSync("package.json"));
const getTime = () => new Date().toTimeString()
.split(" ")[0];
// similar to fs.watch(), but recursively watches all subdirectories
function watchDirs (dir, onChange) {
const callback = (ev, dir, fname) => {
// only listen for "change" events, as renames are noisy
// ignore hidden files and the "4913" temporary file created by vim
const isHidden = /^\./.test(fname);
if (ev !== "change" || isHidden || fname === "4913") {
return;
}
onChange(path.join(dir, fname));
};
fs.watch(dir, {}, (ev, path) => callback(ev, dir, path));
// watch all subdirectories in dir
const d = fs.opendirSync(dir);
let dirent;
while ((dirent = d.readSync()) !== null) {
if (dirent.isDirectory()) {
watchDirs(path.join(dir, dirent.name), onChange);
}
}
d.closeSync();
}
const context = await esbuild.context({
bundle: true,
entryPoints: ["./src/index.js"],
external: [
"*.woff", "*.woff2", "*.jpg",
"@patternfly/react-core/src/components/assets/*.svg",
"@patternfly/react-core/src/demos/assets/*svg"
], // Allow external font files which live in ../../static/fonts
legalComments: "external", // Move all legal comments to a .LEGAL.txt file
loader: {
".js": "jsx",
".py": "text",
".svg": "dataurl",
},
minify: production,
nodePaths,
outdir,
plugins: [
cleanPlugin(),
// Esbuild will only copy assets that are explicitly imported and used
// in the code. This is a problem for index.html and manifest.json which are not imported
copy({
assets: [
{ from: ["./images/qr-code-feedback.svg"], to: ["./qr-code-feedback.svg"] },
{ from: ["./src/manifest.json"], to: ["./manifest.json"] },
{ from: ["./src/index.html"], to: ["./index.html"] },
]
}),
sassPlugin({
loadPaths: [...nodePaths, "node_modules"],
quietDeps: true,
async transform (source, resolveDir, path) {
if (path.includes("patternfly-4-cockpit.scss")) {
return source
.replace(/url.*patternfly-icons-fake-path.*;/g, "url(\"../base1/fonts/patternfly.woff\") format(\"woff\");")
.replace(/@font-face[^}]*patternfly-fonts-fake-path[^}]*}/g, "");
}
return source;
}
}),
cockpitPoEsbuildPlugin(),
cockpitCompressPlugin(),
cockpitRsyncEsbuildPlugin({ dest: packageJson.name }),
{
name: "notify-end",
setup (build) {
build.onEnd(() => console.log(`${getTime()}: Build finished`));
}
},
],
sourcemap: "linked",
target: ["es2020"],
});
try {
await context.rebuild();
} catch (e) {
if (!watchMode) {
process.exit(1);
}
// ignore errors in watch mode
}
if (watchMode) {
const onChange = async path => {
console.log("change detected:", path);
await context.cancel();
try {
await context.rebuild();
} catch (e) {} // ignore in watch mode
};
watchDirs("src", onChange);
// wait forever until Control-C
await new Promise(() => {});
}
context.dispose();