-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbuild.ts
137 lines (121 loc) · 3.77 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
import fs from "node:fs";
import * as sass from "sass";
import esbuild from "esbuild";
import child_process from "child_process";
import AdmZip from "adm-zip";
const production = process.argv.includes("--production");
// TypeScript fix for JSC (Safari/WebKit) memory leak
// Refer to this for more info: https://github.com/microsoft/TypeScript/issues/58137
// Remove if ever fixed
const codeToLookup = "program = createProgram(options);";
const codeToAdd = "options.oldProgram = undefined;";
const tsFilePath = "node_modules/typescript/lib/typescript.js";
const tsFileContent = fs.readFileSync(tsFilePath, { encoding: "utf-8" });
const re = new RegExp(
`${codeToLookup.replace(/(\(|\))/g, (c) => (c === "(" ? "\\(" : "\\)"))}(${codeToAdd})*`
);
const textBlockToUpdate = tsFileContent.match(re);
if (textBlockToUpdate) {
if (!textBlockToUpdate[0].endsWith(codeToAdd)) {
fs.writeFileSync(
tsFilePath,
tsFileContent.replace(re, codeToLookup + codeToAdd)
);
}
} else {
throw "Could not find typescript code block to patch.";
}
// END fix
const outDir = "out";
const outDirEditor = `${outDir}/editor`;
const outTsLib = `${outDirEditor}/tsLib`;
if (fs.existsSync(outDir)) {
fs.rmSync(outDir, { recursive: true });
}
const toBuild = [
["editor/index.ts", "index"],
["editor/typescript/worker.ts", "worker-ts"]
];
for (const [input, output] of toBuild) {
esbuild.buildSync({
entryPoints: [
{
in: input,
out: output
}
],
bundle: true,
format: "esm",
outdir: outDirEditor,
sourcemap: production ? false : "external",
splitting: false,
minify: production
});
}
fs.cpSync("editor/index.html", `${outDirEditor}/index.html`);
fs.cpSync("editor/assets", `${outDirEditor}/assets`, {
recursive: true
});
async function processScss(entryPoint: string, out: string) {
const { css } = await sass.compileAsync(entryPoint, {
style: production ? "compressed" : "expanded"
});
await fs.promises.writeFile(out, css);
}
await processScss("editor/index.scss", `${outDirEditor}/index.css`);
await processScss(
"editor/style/globals/scrollbars.scss",
`${outDirEditor}/scrollbars.css`
);
fs.cpSync("editor/icons", `${outDirEditor}/icons`, {
recursive: true
});
const sampleDemoDir = "editor-sample-demo";
if (fs.existsSync(sampleDemoDir)) {
const zip = new AdmZip();
zip.addLocalFolder(sampleDemoDir, "", (file) => !file.startsWith(".git"));
zip.writeZip(`${outDirEditor}/Demo.zip`);
}
fs.cpSync("node_modules/typescript/lib", outTsLib, {
recursive: true
});
fs.cpSync("lib/fullstacked.d.ts", outTsLib + "/fullstacked.d.ts", {
recursive: true
});
fs.cpSync("lib", outDirEditor + "/lib", {
recursive: true,
filter: (s) => !s.endsWith(".scss")
});
await processScss(
"lib/components/snackbar.scss",
`${outDirEditor}/lib/components/snackbar.css`
);
const { version } = JSON.parse(
fs.readFileSync("package.json", { encoding: "utf-8" })
);
const branch = child_process
.execSync("git rev-parse --abbrev-ref HEAD")
.toString()
.trim();
const commit = child_process.execSync("git rev-parse HEAD").toString().trim();
const commitNumber = child_process
.execSync(`git rev-list --count ${branch}`)
.toString()
.trim();
fs.writeFileSync(
`${outDirEditor}/version.json`,
JSON.stringify({
version,
branch,
commit,
commitNumber
})
);
if (!process.argv.includes("--no-zip")) {
const outZipDir = `${outDir}/zip`;
const outZip = `${outZipDir}/editor-${production ? commitNumber : Date.now()}.zip`;
const zip = new AdmZip();
zip.addLocalFolder(outDirEditor);
fs.mkdirSync(outZipDir, { recursive: true });
zip.writeZip(outZip);
}