/
build.mjs
135 lines (117 loc) · 3.38 KB
/
build.mjs
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
129
130
131
132
133
134
135
import esbuild from "esbuild";
import fs from "fs";
const outputDir = "dist";
const shouldClean = process.argv.includes("--clean");
if (shouldClean) {
// Clean up any files in the output directory, or make sure the output directory exists
if (fs.existsSync(outputDir)) {
await Promise.all(
fs
.readdirSync(outputDir)
.map((file) => fs.promises.unlink(`${outputDir}/${file}`))
);
} else {
fs.mkdirSync(outputDir);
}
}
let buildTargets;
const buildsArgIndex = process.argv.indexOf("--builds");
if (buildsArgIndex < 0) {
buildTargets = ["all"];
} else {
buildTargets = process.argv[buildsArgIndex + 1].split(",");
}
const shouldWatch = process.argv.includes("--watch");
const MinifyCSSPlugin = {
name: "css",
setup(build) {
build.onLoad({ filter: /\.css$/ }, async ({ path }) => {
const file = await fs.promises.readFile(path);
const css = await esbuild.transform(file, {
loader: "css",
minify: true,
});
return { loader: "text", contents: css.code };
});
},
};
const MinifyHTMLPlugin = {
name: "html",
setup(build) {
build.onLoad({ filter: /\.html$/ }, async ({ path }) => {
const file = await fs.promises.readFile(path, "utf-8");
// Remove unnecessary whitespace (spaces, tabs, newlines) from the HTML
const minifiedHTML = file
// Replace all instances of 2+ spaces in a row with a single space
.replace(/[ ]{2,}/g, " ")
// Remove all tabs, newlines, and carriage returns
.replace(/[\t\r\n\f]/g, "");
return { loader: "text", contents: minifiedHTML };
});
},
};
const sharedOptions = {
entryPoints: ["src/hover-video-player.ts"],
plugins: [MinifyCSSPlugin, MinifyHTMLPlugin],
minify: true,
bundle: true,
sourcemap: true,
// Mangle all internal private properties, which start with an underscore
mangleProps: /^_.+$/,
logLevel: "info",
};
const builds = [];
buildTargets.forEach((buildTarget) => {
if (buildTarget === "esm" || buildTarget === "all") {
const esmOptions = {
...sharedOptions,
outfile: `${outputDir}/index.mjs`,
format: "esm",
};
builds.push(
shouldWatch
? esbuild
.context(esmOptions)
.then((ctx) => ctx.watch().then(() => ctx.dispose()))
: esbuild.build(esmOptions)
);
}
if (buildTarget === "cjs" || buildTarget === "all") {
const cjsOptions = {
...sharedOptions,
outfile: `${outputDir}/index.cjs`,
format: "cjs",
};
builds.push(
shouldWatch
? esbuild
.context(cjsOptions)
.then((ctx) => ctx.watch().then(() => ctx.dispose()))
: esbuild.build(cjsOptions)
);
}
if (buildTarget === "iife" || buildTarget === "all") {
const iifeOptions = {
...sharedOptions,
outfile: `${outputDir}/index.js`,
format: "iife",
};
builds.push(
shouldWatch
? esbuild
.context(iifeOptions)
.then((ctx) => ctx.watch().then(() => ctx.dispose()))
: esbuild.build(iifeOptions)
);
}
});
if (builds.length === 0) {
console.error("No valid builds specified for the --builds arg.");
process.exit(1);
}
await Promise.all(builds);
// Create a .webc file which just imports the built client-side script
await fs.writeFileSync(
`${outputDir}/hover-video-player.webc`,
`<script src="./index.client.js"></script>`
);