1
0
Fork 0
forked from svrjs/svrjs

style: format esbuild configuration (the build script)

This commit is contained in:
Dorian Niemiec 2024-11-10 15:46:39 +01:00
parent d34d4bcc2d
commit c9bbd0e202

View file

@ -7,7 +7,9 @@ const archiver = require("archiver");
const chokidar = require("chokidar"); const chokidar = require("chokidar");
const svrjsInfo = JSON.parse(fs.readFileSync(__dirname + "/svrjs.json")); const svrjsInfo = JSON.parse(fs.readFileSync(__dirname + "/svrjs.json"));
const { version } = svrjsInfo; const { version } = svrjsInfo;
const svrjsCoreInfo = JSON.parse(fs.readFileSync(__dirname + "/svrjs.core.json")); const svrjsCoreInfo = JSON.parse(
fs.readFileSync(__dirname + "/svrjs.core.json")
);
const { externalPackages } = svrjsCoreInfo; const { externalPackages } = svrjsCoreInfo;
const coreVersion = svrjsCoreInfo.version; const coreVersion = svrjsCoreInfo.version;
const corePackageJSON = svrjsCoreInfo.packageJSON; const corePackageJSON = svrjsCoreInfo.packageJSON;
@ -23,10 +25,12 @@ if (!fs.existsSync(__dirname + "/dist/temp"))
fs.mkdirSync(__dirname + "/dist/temp"); fs.mkdirSync(__dirname + "/dist/temp");
// Create the out directory if it doesn't exist and if not building for development // Create the out directory if it doesn't exist and if not building for development
if (!isDev && !fs.existsSync(__dirname + "/out")) fs.mkdirSync(__dirname + "/out"); if (!isDev && !fs.existsSync(__dirname + "/out"))
fs.mkdirSync(__dirname + "/out");
// Create the core directory if it doesn't exist and if not building for development // Create the core directory if it doesn't exist and if not building for development
if (!isDev && !fs.existsSync(__dirname + "/core")) fs.mkdirSync(__dirname + "/core"); if (!isDev && !fs.existsSync(__dirname + "/core"))
fs.mkdirSync(__dirname + "/core");
function generateAssets() { function generateAssets() {
// Variables from "svrjs.json" file // Variables from "svrjs.json" file
@ -178,23 +182,25 @@ if (!isDev) {
__dirname + "/package.json", __dirname + "/package.json",
__dirname + "/svrjs.json" __dirname + "/svrjs.json"
]); ]);
watcher.on("change", () => { watcher
try { .on("change", () => {
generateAssets(); try {
} catch (err) { generateAssets();
console.error("There is a problem when regenerating assets!"); } catch (err) {
console.error("Stack:"); console.error("There is a problem when regenerating assets!");
console.error(err.stack); console.error("Stack:");
} console.error(err.stack);
}).on("ready", () => { }
try { })
generateAssets(); .on("ready", () => {
} catch (err) { try {
console.error("There is a problem when regenerating assets!"); generateAssets();
console.error("Stack:"); } catch (err) {
console.error(err.stack); console.error("There is a problem when regenerating assets!");
} console.error("Stack:");
}); console.error(err.stack);
}
});
} }
if (!isDev) { if (!isDev) {
@ -250,85 +256,97 @@ if (!isDev) {
target: "es2017" target: "es2017"
}) })
.then(() => { .then(() => {
const dependencies = JSON.parse(fs.readFileSync(__dirname + "/package.json")).dependencies || {}; const dependencies =
const coreDependencyNames = Object.keys(dependencies).filter((dependency) => externalPackages.indexOf(dependency) != -1); JSON.parse(fs.readFileSync(__dirname + "/package.json"))
.dependencies || {};
const coreDependencyNames = Object.keys(dependencies).filter(
(dependency) => externalPackages.indexOf(dependency) != -1
);
const packageJSON = Object.assign({}, corePackageJSON); const packageJSON = Object.assign({}, corePackageJSON);
// Add package.json properties // Add package.json properties
packageJSON.version = coreVersion; packageJSON.version = coreVersion;
packageJSON.main = "./svr.core.js"; packageJSON.main = "./svr.core.js";
packageJSON.dependencies = coreDependencyNames.reduce((previousDependencies, dependency) => { packageJSON.dependencies = coreDependencyNames.reduce(
previousDependencies[dependency] = dependencies[dependency]; (previousDependencies, dependency) => {
return previousDependencies; previousDependencies[dependency] = dependencies[dependency];
}, {}); return previousDependencies;
},
{}
);
// Write package.json // Write package.json
fs.writeFileSync(__dirname + "/core/package.json", JSON.stringify(packageJSON, null, 2)); fs.writeFileSync(
__dirname + "/core/package.json",
JSON.stringify(packageJSON, null, 2)
);
// Build SVR.JS Core // Build SVR.JS Core
esbuild esbuild
.build({ .build({
entryPoints: ["src/core.js"], entryPoints: ["src/core.js"],
bundle: true, bundle: true,
outfile: "core/svr.core.js", outfile: "core/svr.core.js",
platform: "node", platform: "node",
target: "es2017", target: "es2017",
external: coreDependencyNames, external: coreDependencyNames,
plugins: [ plugins: [
esbuildCopyPlugin.copy({ esbuildCopyPlugin.copy({
resolveFrom: __dirname, resolveFrom: __dirname,
assets: { assets: {
from: ["./coreAssets/**/*"], from: ["./coreAssets/**/*"],
to: ["./core"] to: ["./core"]
}, },
globbyOptions: { globbyOptions: {
dot: true dot: true
} }
}) })
] ]
}) })
.then(() => { .then(() => {
const archiveName = const archiveName =
"svr.js." + "svr.js." +
version.toLowerCase().replace(/[^0-9a-z]+/g, ".") + version.toLowerCase().replace(/[^0-9a-z]+/g, ".") +
".zip"; ".zip";
const output = fs.createWriteStream( const output = fs.createWriteStream(
__dirname + "/out/" + archiveName __dirname + "/out/" + archiveName
); );
const archive = archiver("zip", { const archive = archiver("zip", {
zlib: { level: 9 } zlib: { level: 9 }
}); });
archive.pipe(output); archive.pipe(output);
// Add everything in the "dist" directory except for "svr.js" and "svr.compressed" // Add everything in the "dist" directory except for "svr.js" and "svr.compressed"
archive.glob("**/*", { archive.glob("**/*", {
cwd: __dirname + "/dist", cwd: __dirname + "/dist",
ignore: ["svr.js", "svr.compressed"], ignore: ["svr.js", "svr.compressed"],
dot: true dot: true
}); });
// Create a stream for the "svr.compressed" file // Create a stream for the "svr.compressed" file
const compressedSVRJSFileStream = fs const compressedSVRJSFileStream = fs
.createReadStream(__dirname + "/dist/svr.js") .createReadStream(__dirname + "/dist/svr.js")
.pipe( .pipe(
zlib.createGzip({ zlib.createGzip({
level: 9 level: 9
}) })
); );
archive.append(compressedSVRJSFileStream, { name: "svr.compressed" }); archive.append(compressedSVRJSFileStream, {
archive.append( name: "svr.compressed"
'const zlib = require("zlib");\nconst fs = require("fs");\nconsole.log("Deleting SVR.JS stub...");\nfs.unlinkSync("svr.js");\nconsole.log("Decompressing SVR.JS...");\nconst script = zlib.gunzipSync(fs.readFileSync("svr.compressed"));\nfs.unlinkSync("svr.compressed");\nfs.writeFileSync("svr.js",script);\nconsole.log("Restart SVR.JS to get server interface.");', });
{ name: "svr.js" } archive.append(
); 'const zlib = require("zlib");\nconst fs = require("fs");\nconsole.log("Deleting SVR.JS stub...");\nfs.unlinkSync("svr.js");\nconsole.log("Decompressing SVR.JS...");\nconst script = zlib.gunzipSync(fs.readFileSync("svr.compressed"));\nfs.unlinkSync("svr.compressed");\nfs.writeFileSync("svr.js",script);\nconsole.log("Restart SVR.JS to get server interface.");',
archive.finalize(); { name: "svr.js" }
);
archive.finalize();
})
.catch((err) => {
throw err;
});
}) })
.catch((err) => { .catch((err) => {
throw err; throw err;
}); });
})
.catch((err) => {
throw err;
});
}) })
.catch((err) => { .catch((err) => {
throw err; throw err;