🐛 fix build

This commit is contained in:
Flam3rboy 2021-10-09 01:44:44 +02:00
parent d747b202c8
commit 826b2c9820
13 changed files with 55 additions and 79 deletions

3
.gitignore vendored
View File

@ -4,4 +4,5 @@ dist/
node_modules node_modules
api/assets/*.js api/assets/*.js
api/assets/*.css api/assets/*.css
database.db database.db
tsconfig.tsbuildinfo

View File

@ -1,17 +0,0 @@
{
"module": {
"type": "commonjs"
},
"jsc": {
"parser": {
"syntax": "typescript",
"decorators": true
},
"target": "es2021",
"baseUrl": "./",
"paths": {
"@fosscord/api": ["src/index.ts"],
"@fosscord/api/*": ["src/*"]
}
}
}

BIN
api/package-lock.json generated

Binary file not shown.

View File

@ -1,12 +0,0 @@
{
"module": {
"type": "commonjs"
},
"jsc": {
"parser": {
"syntax": "typescript",
"decorators": true
},
"target": "es2021"
}
}

BIN
bundle/package-lock.json generated

Binary file not shown.

View File

@ -1,8 +1,7 @@
const { spawn } = require("child_process"); const { spawn } = require("child_process");
const path = require("path"); const path = require("path");
const { performance } = require("perf_hooks");
const fs = require("fs"); const fs = require("fs");
const esbuildPluginTsc = require("esbuild-plugin-tsc"); const { performance } = require("perf_hooks");
let parts = "api,cdn,gateway,bundle".split(","); let parts = "api,cdn,gateway,bundle".split(",");
const tscBin = path.join(__dirname, "..", "..", "util", "node_modules", "typescript", "bin", "tsc"); const tscBin = path.join(__dirname, "..", "..", "util", "node_modules", "typescript", "bin", "tsc");
@ -13,17 +12,49 @@ const swcBin = path.join(__dirname, "..", "..", "util", "node_modules", "@swc",
function buildPackage(dir) { function buildPackage(dir) {
const element = path.basename(dir); const element = path.basename(dir);
require("esbuild").build({ return require("esbuild").build({
entryPoints: walk(path.join(dir, "src")), entryPoints: walk(path.join(dir, "src")),
bundle: false, bundle: false,
outdir: path.join(dir, "dist"), outdir: path.join(dir, "dist"),
target: "es2021", target: "es2021",
format: "cjs", // plugins don't really work because bundle is false
plugins: [esbuildPluginTsc({})], keepNames: false,
keepNames: true, 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" });
console.log(file);
content = content
.replaceAll(
`@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() { function util() {
// const child = spawn("node", `${swcBin} src --out-dir dist --sync`.split(" "), { // const child = spawn("node", `${swcBin} src --out-dir dist --sync`.split(" "), {
const child = spawn("node", `${tscBin} -b .`.split(" "), { const child = spawn("node", `${tscBin} -b .`.split(" "), {
@ -41,13 +72,16 @@ function util() {
} }
const start = performance.now(); const start = performance.now();
console.log("[Build] starting ...");
util(); async function main() {
for (const part of parts) { console.log("[Build] starting ...");
buildPackage(path.join(__dirname, "..", "..", part)); util();
await Promise.all(parts.map((part) => buildPackage(path.join(__dirname, "..", "..", part))));
transpileFiles();
} }
main();
process.on("exit", () => { process.on("exit", () => {
console.log("[Build] took " + Math.round(performance.now() - start) + "ms"); console.log("[Build] took " + Math.round(performance.now() - start) + "ms");
}); });
@ -61,7 +95,7 @@ function walk(dir) {
if (stat && stat.isDirectory()) { if (stat && stat.isDirectory()) {
/* Recurse into a subdirectory */ /* Recurse into a subdirectory */
results = results.concat(walk(file)); results = results.concat(walk(file));
} else if (file.endsWith(".ts")) { } else if (file.endsWith(".ts") || file.endsWith(".js")) {
/* Is a file */ /* Is a file */
results.push(file); results.push(file);
} }

View File

@ -65,6 +65,7 @@
"skipLibCheck": true /* Skip type checking of declaration files. */, "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, "emitDecoratorMetadata": true,
"experimentalDecorators": true "experimentalDecorators": true,
"baseUrl": "."
} }
} }

View File

@ -4,7 +4,7 @@
/* Visit https://aka.ms/tsconfig.json to read more about this file */ /* Visit https://aka.ms/tsconfig.json to read more about this file */
/* Basic Options */ /* Basic Options */
"incremental": true, /* Enable incremental compilation */ "incremental": true /* Enable incremental compilation */,
"target": "ES6" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */, "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'. */, "module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */,
"lib": ["ES2015", "dom"] /* Specify library files to be included in the compilation. */, "lib": ["ES2015", "dom"] /* Specify library files to be included in the compilation. */,
@ -68,7 +68,7 @@
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */, "forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */,
"baseUrl": ".", "baseUrl": ".",
"paths": { "paths": {
"@fosscord/cdn/": ["src/index"], "@fosscord/cdn": ["src/index"],
"@fosscord/cdn/*": ["src/*"] "@fosscord/cdn/*": ["src/*"]
}, },
"plugins": [{ "transform": "@zerollup/ts-transform-paths" }] "plugins": [{ "transform": "@zerollup/ts-transform-paths" }]

View File

@ -7,11 +7,10 @@
"syntax": "typescript", "syntax": "typescript",
"decorators": true "decorators": true
}, },
"target": "es2021", "target": "es5",
"baseUrl": ".", "baseUrl": ".",
"paths": { "paths": {
"@fosscord/gateway": ["src/index"], "@fosscord/gateway": ["src/index"]
"@fosscord/gateway/*": ["src/*"]
} }
} }
} }

Binary file not shown.

View File

@ -5,7 +5,7 @@
/* Visit https://aka.ms/tsconfig.json to read more about this file */ /* Visit https://aka.ms/tsconfig.json to read more about this file */
/* Basic Options */ /* Basic Options */
"incremental": true, /* Enable incremental compilation */ "incremental": true /* Enable incremental compilation */,
"target": "ES2020" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */, "target": "ES2020" /* 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'. */, "module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */,
"lib": ["ES2015", "ES2020.BigInt", "DOM"] /* Specify library files to be included in the compilation. */, "lib": ["ES2015", "ES2020.BigInt", "DOM"] /* Specify library files to be included in the compilation. */,
@ -70,7 +70,7 @@
"resolveJsonModule": true, "resolveJsonModule": true,
"baseUrl": ".", "baseUrl": ".",
"paths": { "paths": {
"@fosscord/gateway": ["src/index"], "@fosscord/gateway": ["src/index.ts"],
"@fosscord/gateway/*": ["src/*"] "@fosscord/gateway/*": ["src/*"]
}, },
"plugins": [{ "transform": "@zerollup/ts-transform-paths" }] "plugins": [{ "transform": "@zerollup/ts-transform-paths" }]

View File

@ -1,18 +0,0 @@
{
"module": {
"type": "commonjs"
},
"jsc": {
"parser": {
"syntax": "typescript",
"decorators": true
},
"target": "es2021",
"transform": {
"legacyDecorator": true,
"decoratorMetadata": true
},
"keepClassNames": true,
"loose": true
}
}

View File

@ -1,12 +0,0 @@
{
"module": {
"type": "commonjs"
},
"jsc": {
"parser": {
"syntax": "typescript",
"decorators": true
},
"target": "es2021"
}
}