-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathbuild.js
More file actions
124 lines (110 loc) · 3.4 KB
/
build.js
File metadata and controls
124 lines (110 loc) · 3.4 KB
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
const { execSync } = require("child_process");
const fs = require("fs");
const fflate = require("fflate");
const { build } = require("@finos/perspective-esbuild-plugin/build");
const {
PerspectiveEsbuildPlugin,
} = require("@finos/perspective-esbuild-plugin");
const {
wasm_opt,
wasm_bindgen,
} = require("@finos/perspective-esbuild-plugin/rust_wasm");
const {
IgnoreCSSPlugin,
} = require("@finos/perspective-esbuild-plugin/ignore_css");
const {
IgnoreFontsPlugin,
} = require("@finos/perspective-esbuild-plugin/ignore_fonts");
const {
NodeModulesExternal,
} = require("@finos/perspective-esbuild-plugin/external");
const IS_DEBUG =
!!process.env.PSP_DEBUG || process.argv.indexOf("--debug") >= 0;
const BUILD = [
{
entryPoints: ["src/ts/perspective-viewer.ts"],
format: "esm",
plugins: [
IgnoreCSSPlugin(),
IgnoreFontsPlugin(),
NodeModulesExternal(),
],
external: ["*.wasm", "*.worker.js"],
outdir: "dist/esm",
},
{
entryPoints: ["src/ts/perspective-viewer.ts"],
plugins: [
IgnoreCSSPlugin(),
IgnoreFontsPlugin(),
PerspectiveEsbuildPlugin({
wasm: { inline: true },
worker: { inline: true },
}),
],
outfile: "dist/umd/perspective-viewer.js",
},
{
entryPoints: ["src/ts/migrate.ts"],
format: "cjs",
outfile: "dist/cjs/migrate.js",
},
{
entryPoints: ["src/ts/perspective-viewer.ts"],
format: "esm",
plugins: [
IgnoreCSSPlugin(),
IgnoreFontsPlugin(),
PerspectiveEsbuildPlugin(),
],
splitting: true,
outdir: "dist/cdn",
},
];
// This is dumb. `splitting` param for `esbuild` outputs a `__require`/
// `__exports`/`__esModule` polyfill and does not tree-shake it; this <1kb
// file blocks downloading of the wasm asset until after alot of JavaScript has
// parsed due to this multi-step download+eval. Luckily `esbuild` is quite fast
// enough to just run another build to inline this one file `chunk.js`.
const POSTBUILD = [
{
entryPoints: ["dist/cdn/perspective-viewer.js"],
format: "esm",
plugins: [NodeModulesExternal()],
external: ["*.wasm", "*.worker.js", "*.main.js"],
outdir: "dist/cdn",
allowOverwrite: true,
},
];
const INHERIT = {
stdio: "inherit",
stderr: "inherit",
};
async function compile_rust() {
const cargo_debug = IS_DEBUG ? "" : "--release";
execSync(`cargo build ${cargo_debug}`, INHERIT);
await wasm_bindgen("perspective", {
debug: IS_DEBUG,
version: "0.2.82",
targetdir: "build",
});
if (!IS_DEBUG) {
await wasm_opt("perspective");
}
// Compress wasm
const wasm = fs.readFileSync("dist/pkg/perspective_bg.wasm");
const compressed = fflate.compressSync(wasm, { level: 9 });
fs.writeFileSync("dist/pkg/perspective_bg.wasm", compressed);
}
async function build_all() {
// Rust
await compile_rust();
// JavaScript
execSync("yarn tsc --project tsconfig.json", INHERIT);
await Promise.all(BUILD.map(build)).catch(() => process.exit(1));
await Promise.all(POSTBUILD.map(build)).catch(() => process.exit(1));
// legacy compat
execSync("cpy target/themes/* dist/css");
execSync("cpy dist/css/* dist/umd");
}
build_all();