forked from hestiacp/hestiacp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
156 lines (141 loc) · 3.75 KB
/
build.js
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
/* eslint-env node */
/* eslint-disable no-console */
// Build JS and CSS using esbuild and PostCSS
import { promises as fs } from 'node:fs';
import path from 'node:path';
import browserslist from 'browserslist';
import esbuild from 'esbuild';
import * as lightningcss from 'lightningcss';
// Packages to build but exclude from bundle
const externalPackages = [
'chart.js/auto',
'alpinejs/dist/cdn.min.js',
'xterm',
'xterm-addon-webgl',
'xterm-addon-canvas',
];
// Build main bundle
async function buildJS() {
const inputPath = './web/js/src/index.js';
try {
await esbuild.build({
entryPoints: [inputPath],
outfile: './web/js/dist/main.min.js',
bundle: true,
minify: true,
sourcemap: true,
external: externalPackages,
});
console.log('✅ JavaScript build completed for', inputPath);
} catch (error) {
console.error('❌ Error building JavaScript:', error);
process.exit(1);
}
}
// Build external packages
async function buildExternalJS() {
try {
const buildPromises = externalPackages.map(async (pkg) => {
const outputPath = getOutputPath(pkg);
await esbuild.build({
entryPoints: [pkg],
outfile: outputPath,
bundle: true,
minify: true,
format: 'esm',
});
console.log(`✅ Dependency build completed for ${pkg}`);
});
await Promise.all(buildPromises);
} catch (error) {
console.error('❌ Error building external packages:', error);
process.exit(1);
}
}
function getOutputPath(pkg) {
let pkgName;
if (pkg.startsWith('alpinejs')) {
pkgName = 'alpinejs';
} else {
pkgName = pkg.replace(/\//g, '-');
}
return `./web/js/dist/${pkgName}.min.js`;
}
// Process a CSS file
async function processCSS(inputFile, outputFile) {
try {
await ensureDir(path.dirname(outputFile));
const css = await fs.readFile(inputFile);
const bundle = await lightningcss.bundleAsync({
filename: inputFile,
sourceMap: true,
code: Buffer.from(css),
minify: true,
targets: lightningcss.browserslistToTargets(browserslist()),
drafts: { customMedia: true, nesting: true },
visitor: {
Url: (node) => {
// Fix relative paths for webfonts
if (node.url.startsWith('../webfonts/')) {
return {
url: node.url.replace('../webfonts/', '/webfonts/'),
loc: node.loc,
};
}
return node;
},
},
resolver: {
resolve(specifier, from) {
if (!specifier.endsWith('.css')) {
specifier += '.css';
}
if (specifier.startsWith('node:')) {
return `node_modules/${specifier.replace('node:', '')}`;
}
return `${path.dirname(from)}/${specifier}`;
},
},
});
await fs.writeFile(outputFile, bundle.code);
await fs.writeFile(`${outputFile}.map`, bundle.map);
console.log(`✅ CSS build completed for ${inputFile}`);
} catch (error) {
console.error(`❌ Error processing CSS for ${inputFile}:`, error);
process.exit(1);
}
}
// Build CSS
async function buildCSS() {
const themesSourcePath = './web/css/src/themes/';
const cssEntries = await fs.readdir(themesSourcePath);
const cssBuildPromises = cssEntries
.filter((entry) => path.extname(entry) === '.css')
.map(async (entry) => {
const entryName = entry.replace('.css', '.min.css');
const inputPath = path.join(themesSourcePath, entry);
const outputPath = `./web/css/themes/${entryName}`;
await processCSS(inputPath, outputPath);
});
await Promise.all(cssBuildPromises);
}
// Ensure a directory exists
async function ensureDir(dir) {
try {
await fs.mkdir(dir, { recursive: true });
} catch (error) {
if (error.code !== 'EEXIST') {
throw error;
}
}
}
// Build all assets
async function build() {
console.log('🚀 Building JS and CSS...');
await buildJS();
await buildExternalJS();
await buildCSS();
console.log('🎉 Build completed.');
}
// Execute build
build();