-
Notifications
You must be signed in to change notification settings - Fork 725
/
Copy pathbundle-dts.js
39 lines (29 loc) · 1.21 KB
/
bundle-dts.js
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
import fs from "fs";
let sourcePath = "./dist/index.d.ts";
let sourceTypingsPath = "./src/types.d.ts";
let sourceTypings = fs.readFileSync(sourceTypingsPath, "utf-8");
let typings = fs.readFileSync(sourcePath, "utf-8");
let typingsLength = typings.length;
// Honestly, this is just a horrible hack.
// Remove index module at the end
typings = typings.replace(/declare module "index" {([^]+?)\n}/, "");
if (typingsLength == typings.length)
throw new Error(
"An index module should have been generated and then replaced"
);
// Rename common module to glMatrix
typings = typings.replace(
'declare module "common" {',
"export namespace glMatrix {"
);
// Replace imports from other modules with direct references
typings = typings.replace(/import\("([^"]+?)(\.js)?"\)/g, "$1");
// Replace imports with nothing
typings = typings.replace(/ *import.+from.*;/g, "");
// Replace declare module with exports
typings = typings.replace(/declare module "([^"]+?)" {/g, "export namespace $1 {");
// Add types
typings = "\n" + sourceTypings.replace(/declare/g, "export") + "\n" + typings;
// Wrap them in a "gl-matrix module"
typings = 'declare module "gl-matrix" {\n' + typings + "\n}";
fs.writeFileSync(sourcePath, typings, "utf-8");