-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathesbuild.js
More file actions
527 lines (469 loc) · 13.4 KB
/
Copy pathesbuild.js
File metadata and controls
527 lines (469 loc) · 13.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
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
/**
* ....
* .: '':.
* :::: ':..
* ::. ''..
* .:'.. ..':.:::' . :. '':.
* :. '' '' '. ::::.. ..:
* ::::. ..':.. .'''::::: .
* :::::::.. '..:::: :. :::: :
* ::'':::::::. ':::.'':.:::: :
* :.. ''::::::....': '':: :
* :::::. '::::: : .. '' .
* .''::::::::... ':::.'' ..'' :.''''.
* :..:::''::::: :::::...:'' :..:
* ::::::. ':::: :::::::: ..:: .
* ::::::::.:::: :::::::: :'':.:: .''
* ::: '::::::::.' ''::::: :.' '': :
* ::: :::::::::..' :::: ::...' .
* ::: .:::::::::: :::: :::: .:'
* '::' ''::::::: :::: : :: :
* ':::: :::: :'' .:
* :::: :::: ..''
* :::: ..:::: .:''
* '''' '''''
*
*
* AUTOMAD
*
* Copyright (c) 2025-2026 by Marc Anton Dahmen
* https://marcdahmen.de
*
* See LICENSE.md for license information.
*/
/*
* Esbuild config for Automad
*
* This script handles automatic splitting of vendor modules and block classes in order
* to be able to import those modules asynchronously. Splitting can be controlled entirely
* by the file structure of the entry points. There are three main catergories:
*
* 1. Main "index" Files
*
* Index files are the main entry points that are loaded by the actual PHP pages.
* They have to named "index.ts".
*
* 2. Block Classes
*
* Block classes are imported dynamically whenever a related web component is connected.
* They have to match the pattern "blocks/components/*.ts".
* All class modules are marked as external for esbuild and split into separate files
* with a hashed filename.
*
* 3. Vendor Modules
*
* Large vendor modules are also split into separate files and also have hashed
* filenames. Like blocks, they are also imported by other modules and therefore
* are not loaded by PHP pages.
* They have to match the pattern "vendor/*.ts".
*
* Note that the common module is not build as an external file.
*/
import pkg from './package.json' with { type: 'json' };
import esbuild from 'esbuild';
import less from 'less';
import postcss from 'esbuild-postcss';
import browserSync from 'browser-sync';
import { sassPlugin } from 'esbuild-sass-plugin';
import { fileURLToPath } from 'node:url';
import crypto from 'node:crypto';
import path from 'node:path';
import fs from 'node:fs';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const clientSrc = path.join(__dirname, 'automad/src/client');
const outdir = path.join(__dirname, 'automad/dist/build');
const isDev = process.argv.includes('--dev');
const hashPlaceholder = '@hash';
const banner = `/* This file is part of Automad. Copyright and license info at the end. */`;
const licenseAutomad = `
/*!
* Automad (https://automad.org)
*
* Copyright (c) ${new Date().getFullYear()} ${pkg.author} (https://marcdahmen.de)
* See LICENSE.md for license information.
*/
`;
const fileHash = (buffer, length = 8) => {
return crypto
.createHash('sha1')
.update(buffer)
.digest('hex')
.slice(0, length);
};
const findEntries = (pattern) => {
return fs
.globSync(`${clientSrc}/${pattern}`)
.map((f) => f.replace(`${clientSrc}/`, '').replace('.ts', ''));
};
const makeRelative = (file) =>
`../${path.basename(path.dirname(file))}/${path.basename(file)}`;
const write = (file, contents) => {
fs.mkdirSync(path.dirname(file), {
recursive: true,
});
fs.writeFileSync(file, contents);
console.log(` ${makeRelative(file)}`);
};
/**
* Create a path configuration including entry points, external files and aliases.
*/
const pathConfig = () => {
const mainEntries = findEntries('*/index.ts').filter(
(f) => !f.match(/common/)
);
const vendorEntries = findEntries('vendor/*.ts');
const blockEntries = findEntries('blocks/components/*.ts');
// Prepare the entryPoints by adding a hash placeholder to all
// non-index files.
const entryPoints = [
...mainEntries.map((entry) => {
return {
in: path.join(clientSrc, entry),
out: entry,
};
}),
...blockEntries.map((entry) => {
return {
in: path.join(clientSrc, entry),
out: `${entry.replace('components/', '')}-${hashPlaceholder}`,
};
}),
...vendorEntries.map((entry) => {
return {
in: path.join(clientSrc, entry),
out: `${entry}-${hashPlaceholder}`,
};
}),
];
const alias = {};
// The items in the alias list are modified by adding the hash placeholder
// accordingly to match the entryPoints config.
blockEntries.forEach((entry) => {
const key = `@/${entry}`;
alias[key] =
`../${entry.replace('components/', '')}-${hashPlaceholder}.js`;
});
vendorEntries.forEach((entry) => {
const key = `@/${entry}`;
alias[key] = `../${entry}-${hashPlaceholder}.js`;
});
// Now mark all non-index files with their hashed filenames as external.
const external = [
...blockEntries.map(
(entry) =>
`../${entry.replace('components/', '')}-${hashPlaceholder}.js`
),
...vendorEntries.map((entry) => `../${entry}-${hashPlaceholder}.js`),
];
return { alias, entryPoints, external };
};
/**
* The output hash map that can be used across build steps in dev mode.
*/
const outputHashes = new Map();
/**
* A plugin that hashes output files and updates import statments accordingly.
*/
const hashImportsPlugin = () => {
return {
name: 'hash-imports',
setup(build) {
build.onEnd(async (result) => {
const outputs = new Map();
const placeholderRegex = new RegExp(
`\\.\\.\\/[\\w\\/-]*-${hashPlaceholder}\\.js`,
'g'
);
// First the esbuild output files array is converted
// to a custom output map with additional data.
result.outputFiles.forEach((outputFile) => {
if (outputFile.path.match(/\.js$/)) {
const content = new TextDecoder('utf-8').decode(
outputFile.contents
);
const pathRelative = makeRelative(outputFile.path);
const pathAbsolute = outputFile.path;
// Placeholder imports are imports that contain
// the @hash placeholder in their path.
const placeholderImports =
content.match(placeholderRegex) ?? [];
outputs.set(pathRelative, {
content,
pathRelative,
pathAbsolute,
placeholderImports,
newPathAbsolute: null,
});
} else {
// Write all non-JS assets directly and without hashing
// since they are not async imported.
if (
!outputHashes.get(outputFile.path) ||
outputHashes.get(outputFile.path) != outputFile.hash
) {
write(outputFile.path, outputFile.contents);
outputHashes.set(outputFile.path, outputFile.hash);
}
}
});
// Now iterate all output files once and store all files
// that import it inside the dependents array.
outputs.forEach((out) => {
out.dependents = [];
outputs.forEach((dep) => {
if (
dep.placeholderImports?.includes(out.pathRelative)
) {
out.dependents.push(dep.pathRelative);
}
});
});
// In order to resolve all paths correctly and hash their content
// after the contained import placeholders are also resolved
// and hashed as well, the output map is processed in a
// while loop as longs as it contains elements.
while (outputs.size > 0) {
let finalizedFiles = [];
// During each iteration, all files that do not contain any import
// placeholders, are considered clean and finalized" and will be
// added to the array of files that can be hashed and processed.
outputs.forEach((out, key) => {
if (out.placeholderImports.length == 0) {
finalizedFiles.push(key);
}
});
// That hash is used to update the file path by replacing
// the placeholder with it. Then all references in its dependents are updated as well.
finalizedFiles.forEach((key) => {
const out = outputs.get(key);
// When processing the clean and finalized files, first,
// a hash of the file content is generated.
const hash = fileHash(out.content);
// That hash is used to update the file path by replacing
// the placeholder with it.
out.newPathAbsolute = out.pathAbsolute.replace(
hashPlaceholder,
hash
);
const newImport = out.pathRelative.replace(
hashPlaceholder,
hash
);
out.dependents.forEach((path) => {
const dep = outputs.get(path);
// Then all file references in its dependents are updated as well.
dep.content = dep.content.replaceAll(
out.pathRelative,
newImport
);
// Now the finalized and hashed imports are removed from the
// dependent's list of placeholder imports.
dep.placeholderImports =
dep.content.match(placeholderRegex) ?? [];
});
if (
!outputHashes.get(key) ||
outputHashes.get(key) != hash
) {
// In case the hash has changed since the last build or it is
// the first build, remove old versions of the file first.
fs.globSync(
out.pathAbsolute.replace(
/-[a-zA-Z0-9@]+\.js/,
'-*.js'
)
).forEach((f) => {
if (fs.existsSync(f)) {
fs.unlinkSync(f);
}
});
// Then write the content to the file with the new hashed filename.
write(out.newPathAbsolute, out.content);
// And store the hash in the cross-build map.
outputHashes.set(key, hash);
}
// Finally remove the file form the outputs map.
outputs.delete(key);
});
}
});
},
};
};
/**
* Return a license comment based on a file path.
* For all files inside that the Automad src directory
* that are not in vendor, a license comment is returned.
*/
const generateLicense = (path) => {
if (path.match(/(vendor|node_modules)/)) {
return '';
}
return licenseAutomad;
};
/**
* Minify TS file and especially included string litrals.
*/
const minifyTs = (source) =>
source
// Remove all single line comments in order to safely remove newlines.
.replace(/\/\/\s.*$/gm, ' ')
// Once replace multiple whitespace chars including newlines to a single space.
.replace(/\s+/g, ' ')
// From here on, on single \s needs to be matched.
.replace(/\>\s\</g, '><')
.replace(/(\w\>)\s/g, '$1')
.replace(/\s(\<\/\w)/g, '$1')
// Also trim template strings.
.replace(/`([^`]+)`/g, (_, s) => `\`${s.trim()}\``);
/**
* A plugin that minifies TS code with string literals and
* injects a license comment for Automad entry points.
*/
const tsOnLoadPlugin = () => {
return {
name: 'ts-on-load',
setup(build) {
build.onLoad(
{ filter: /\.ts$/, namespace: 'file' },
async (args) => {
const source = await fs.promises.readFile(
args.path,
'utf8'
);
return {
contents: generateLicense(args.path) + minifyTs(source),
loader: 'ts',
};
}
);
},
};
};
/**
* Build less files and inject license comment for Automad entry points.
*/
const lessOnLoadPlugin = () => {
return {
name: 'less-on-load',
setup(build) {
build.onLoad(
{ filter: /\.less$/, namespace: 'file' },
async (args) => {
const source = await fs.promises.readFile(
args.path,
'utf8'
);
try {
// https://lesscss.org/usage/#programmatic-usage
const { css, imports } = await less.render(source, {
// Set this in order to resolve imports
filename: args.path,
});
return {
contents: generateLicense(args.path) + css,
watchFiles: imports,
loader: 'css',
};
} catch (error) {
process.exit(1);
}
}
);
},
};
};
/**
* Minify SVG files.
*/
const minifySvg = (source) =>
source
// remove XML / HTML comments
.replace(/<!--[\s\S]*?-->/g, '')
// collapse whitespace
.replace(/\s+/g, ' ')
// remove space between tags
.replace(/>\s+</g, '><')
.trim();
/**
* A plugin that minifies SVG files.
*/
const svgOnLoadPlugin = () => ({
name: 'svg-on-load',
setup(build) {
build.onLoad({ filter: /\.svg$/ }, async (args) => {
const source = await fs.promises.readFile(args.path, 'utf8');
return {
contents: minifySvg(source),
loader: 'text', // still inline as string
};
});
},
});
/**
* A common config shared between build and dev mode.
*/
const commonConfig = {
...pathConfig(),
bundle: true,
format: 'esm',
minify: !isDev,
target: ['es2022'],
assetNames: '[name]',
write: false,
outdir,
banner: {
js: banner,
css: banner,
},
legalComments: 'eof',
pure: isDev ? [] : ['console.warn'],
logLevel: 'info',
loader: {
'.svg': 'text',
'.woff': 'file',
'.woff2': 'file',
},
metafile: true,
define: { DEVELOPMENT: isDev.toString() },
plugins: [
svgOnLoadPlugin(),
lessOnLoadPlugin(),
sassPlugin({
quietDeps: true,
filter: /\.scss/,
}),
postcss(),
...(isDev ? [] : [tsOnLoadPlugin()]),
hashImportsPlugin(),
],
};
async function buildAll() {
await esbuild.build(commonConfig);
}
async function startDev() {
await buildAll();
const bs = browserSync.create();
bs.init({
host: 'localhost',
port: 3000,
proxy: 'http://localhost:8000',
open: false,
notify: false,
files: ['**/src/**/*.php', '**/dist/**/*.{js,css}'],
serveStatic: [outdir],
});
let ctx = await esbuild.context(commonConfig);
await ctx.watch();
}
if (isDev) {
startDev().catch((err) => {
console.error(err);
process.exit(1);
});
} else {
buildAll().catch((err) => {
console.error(err);
process.exit(1);
});
}