-
Notifications
You must be signed in to change notification settings - Fork 45
/
build.js
164 lines (139 loc) · 6.34 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
157
158
159
160
161
162
163
164
// This script deploys all files from /src/client to /dist in order to run the website.
// Depending on the value of DEV_BUILD in /src/server/config/config.js, this happens either in development mode or in production mode.
// Development mode: All files are simply copied over unmodified.
// Production mode: All non-script and non-css assets are copied over unmodified,
// but all ESM scripts are bundled by esbuild,
// all scripts are then minified with the use of @swc/core,
// Further, all css files are minified by lightningcss.
import { cp as copy, rm as remove, readFile, writeFile } from 'node:fs/promises';
import swc from "@swc/core";
import browserslist from 'browserslist';
import { transform, browserslistToTargets } from 'lightningcss';
import { insertScriptIntoHTML } from './src/server/utility/HTMLScriptInjector.js';
import { BUNDLE_FILES } from './src/server/config/config.js';
import esbuild from 'esbuild';
import path from "node:path";
import { getAllFilesInDirectoryWithExtension, writeFile_ensureDirectory } from './src/server/utility/fileUtils.js';
/**
* Any ES Module that any HTML document IMPORTS directly!
* ADD TO THIS when we create new modules that nothing else depends on!
*
* ESBuild has to build each of them and their dependancies
* into their own bundle!
*/
const entryPoints = [
'src/client/scripts/esm/game/main.js',
'src/client/scripts/esm/components/header/header.js',
'src/client/scripts/esm/views/member.js',
];
// Targetted browsers for CSS transpilation
// Format: https://github.com/browserslist/browserslist?tab=readme-ov-file#query-composition
const targets = browserslistToTargets(browserslist('defaults'));
/**
* ESBuild takes each entry point and all of their dependencies and merges them bundling them into one file.
* If multiple entry points share dependencies, then those dependencies will be split into separate modules,
* which means they aren't duplicated, and there's only one instance of it per page.
* This also means more requests to the server, but not many.
*/
async function bundleESMScripts() {
await esbuild.build({
bundle: true,
entryPoints,
// outfile: './dist/scripts/game/main.js', // Old, for a single entry point
outdir: './dist/scripts/esm',
/**
* Enable code splitting, which means if multiple entry points require the same module,
* that dependancy will be separated out of both of them which means it isn't duplicated,
* and there's only one instance of it per page.
* This also means more requests to the server, but not many.
* If this is false, multiple copies of the same code may be loaded onto a page,
* each belonging to a separate entry point module.
*/
splitting: true,
legalComments: 'none', // Even skips copyright noticies, such as in gl-matrix
format: 'esm', // or 'cjs' for Common JS
});
// Further minify them. This cuts off their size a further 60%!!!
await minifyDirectory('./dist/scripts/esm/', './dist/scripts/esm/', true); // true for ES Modules
}
/**
* Minifies all JavaScript files in a directory and writes them to an output directory.
* @param {string} inputDir - The directory to scan for scripts.
* @param {string} outputDir - The directory where the minified files will be written.
* @param {boolean} isModule - True if the scripts are ES Modules instead of CommonJS.
* @returns {Promise<void>} Resolves when all files are minified.
*/
async function minifyDirectory(inputDir, outputDir, isModule) {
const files = await getAllFilesInDirectoryWithExtension(inputDir, '.js');
for (const file of files) {
const inputFilePath = path.join(inputDir, file);
const outputFilePath = path.join(outputDir, file);
const content = await readFile(inputFilePath, 'utf-8');
const minified = await swc.minify(content, {
mangle: true, // Enable variable name mangling
compress: true, // Enable compression
sourceMap: false,
module: isModule, // Include if we're minifying ES Modules instead of Common JS
});
// Write the minified file to the output directory
writeFile_ensureDirectory(outputFilePath, minified.code);
// console.log(`Minified: ${outputFilePath}`);
}
}
/**
* Minifies all CSS files from src/client/css/ directory
* to the distribution directory, preserving the original structure.
* @returns {Promise<void>} Resolves when all CSS files are processed.
*/
async function minifyCSSFiles() {
// Bundle and compress all css files
const cssFiles = await getAllFilesInDirectoryWithExtension("./src/client/css", ".css");
for (const file of cssFiles) {
// Minify css files
const { code } = transform({
targets: targets,
code: Buffer.from(await readFile(`./src/client/css/${file}`, 'utf8')),
minify: true,
});
// Write into /dist
await writeFile_ensureDirectory(`./dist/css/${file}`, code);
}
}
// Delete the built "dist" folder from the last run
await remove("./dist", {
recursive: true,
force: true,
});
if (!BUNDLE_FILES) {
/**
* In development, copy all clientside files directly over to /dist AS THEY ARE.
* No files are bundled. This means a LOT of requests! But, all our comments are there!
*/
await copy("./src/client", "./dist", {
recursive: true,
force: true
});
} else { // BUNDLE files in production! Far fewer requests, and each file is significantly smaller!
// Copy EVERYTHING over from src/client/ into dist/ EXCEPT SCRIPTS and CSS files,
// because those are compressed and minified manually.
await copy("./src/client", "./dist", {
recursive: true,
force: true,
filter: filename => { // Only pass if it is not a script or css file.
return !/(\\|\/)scripts/.test(filename) && !/(\\|\/)css/.test(filename);
}
});
// Minify all CJS scripts and copy them over to dist/
await minifyDirectory('src/client/scripts/cjs/', './dist/scripts/cjs/', false); // false for CommonJS Modules
// Bundle and Minify all ESM scripts and copy them over to dist/
await bundleESMScripts();
// Bundle and compress all css files
await minifyCSSFiles();
}
// Overwrite play.ejs, directly inserting htmlscript.js into it.
/** The relative path to play.ejs */
const playEJSPath = './dist/views/play.ejs';
const playEJS = await readFile(playEJSPath, 'utf8');
const htmlscriptJS = await readFile('./dist/scripts/cjs/game/htmlscript.js');
const newPlayEJS = insertScriptIntoHTML(playEJS, htmlscriptJS, {}, '<!-- htmlscript inject here -->');
await writeFile(playEJSPath, newPlayEJS, 'utf8');