From 7f41933763357119125b8ac388e957ad1c98a4ab Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Sun, 10 Oct 2021 11:02:25 +0200 Subject: :sparkles: changed and fixed compiler --- bundle/scripts/build.js | 112 ++++++------------------------------------------ 1 file changed, 14 insertions(+), 98 deletions(-) (limited to 'bundle/scripts/build.js') diff --git a/bundle/scripts/build.js b/bundle/scripts/build.js index 4266942f..2a82ec57 100644 --- a/bundle/scripts/build.js +++ b/bundle/scripts/build.js @@ -1,103 +1,19 @@ -const { spawn } = require("child_process"); +const { execSync } = require("child_process"); const path = require("path"); -const fs = require("fs"); -const { performance } = require("perf_hooks"); +const fse = require("fs-extra"); -let parts = "api,cdn,gateway,bundle".split(","); -const tscBin = path.join(__dirname, "..", "..", "util", "node_modules", "typescript", "bin", "tsc"); -const swcBin = path.join(__dirname, "..", "..", "util", "node_modules", "@swc", "cli", "bin", "swc"); +const api = path.join(__dirname, "..", "..", "api"); +const dist = path.join(__dirname, "..", "dist"); -// because npm run is slow we directly get the build script of the package.json script +fse.copySync(path.join(api, "assets"), path.join(dist, "api", "assets")); +fse.copySync(path.join(api, "client_test"), path.join(dist, "api", "client_test")); +fse.copySync(path.join(api, "locales"), path.join(dist, "api", "locales")); -function buildPackage(dir) { - const element = path.basename(dir); - - return require("esbuild").build({ - entryPoints: walk(path.join(dir, "src")), - bundle: false, - outdir: path.join(dir, "dist"), - target: "es2021", - // plugins don't really work because bundle is false - keepNames: false, - tsconfig: path.join(dir, "tsconfig.json"), - }); -} - -const importPart = /import (\* as )?(({[^}]+})|(\w+)) from ("[.\w-/@q]+")/g; -const importMod = /import ("[\w-/@q.]+")/g; -const exportDefault = /export default/g; -const exportAllAs = /export \* from (".+")/g; -const exportMod = /export ({[\w, ]+})/g; -const exportConst = /export (const|var|let) (\w+)/g; -const exportPart = /export ((async )?\w+) (\w+)/g; - -// resolves tsconfig paths + rewrites es6 imports/exports to require (because esbuild/swc doesn't work properly) -function transpileFiles() { - for (const part of ["gateway", "api", "cdn", "bundle"]) { - const files = walk(path.join(__dirname, "..", "..", part, "dist")); - for (const file of files) { - let content = fs.readFileSync(file, { encoding: "utf8" }); - content = content - .replace( - new RegExp(`@fosscord/${part}`), - path.relative(file, path.join(__dirname, "..", "..", part, "dist")).slice(3) - ) - .replace(importPart, `const $2 = require($5)`) - .replace(importMod, `require($1)`) - .replace(exportDefault, `module.exports =`) - .replace(exportAllAs, `module.exports = {...(module.exports)||{}, ...require($1)}`) - .replace(exportMod, "module.exports = $1") - .replace(exportConst, `let $2 = {};\nmodule.exports.$2 = $2`) - .replace(exportPart, `module.exports.$3 = $1 $3`); - fs.writeFileSync(file, content); - } - } -} - -function util() { - // const child = spawn("node", `${swcBin} src --out-dir dist --sync`.split(" "), { - const child = spawn("node", `${tscBin} -b .`.split(" "), { - cwd: path.join(__dirname, "..", "..", "util"), - env: process.env, +console.log( + execSync("node " + path.join(__dirname, "..", "node_modules", "typescript", "lib", "tsc.js") + " -p .", { + cwd: path.join(__dirname, ".."), shell: true, - }); - function log(data) { - console.log(`[util] ` + data.toString().slice(0, -1)); - } - child.stdout.on("data", log); - child.stderr.on("data", log); - child.on("error", (err) => console.error("util", err)); - return child; -} - -const start = performance.now(); - -async function main() { - console.log("[Build] starting ..."); - util(); - await Promise.all(parts.map((part) => buildPackage(path.join(__dirname, "..", "..", part)))); - transpileFiles(); -} - -main(); - -process.on("exit", () => { - console.log("[Build] took " + Math.round(performance.now() - start) + "ms"); -}); - -function walk(dir) { - var results = []; - var list = fs.readdirSync(dir); - list.forEach(function (file) { - file = path.join(dir, file); - var stat = fs.statSync(file); - if (stat && stat.isDirectory()) { - /* Recurse into a subdirectory */ - results = results.concat(walk(file)); - } else if (file.endsWith(".ts") || file.endsWith(".js")) { - /* Is a file */ - results.push(file); - } - }); - return results; -} + env: process.env, + encoding: "utf8", + }) +); -- cgit 1.5.1 From 4722dd5d978e39b203c4ded9c56697467b9f7ece Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Sun, 10 Oct 2021 12:35:46 +0200 Subject: :bug: fix tsc compiler --- api/src/Server.ts | 3 ++- bundle/scripts/build.js | 36 +++++++++++++++++++++++++----------- bundle/tsconfig.json | 21 ++++++--------------- cdn/src/Server.ts | 14 ++++++++++---- util/src/util/TraverseDirectory.ts | 10 ++++++++++ util/src/util/index.ts | 1 + 6 files changed, 54 insertions(+), 31 deletions(-) create mode 100644 util/src/util/TraverseDirectory.ts (limited to 'bundle/scripts/build.js') diff --git a/api/src/Server.ts b/api/src/Server.ts index 19e3d245..1f11a295 100644 --- a/api/src/Server.ts +++ b/api/src/Server.ts @@ -11,6 +11,7 @@ import TestClient from "./middlewares/TestClient"; import { initTranslation } from "./middlewares/Translation"; import morgan from "morgan"; import { initInstance } from "./util/Instance"; +import { registerRoutes } from "@fosscord/util"; export interface FosscordServerOptions extends ServerOptions {} @@ -72,7 +73,7 @@ export class FosscordServer extends Server { await initRateLimits(api); await initTranslation(api); - this.routes = await this.registerRoutes(path.join(__dirname, "routes", "/")); + this.routes = await registerRoutes(this, path.join(__dirname, "routes", "/")); api.use("*", (error: any, req: Request, res: Response, next: NextFunction) => { if (error) return next(error); diff --git a/bundle/scripts/build.js b/bundle/scripts/build.js index 2a82ec57..69111c88 100644 --- a/bundle/scripts/build.js +++ b/bundle/scripts/build.js @@ -2,18 +2,32 @@ const { execSync } = require("child_process"); const path = require("path"); const fse = require("fs-extra"); -const api = path.join(__dirname, "..", "..", "api"); -const dist = path.join(__dirname, "..", "dist"); +fse.copySync(path.join(__dirname, "..", "..", "api", "assets"), path.join(__dirname, "..", "dist", "api", "assets")); +fse.copySync( + path.join(__dirname, "..", "..", "api", "client_test"), + path.join(__dirname, "..", "dist", "api", "client_test") +); +fse.copySync(path.join(__dirname, "..", "..", "api", "locales"), path.join(__dirname, "..", "dist", "api", "locales")); +fse.copySync(path.join(__dirname, "..", "..", "api", "src"), path.join(__dirname, "..", "dist", "api", "src")); +fse.copySync(path.join(__dirname, "..", "..", "util", "src"), path.join(__dirname, "..", "dist", "util", "src")); +fse.copySync(path.join(__dirname, "..", "..", "cdn", "src"), path.join(__dirname, "..", "dist", "cdn", "src")); +fse.copySync(path.join(__dirname, "..", "..", "gateway", "src"), path.join(__dirname, "..", "dist", "gateway", "src")); +fse.copySync(path.join(__dirname, "..", "..", "bundle", "src"), path.join(__dirname, "..", "dist", "bundle", "src")); -fse.copySync(path.join(api, "assets"), path.join(dist, "api", "assets")); -fse.copySync(path.join(api, "client_test"), path.join(dist, "api", "client_test")); -fse.copySync(path.join(api, "locales"), path.join(dist, "api", "locales")); +console.log("Copying src files done"); +console.log("Compiling src files ..."); console.log( - execSync("node " + path.join(__dirname, "..", "node_modules", "typescript", "lib", "tsc.js") + " -p .", { - cwd: path.join(__dirname, ".."), - shell: true, - env: process.env, - encoding: "utf8", - }) + execSync( + "node " + + path.join(__dirname, "..", "node_modules", "typescript", "lib", "tsc.js") + + " -p " + + path.join(__dirname, ".."), + { + cwd: path.join(__dirname, ".."), + shell: true, + env: process.env, + encoding: "utf8", + } + ) ); diff --git a/bundle/tsconfig.json b/bundle/tsconfig.json index 1b2949da..4e8db342 100644 --- a/bundle/tsconfig.json +++ b/bundle/tsconfig.json @@ -1,16 +1,11 @@ { - "include": [ - "../api/src/**/*.ts", - "../gateway/src/**/*.ts", - "../cdn/src/**/*.ts", - "../util/src/**/*.ts", - "src/**/*.ts" - ], + "include": ["dist/**/*.ts"], + "exclude": [], "compilerOptions": { /* Visit https://aka.ms/tsconfig.json to read more about this file */ /* Basic Options */ - "incremental": true /* Enable incremental compilation */, + "incremental": false /* Enable incremental compilation */, "target": "ES6" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */, "module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */, "lib": ["ES2021"] /* Specify library files to be included in the compilation. */, @@ -22,7 +17,7 @@ "sourceMap": false /* Generates corresponding '.map' file. */, // "outFile": "./", /* Concatenate and emit output to single file. */ "outDir": "./dist/" /* Redirect output structure to the directory. */, - "rootDir": "../" /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */, + "rootDir": "./dist/" /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */, // "composite": true, /* Enable project compilation */ // "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */ // "removeComments": true, /* Do not emit comments to output. */ @@ -73,16 +68,12 @@ "emitDecoratorMetadata": true, "experimentalDecorators": true, "resolveJsonModule": true, - "baseUrl": "..", + "baseUrl": "./dist/", "paths": { "@fosscord/api": ["api/src/index"], - "@fosscord/api/*": ["api/src/*"], "@fosscord/gateway": ["gateway/src/index"], - "@fosscord/gateway/*": ["gateway/src/*"], "@fosscord/cdn": ["cdn/src/index"], - "@fosscord/cdn/*": ["cdn/src/*"], - "@fosscord/util": ["util/src/index"], - "@fosscord/util/*": ["util/src/*"] + "@fosscord/util": ["util/src/index"] }, "plugins": [{ "transform": "@zerollup/ts-transform-paths" }] } diff --git a/cdn/src/Server.ts b/cdn/src/Server.ts index 590eda6f..cac34a80 100644 --- a/cdn/src/Server.ts +++ b/cdn/src/Server.ts @@ -1,5 +1,5 @@ import { Server, ServerOptions } from "lambert-server"; -import { Config, initDatabase } from "@fosscord/util"; +import { Config, initDatabase, registerRoutes } from "@fosscord/util"; import path from "path"; import avatarsRoute from "./routes/avatars"; import bodyParser from "body-parser"; @@ -23,13 +23,19 @@ export class CDNServer extends Server { "Content-security-policy", "default-src * data: blob: filesystem: about: ws: wss: 'unsafe-inline' 'unsafe-eval'; script-src * data: blob: 'unsafe-inline' 'unsafe-eval'; connect-src * data: blob: 'unsafe-inline'; img-src * data: blob: 'unsafe-inline'; frame-src * data: blob: ; style-src * data: blob: 'unsafe-inline'; font-src * data: blob: 'unsafe-inline';" ); - res.set("Access-Control-Allow-Headers", req.header("Access-Control-Request-Headers") || "*"); - res.set("Access-Control-Allow-Methods", req.header("Access-Control-Request-Methods") || "*"); + res.set( + "Access-Control-Allow-Headers", + req.header("Access-Control-Request-Headers") || "*" + ); + res.set( + "Access-Control-Allow-Methods", + req.header("Access-Control-Request-Methods") || "*" + ); next(); }); this.app.use(bodyParser.json({ inflate: true, limit: "10mb" })); - await this.registerRoutes(path.join(__dirname, "routes/")); + await registerRoutes(this, path.join(__dirname, "routes/")); this.app.use("/icons/", avatarsRoute); this.log("verbose", "[Server] Route /icons registered"); diff --git a/util/src/util/TraverseDirectory.ts b/util/src/util/TraverseDirectory.ts new file mode 100644 index 00000000..275b7dcc --- /dev/null +++ b/util/src/util/TraverseDirectory.ts @@ -0,0 +1,10 @@ +import { Server, traverseDirectory } from "lambert-server"; + +const DEFAULT_FILTER = /^([^\.].*)(?