diff --git a/api/package-lock.json b/api/package-lock.json index 32910baa..2af8c7b9 100644 Binary files a/api/package-lock.json and b/api/package-lock.json differ diff --git a/api/package.json b/api/package.json index eb833417..3f0315ae 100644 --- a/api/package.json +++ b/api/package.json @@ -10,7 +10,7 @@ "test": "npm run build && npm run test:only", "test:watch": "jest --watch", "start": "npm run build && node dist/start", - "build": "npx swc src --out-dir dist", + "build": "npx tsc -b .", "build-docker": "tsc -p tsconfig-docker.json", "dev": "tsnd --respawn src/start.ts", "patch": "ts-patch install -s && npx patch-package", @@ -69,6 +69,7 @@ "ts-node": "^9.1.1", "ts-node-dev": "^1.1.6", "ts-patch": "^1.4.4", + "tsup": "^5.4.0", "typescript": "^4.4.2", "typescript-json-schema": "0.50.1" }, @@ -86,6 +87,7 @@ "dot-prop": "^6.0.1", "dotenv": "^8.2.0", "env-paths": "^2.2.1", + "esbuild": "^0.13.4", "express": "^4.17.1", "express-validator": "^6.9.2", "form-data": "^3.0.0", diff --git a/bundle/package-lock.json b/bundle/package-lock.json index f44e5746..dd80d787 100644 Binary files a/bundle/package-lock.json and b/bundle/package-lock.json differ diff --git a/bundle/package.json b/bundle/package.json index c62d6c53..a7f5358b 100644 --- a/bundle/package.json +++ b/bundle/package.json @@ -6,9 +6,9 @@ "scripts": { "setup": "cd ../util && npm --production=false i && cd ../api && npm --production=false i && cd ../cdn && npm --production=false i && cd ../gateway && npm --production=false i && cd ../bundle/ && npm --production=false i && npm run build", "build": "node scripts/build.js", - "build:bundle": "npx swc src --out-dir dist", - "start": "npm run build && npm run start:bundle", - "start:bundle": "node dist/start.js", + "build:bundle": "npx tsc -b .", + "start": "node scripts/build.js && node -r tsconfig-paths/register dist/start.js", + "start:bundle": "node -r tsconfig-paths/register dist/start.js", "test": "echo \"Error: no test specified\" && exit 1" }, "repository": { @@ -41,9 +41,12 @@ "@types/uuid": "^8.3.0", "@types/ws": "^7.4.0", "@zerollup/ts-transform-paths": "^1.7.18", + "esbuild": "^0.13.4", + "esbuild-plugin-tsc": "^0.3.0", "ts-node": "^10.2.1", "ts-patch": "^1.4.4", - "typescript": "^4.3.5" + "tsconfig-paths": "^3.11.0", + "typescript": "^4.4.3" }, "dependencies": { "@fosscord/api": "file:../api", @@ -56,7 +59,6 @@ "missing-native-js-functions": "^1.2.17", "nanocolors": "^0.2.12", "node-os-utils": "^1.3.5", - "swc": "^1.0.11", - "tsconfig-paths": "^3.11.0" + "reflect-metadata": "^0.1.13" } } diff --git a/bundle/scripts/build.js b/bundle/scripts/build.js index c5f9149c..ba559ecf 100644 --- a/bundle/scripts/build.js +++ b/bundle/scripts/build.js @@ -1,37 +1,70 @@ const { spawn } = require("child_process"); const path = require("path"); const { performance } = require("perf_hooks"); +const fs = require("fs"); +const esbuildPluginTsc = require("esbuild-plugin-tsc"); -let parts = "util,api,cdn,gateway,bundle".split(","); +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"); // because npm run is slow we directly get the build script of the package.json script function buildPackage(dir) { const element = path.basename(dir); - const swcBin = path.join(dir, "node_modules", "@swc", "cli", "lib", "swc", "index.js"); - const child = spawn("node", `${swcBin} src --out-dir dist --sync`.split(" "), { - cwd: dir, + require("esbuild").build({ + entryPoints: walk(path.join(dir, "src")), + bundle: false, + outdir: path.join(dir, "dist"), + target: "es2021", + format: "cjs", + plugins: [esbuildPluginTsc({})], + keepNames: true, + }); +} + +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, shell: true, }); function log(data) { - console.log(`[${element}]`.padEnd(10, " ") + data.toString().slice(0, -1)); + console.log(`[util] ` + data.toString().slice(0, -1)); } child.stdout.on("data", log); child.stderr.on("data", log); - child.on("error", (err) => console.error(element, err)); + child.on("error", (err) => console.error("util", err)); return child; } -// util needs to be compiled first as the others require it - const start = performance.now(); +console.log("[Build] starting ..."); +util(); for (const part of parts) { buildPackage(path.join(__dirname, "..", "..", part)); } process.on("exit", () => { - console.log("[Build] took " + Math.round(performance.now() - start) + "ms"); + 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 = 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")) { + /* Is a file */ + results.push(file); + } + }); + return results; +} diff --git a/bundle/src/start.ts b/bundle/src/start.ts index 353ebfc6..872f324e 100644 --- a/bundle/src/start.ts +++ b/bundle/src/start.ts @@ -1,4 +1,20 @@ // process.env.MONGOMS_DEBUG = "true"; +const tsConfigPaths = require("tsconfig-paths"); +const path = require("path"); +const baseUrl = path.join(__dirname, ".."); // Either absolute or relative path. If relative it's resolved to current working directory. +const cleanup = tsConfigPaths.register({ + baseUrl, + paths: { + "@fosscord/api": ["../api/dist/index.js"], + "@fosscord/api/*": ["../api/dist/*"], + "@fosscord/gateway": ["../gateway/dist/index.js"], + "@fosscord/gateway/*": ["../gateway/dist/*"], + "@fosscord/cdn": ["../cdn/dist/index.js"], + "@fosscord/cdn/*": ["../cdn/dist/*"], + }, +}); + +import "reflect-metadata"; import cluster from "cluster"; import os from "os"; import { red, bold, yellow, cyan } from "nanocolors"; diff --git a/bundle/tsconfig.json b/bundle/tsconfig.json index 232531fb..b300a426 100644 --- a/bundle/tsconfig.json +++ b/bundle/tsconfig.json @@ -4,7 +4,7 @@ /* Visit https://aka.ms/tsconfig.json to read more about this file */ /* Basic Options */ - "incremental": true, /* Enable incremental compilation */ + "incremental": true /* Enable incremental compilation */, "target": "ESNext" /* 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. */, @@ -63,6 +63,8 @@ /* Advanced Options */ "skipLibCheck": true /* Skip type checking of declaration files. */, - "forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */ + "forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */, + "emitDecoratorMetadata": true, + "experimentalDecorators": true } } diff --git a/cdn/package-lock.json b/cdn/package-lock.json index 71c8d364..f0cce0af 100644 Binary files a/cdn/package-lock.json and b/cdn/package-lock.json differ diff --git a/cdn/package.json b/cdn/package.json index 4ca7f68b..5d3f1589 100644 --- a/cdn/package.json +++ b/cdn/package.json @@ -7,7 +7,7 @@ "scripts": { "postinstall": "ts-patch install -s", "test": "npm run build && jest --coverage ./tests", - "build": "npx swc src --out-dir dist", + "build": "npx tsc -b .", "start": "npm run build && node dist/start.js" }, "repository": { @@ -61,7 +61,6 @@ "nanocolors": "^0.2.12", "node-fetch": "^2.6.1", "supertest": "^6.1.6", - "swc": "^1.0.11", "typescript": "^4.1.2", "uuid": "^8.3.2" }, diff --git a/gateway/package-lock.json b/gateway/package-lock.json index c48d3987..38962a2a 100644 Binary files a/gateway/package-lock.json and b/gateway/package-lock.json differ diff --git a/gateway/package.json b/gateway/package.json index d9a0ba6c..ddbddeff 100644 --- a/gateway/package.json +++ b/gateway/package.json @@ -8,7 +8,7 @@ "postinstall": "npx ts-patch install -s", "test": "echo \"Error: no test specified\" && exit 1", "start": "npm run build && node dist/start.js", - "build": "npx swc src --out-dir dist", + "build": "npx tsc -b .", "dev": "tsnd --respawn src/start.ts" }, "keywords": [], @@ -41,7 +41,6 @@ "missing-native-js-functions": "^1.2.17", "mongoose-autopopulate": "^0.12.3", "node-fetch": "^2.6.1", - "swc": "^1.0.11", "typeorm": "^0.2.37", "uuid": "^8.3.2", "ws": "^7.4.2" diff --git a/util/package-lock.json b/util/package-lock.json index aa2928e1..0ce50636 100644 Binary files a/util/package-lock.json and b/util/package-lock.json differ diff --git a/util/package.json b/util/package.json index a4dd11c7..32204081 100644 --- a/util/package.json +++ b/util/package.json @@ -8,7 +8,7 @@ "start": "npm run build && node dist/", "test": "npm run build && jest", "postinstall": "npm run build", - "build": "npx swc src --out-dir dist" + "build": "npx tsc -b ." }, "repository": { "type": "git", @@ -54,7 +54,6 @@ "pg": "^8.7.1", "reflect-metadata": "^0.1.13", "sqlite3": "^5.0.2", - "swc": "^1.0.11", "tsconfig-paths": "^3.11.0", "typeorm": "^0.2.37", "typescript": "^4.4.2", diff --git a/util/src/util/Snowflake.ts b/util/src/util/Snowflake.ts index 1d725710..f7a13388 100644 --- a/util/src/util/Snowflake.ts +++ b/util/src/util/Snowflake.ts @@ -1,5 +1,5 @@ // @ts-nocheck -import cluster from "cluster"; +import * as cluster from "cluster"; // https://github.com/discordjs/discord.js/blob/master/src/util/Snowflake.js // Apache License Version 2.0 Copyright 2015 - 2021 Amish Shah