-
Notifications
You must be signed in to change notification settings - Fork 34
/
build.ts
231 lines (211 loc) · 6.23 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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
import { build as esbuild, BuildOptions } from "esbuild";
import * as util from "util";
import chalk from "chalk";
import { promises as fs } from "fs";
import { watch } from "chokidar";
import debounce from "debounce";
import copy0 from "copy";
import child_process0 from "child_process";
import sass0 from "sass";
import { rimraf } from "rimraf";
const copy = util.promisify(copy0);
const exec = util.promisify(child_process0.exec);
const sass = util.promisify(sass0.render);
const isWatchEnabled = process.argv[2] === "-w";
// for now, we bundle for production whenever we aren't in watch mode
const isProduction = !isWatchEnabled;
/**
* Generates TS Declarations using tsc. Is pretty slow : /.
*/
async function generateDts() {
try {
return await exec(
`tsc --declaration --emitDeclarationOnly --declarationDir dist/types`
);
} catch (e) {
return Promise.reject(e.stdout);
}
}
/**
* Builds Typescript (or JS) using ESBuild. Super fast and easy.
*/
async function buildTs(
options: BuildOptions,
successMsg: string,
filename: string
) {
const config: BuildOptions = {
bundle: true,
// what browsers we want to support, this is basically all >1% usage
target: ["chrome89", "firefox88", "safari14", "edge90"],
sourcemap: true,
// we include some node.js polyfills
inject: ["./polyfills.js"],
define: {
// note these need to be double quoted if we want to define string constants
"process.env.NODE_ENV": isProduction ? "'production'" : "'development'",
// the Node.js util polyfill uses "global" instead of "window" annoyingly
global: "globalThis",
},
tsconfig: "tsconfig.json",
...options,
};
try {
const r = await esbuild(config);
logBundled(successMsg, filename);
return r;
} catch (e) {
return Promise.reject(e.stdout);
}
}
/**
* Compiles SASS to CSS and writes it to the filesystem
*/
async function compileCss(input: string, filename: string) {
const options = {
file: input,
sourceMap: true,
outFile: `dist/${filename}.css`,
bundle: false,
outputStyle: "compressed",
};
try {
const result = await sass(options);
const fullPath = `dist/${filename}`;
const p1 = fs.writeFile(`${fullPath}.css`, result.css);
const p2 = fs.writeFile(`${fullPath}.css.map`, result.map);
await Promise.all([p1, p2]);
logBundled("Compiled SASS", `${fullPath}.css`);
} catch (e) {
err(`CSS Error (${input})`, e);
}
}
async function copyCssInjectables() {
try {
await copy("injectables/**/*.css", "dist/injectables");
logBundled("Copied CSS injectables", "dist/injectables/**/*.css");
} catch (e) {
err("CSS Copy Error: ", e);
}
}
async function copyJsInjectables() {
try {
await copy("injectables/**/*.js", "dist/injectables");
logBundled("Copied JS injectables", "dist/injectables/**/*.js");
} catch (e) {
err("CSS Copy Error: ", e);
}
}
/**
* Build pipeline:
* - clean the build folder
* - build iife version (for: <script href="https://../d2reader.js" />)
* - build esm version (for: import D2Reader from "@d-i-t-a/reader")
* - build SASS
* - generate TS declarations
* - Build iife version of injectables to dist
* - copy injectables css to dist
*
* Do all of this in parallel and wait for it all to finish.
* Optionally watch for changes!
*/
async function buildAll() {
await rimraf("dist/");
await fs.mkdir("dist");
console.log("🧹 Cleaned output folder -", chalk.blue("dist/"));
// build the main entrypoint as an IIFE module for use in a
// <script> tag. This is built at dist/reader.js for backwards
// compatibility
const p1 = buildTs(
{
format: "iife",
entryPoints: ["src/index.ts"],
globalName: "D2Reader",
outfile: "dist/reader.js",
minify: isProduction,
},
"Compiled IIFE (for <script> tags)",
"dist/reader.js"
);
// build the main entrypoint as an ES Module.
// This one doesn't need to be minified because it will
// be rebundled by the consumer's bundler
const p2 = buildTs(
{
format: "esm",
entryPoints: ["src/index.ts"],
outdir: "dist/esm",
minify: false,
},
"Compiled ESM (for 'import D2Reader' uses)",
"dist/esm/index.js"
);
// generate type declarations
const p3 = generateDts()
.then(() => logBundled("Generated TS Declarations", "dist/index.d.ts"))
.catch((e) => err("TS Error", e));
// compile the injectables separately with their own tsconfig
const p4 = buildTs(
{
format: "iife",
entryPoints: ["injectables/click/click.ts"],
outbase: ".",
tsconfig: "injectables/tsconfig.json",
outdir: "dist",
},
"Compiled injectables",
"dist/injectables/"
);
// copy over the css and js injectables
const p5 = copyCssInjectables();
const p6 = copyJsInjectables();
// compile sass files into reader.css and material.css
const p7 = compileCss("src/styles/sass/reader.scss", "reader");
// wait for everything to finish running in parallel
await Promise.all([p1, p2, p3, p4, p5, p6, p7]);
console.log("🔥 Build finished.");
}
// debounce the build command so that it only ever runs once every 100ms
// in watch mode
const debouncedBuildAll = debounce(buildAll, 1000);
// starts chokidar to watch the directory for changes
async function startWatcher() {
const ignored = [
"parcel-dist",
".parcel-cache",
".git",
"node_modules",
"dist",
"examples",
"viewer",
];
const watchPaths = ["."];
console.log("👀 Watching for changes...");
const watcher = watch(watchPaths, {
ignoreInitial: true,
ignorePermissionErrors: true,
ignored,
});
watcher.on("all", async (type: string, file: string) => {
console.log(`\n🕝 Change detected: ${type} ${file}`);
debouncedBuildAll();
});
}
/**
* Some logging utils
*/
const log = (msg: string, file?: string) => console.log(msg, chalk.blue(file));
const err = (title: string, e: string) =>
console.error(chalk.red(`❌ ${title}:`), e);
const logBundled = (msg: string, file: string) => log(`📦 ${msg} -`, file);
/**
* The main entrypoint for the script
*/
console.log(
`🌪 Building D2Reader${isWatchEnabled ? " in watch mode..." : "..."}`
);
buildAll().then(() => {
if (isWatchEnabled) {
startWatcher();
}
});