|
1 | 1 | const fs = require("fs");
|
2 | 2 | const path = require("path");
|
3 | 3 |
|
4 |
| -let sourcePath = "./dist/index.d.ts"; |
5 |
| -let sourceTypingsPath = "./src/types.d.ts"; |
6 |
| -let sourceTypings = fs.readFileSync(sourceTypingsPath, "utf-8"); |
7 |
| -let typings = fs.readFileSync(sourcePath, "utf-8"); |
8 |
| -let typingsLength = typings.length; |
9 |
| - |
10 |
| -// Honestly, this is just a horrible hack. |
11 |
| - |
12 |
| -// Remove index module at the end |
13 |
| -typings = typings.replace(/declare module "index" {([^]+?)\n}/, ""); |
14 |
| -if (typingsLength == typings.length) |
15 |
| - throw new Error( |
16 |
| - "An index module should have been generated and then replaced" |
17 |
| - ); |
18 |
| - |
19 |
| -// Rename common module to glMatrix |
20 |
| -typings = typings.replace( |
21 |
| - 'declare module "common" {', |
22 |
| - "export module glMatrix {" |
23 |
| -); |
24 |
| - |
25 |
| -// Replace imports from other modules with direct references |
26 |
| -typings = typings.replace(/import\("([^"]+?)(\.js)?"\)/g, "$1"); |
27 |
| - |
28 |
| -// Replace imports with nothing |
29 |
| -typings = typings.replace(/ *import.+from.*;/g, ""); |
30 |
| - |
31 |
| -// Replace declare module with exports |
32 |
| -typings = typings.replace(/declare module "([^"]+?)" {/g, "export module $1 {"); |
33 |
| - |
34 |
| -// Add types |
35 |
| -typings = "\n" + sourceTypings.replace(/declare/g, "export") + "\n" + typings; |
36 |
| - |
37 |
| -// Wrap them in a "gl-matrix module" |
38 |
| -typings = 'declare module "gl-matrix" {\n' + typings + "\n}"; |
39 |
| - |
40 |
| -fs.writeFileSync(sourcePath, typings, "utf-8"); |
| 4 | +let sourceDir = './dist/types' |
| 5 | +let typesSource = fs.readFileSync("./src/types.d.ts", "utf-8"); |
| 6 | +let typesResult = typesSource.replace(/declare/g, "export"); |
| 7 | +let typesExports = []; |
| 8 | +for (const [,exportName] of typesResult.matchAll(/\bexport\s+\w+\s+(\w+)\b/g)) { |
| 9 | + typesExports.push(exportName); |
| 10 | +} |
| 11 | + |
| 12 | +for (let sourceFile of fs.readdirSync(sourceDir)) { |
| 13 | + let sourcePath = path.join(sourceDir, sourceFile); |
| 14 | + let typings = fs.readFileSync(sourcePath, "utf-8"); |
| 15 | + if (sourceFile.includes('index') === false) { |
| 16 | + typings = `import { ${typesExports.join(', ')} } from './types';\n` + typings; |
| 17 | + } |
| 18 | + fs.writeFileSync(sourcePath, typings, "utf-8"); |
| 19 | +} |
| 20 | + |
| 21 | +// write after to prevent reading above |
| 22 | +fs.writeFileSync(path.join(sourceDir, 'types.d.ts'), typesResult); |
0 commit comments