forked from colyseus/colyseus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.ts
121 lines (101 loc) · 3.62 KB
/
build.ts
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
import fs from 'fs';
import path from 'path';
import glob from 'fast-glob';
import { fileURLToPath } from 'url';
import minimist from 'minimist';
import ts from "typescript";
import { getPackages } from '@lerna/project';
import { filterPackages } from '@lerna/filter-packages';
import esbuild from "esbuild";
// we need to change up how __dirname is used for ES6 purposes
const __dirname = path.dirname(fileURLToPath(import.meta.url));
/**
* Get a list of the non-private sorted packages
*/
async function getAllPackages(scope, ignore) {
const packages = await getPackages(__dirname);
return filterPackages(packages, scope, ignore, false);
}
async function main() {
// Support --scope and --ignore globs if passed in via commandline
const argv = minimist(process.argv.slice(2));
const packages = await getAllPackages(argv.scope, argv.ignore);
packages.map(pkg => {
// Absolute path to package directory
const basePath = path.relative(__dirname, pkg.location);
// "main" field from package.json file.
const pkgJSON = pkg.toJSON();
// Skip rollup build if package has "build" configured.
if (pkgJSON.scripts?.build) {
console.log(pkgJSON.name, "has custom build! skipping default build.");
return;
}
// Copy README.md and LICENSE into child package folder.
if (!fs.existsSync(path.join(basePath, "README.md"))) {
fs.copyFileSync(path.resolve(__dirname, "README.md"), path.join(basePath, "README.md"));
fs.copyFileSync(path.resolve(__dirname, "LICENSE"), path.join(basePath, "LICENSE"));
}
// Get all .ts as input files
const entrypoints = glob.sync(path.resolve(basePath, "src", "**", "**.ts")
.replace(/\\/g, '/')); // windows support
const outdir = path.join(basePath, 'build');
// Emit only .d.ts files
const emitTSDeclaration = () => {
console.log("Generating .d.ts files for...", pkgJSON.name);
const program = ts.createProgram(entrypoints, {
declaration: true,
emitDeclarationOnly: true,
resolveJsonModule: true,
skipLibCheck: true,
module: ts.ModuleKind.CommonJS,
target: ts.ScriptTarget.ES2015,
lib: ["lib.es2022.d.ts"],
outDir: outdir,
downlevelIteration: true, // (redis-driver)
esModuleInterop: true,
experimentalDecorators: true,
});
const emitResult = program.emit();
const allDiagnostics = ts
.getPreEmitDiagnostics(program)
.concat(emitResult.diagnostics);
allDiagnostics.forEach(diagnostic => {
if (diagnostic.file) {
const { line, character } = ts.getLineAndCharacterOfPosition(diagnostic.file, diagnostic.start);
const message = ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n");
console.log(`${diagnostic.file.fileName} (${line + 1},${character + 1}): ${message}`);
} else {
console.log(ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n"));
}
});
}
// CommonJS output
esbuild.build({
entryPoints: entrypoints,
outdir,
format: "cjs",
sourcemap: "external",
platform: "node",
watch: argv.watch,
});
// ESM output
esbuild.build({
entryPoints: entrypoints,
outdir,
format: "esm",
sourcemap: "external",
platform: "node",
outExtension: { '.js': '.mjs', },
watch: argv.watch && {
onRebuild(err, result) {
if (err) { return console.error(err); }
// re-emit .d.ts files
emitTSDeclaration();
}
},
});
// emit .d.ts files
emitTSDeclaration();
});
}
export default await main();