-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.ts
More file actions
274 lines (235 loc) · 7.23 KB
/
build.ts
File metadata and controls
274 lines (235 loc) · 7.23 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
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
import * as esbuild from "https://deno.land/x/esbuild@v0.19.11/mod.js";
import { ensureDir } from "https://deno.land/std@0.208.0/fs/mod.ts";
// Get last build time
let lastBuild = new Date('2000-01-01T00:00');
try {
const stat = await Deno.stat('./.lastbuild');
lastBuild = stat.mtime || lastBuild;
} catch {
console.log('No previous build found');
}
console.log('Last build:', lastBuild);
async function fileExists(filePath: string): Promise<boolean> {
try {
await Deno.stat(filePath);
return true;
} catch {
return false;
}
}
async function isNewer(filePath: string): Promise<boolean> {
try {
const stat = await Deno.stat(filePath);
return (stat.mtime || new Date(0)) > lastBuild;
} catch {
return false;
}
}
async function minifyCSS(cssContent: string): Promise<string> {
const result = await esbuild.transform(cssContent, {
minify: true,
loader: "css"
});
return result.code;
}
async function minifyJS(jsContent: string): Promise<string> {
const result = await esbuild.transform(jsContent, {
minify: true,
loader: "js",
target: "esnext"
});
return result.code;
}
async function removeCRLF(jsContent: string): Promise<string> {
// Remove CR+LF and collapse multiple spaces in string literals for better compression
return jsContent.replace(/("([^"\\]|\\.)*")|('([^'\\]|\\.)*')|`([^`\\]|\\.)*`/g, (match) => {
return match.replace(/\n/g, '').replace(/\r/g, '').replace(/ +/g, ' ');
});
}
async function buildCSS(components: string, include = '', force = false) {
const names = components.split(',');
const mainName = names[0];
// Build list of CSS files to check
const cssFiles = [];
if (await fileExists('./css/kv-style.css')) {
cssFiles.push('./css/kv-style.css');
}
if (include) {
for (const name of include.split(',')) {
const file = `./${mainName}/${name}.css`;
if (await fileExists(file)) {
cssFiles.push(file);
}
}
}
for (const name of names) {
const file = `./${name}/${name}.css`;
if (await fileExists(file)) {
cssFiles.push(file);
}
}
if (cssFiles.length === 0) {
console.log(`Warning: No CSS files found for ${mainName}`);
return;
}
// Check if we need to rebuild
if (!force) {
const needsRebuild = await Promise.all(cssFiles.map(file => isNewer(file)));
const outputFile = `./${mainName}/deploy/${mainName}.min.css`;
if (!needsRebuild.some(Boolean) && await fileExists(outputFile)) {
console.log(`Skipping ${mainName} CSS - no changes`);
return;
}
}
let combinedCSS = '';
for (const file of cssFiles) {
try {
const content = await Deno.readTextFile(file);
combinedCSS += content + '\n';
} catch (error) {
if (error instanceof Error) {
if (error instanceof Error) {
console.log(`Error reading ${file}: ${error.message}`);
} else {
console.log(`Error reading ${file}:`, error);
}
} else {
console.log(`Error reading ${file}:`, error);
}
}
}
if (combinedCSS.trim() === '') {
console.log(`Warning: No CSS content found for ${mainName}`);
return;
}
const minified = await minifyCSS(combinedCSS);
await ensureDir(`./${mainName}/deploy`);
await Deno.writeTextFile(`./${mainName}/deploy/${mainName}.min.css`, minified);
console.log(`Built ${mainName}.min.css`);
}
async function buildJS(components: string, include = '', force = false) {
const names = components.split(',');
const mainName = names[0];
// Build list of JS files to check
const jsFiles = [];
if (include) {
for (const name of include.split(',')) {
const file = `./${mainName}/${name}.js`;
if (await fileExists(file)) {
jsFiles.push(file);
}
}
}
for (const name of names) {
const file = `./${name}/${name}.js`;
if (await fileExists(file)) {
jsFiles.push(file);
}
}
if (jsFiles.length === 0) {
console.log(`Warning: No JS files found for ${mainName}`);
return;
}
// Check if we need to rebuild
if (!force) {
const needsRebuild = await Promise.all(jsFiles.map(file => isNewer(file)));
const outputFile = `./${mainName}/deploy/${mainName}.min.js`;
if (!needsRebuild.some(Boolean) && await fileExists(outputFile)) {
console.log(`Skipping ${mainName} JS - no changes`);
return;
}
}
let combinedJS = '';
for (const file of jsFiles) {
try {
const content = await Deno.readTextFile(file);
combinedJS += content + '\n';
} catch (error) {
if (error instanceof Error) {
console.log(`Error reading ${file}: ${error.message}`);
} else {
console.log(`Error reading ${file}:`, error);
}
}
}
if (combinedJS.trim() === '') {
console.log(`Warning: No JS content found for ${mainName}`);
return;
}
combinedJS = await removeCRLF(combinedJS);
const minified = await minifyJS(combinedJS);
await ensureDir(`./${mainName}/deploy`);
await Deno.writeTextFile(`./${mainName}/deploy/${mainName}.min.js`, minified.replace(/(\t+)/g, " "));
console.log(`Built ${mainName}.min.js`);
}
async function buildHTML(name: string, force = false) {
const htmlFile = `./${name}/index.html`;
if (!(await fileExists(htmlFile))) {
console.log(`Warning: ${htmlFile} not found`);
return;
}
if (!force && !(await isNewer(htmlFile))) {
const outputFile = `./${name}/deploy/${name}.min.html`;
if (await fileExists(outputFile)) {
console.log(`Skipping ${name} HTML - no changes`);
return;
}
}
try {
let content = await Deno.readTextFile(htmlFile);
content = content
.replace(new RegExp(`${name}\\.js`, 'g'), `${name}.min.js`)
.replace(new RegExp(`${name}\\.css`, 'g'), `${name}.min.css`);
await ensureDir(`./${name}/deploy`);
await Deno.writeTextFile(`./${name}/deploy/${name}.min.html`, content);
console.log(`Built ${name}.min.html`);
} catch (error) {
if (error instanceof Error) {
console.log(`Error processing ${htmlFile}: ${error.message}`);
} else {
console.log(`Error processing ${htmlFile}:`, error);
}
}
}
// Build all components
async function buildAll() {
const tasks = [
// HTMLElement components
buildCSS("kv-gauge"), buildJS("kv-gauge"),
buildCSS("kv-tags"), buildJS("kv-tags"),
buildCSS("kv-budget"), buildJS("kv-budget"),
buildCSS("kv-pick"), buildJS("kv-pick"),
buildCSS("kv-timeline"), buildJS("kv-timeline"),
buildCSS("kv-params,kv-pair", "", true), buildJS("kv-params,kv-pair", "UMS", true),
buildCSS("kv-pair"), buildJS("kv-pair"), buildHTML("kv-pair"),
buildCSS("kv-gantt"), buildJS("kv-gantt"),
buildJS("kv-fringe"),
buildCSS("kv-jsonform"), buildJS("kv-jsonform"),
buildCSS("wms"), buildJS("wms", "wms-structure,wms-map,wms-loading-units", true),
// Not HTMLElement
buildCSS("kvJSONForm"), buildJS("kvJSONForm"),
buildCSS("kvTags"), buildJS("kvTags"),
buildCSS("kvImportData"), buildJS("kvImportData"),
buildCSS("kvSelect"), buildJS("kvSelect"),
];
await Promise.all(tasks);
// Update last build time
await Deno.writeTextFile('./.lastbuild', new Date().toISOString());
console.log('Build complete!');
// Call updatePartsList from playgrounds.ts
try {
const playgrounds = await import('./playgrounds.ts');
if (typeof playgrounds.updatePartsList === 'function') {
await playgrounds.updatePartsList();
console.log('Project list updated.');
} else {
console.warn('updatePartsList not found in playgrounds.ts');
}
} catch (err) {
console.error('Failed to update project list:', err);
}
}
if (import.meta.main) {
await buildAll();
esbuild.stop();
}