-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
67 lines (57 loc) · 1.7 KB
/
script.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
const fs = require("fs/promises");
const path = require("path");
const { build: buildVite, createServer } = require("vite");
const config = require("./vite.config");
// Constants -------------------------------------------------------------------
const [, , script] = process.argv;
const players = ["plyr", "videojs"];
const defaultPlayer = "videojs";
// Commands --------------------------------------------------------------------
async function build() {
await emptyOutDir("./dist");
await Promise.all(
players.map((player) => buildVite(createPlayerConfig(player)))
);
}
async function dev() {
const config = createPlayerConfig(defaultPlayer);
// Expose dev server on host network.
const server = await createServer({
...config,
server: {
...config.server,
host: true,
},
});
await server.listen(3000);
}
// Helpers ---------------------------------------------------------------------
function createPlayerConfig(player) {
const name = "wd-player" + (player === defaultPlayer ? "" : `-${player}`);
return {
...config,
build: {
emptyOutDir: false,
...config.build,
lib: {
...config.build.lib,
name,
entry: path.resolve(__dirname, `lib/${player}/index.ts`),
fileName: (format) => `${name}.${format}.js`,
},
},
};
}
async function emptyOutDir(dir) {
const files = await fs.readdir(dir);
return Promise.all(files.map((file) => fs.unlink(path.join(dir, file))));
}
// Main ------------------------------------------------------------------------
if (script === "build") {
build();
} else if (script === "dev") {
dev();
} else {
console.error("Invalid argument passed to script.js");
process.exit(1);
}