-
Notifications
You must be signed in to change notification settings - Fork 724
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ref #389 Emitted all types to directory instead of single file. This way each sub package can specify own "types" field. Though there are problems - tsc do not emit types imports in dts files I solved by manual prepending import from types ``` import { mat2, mat2d, mat3, mat4, quat, quat2, vec2, vec3, vec4, ReadonlyMat2, ReadonlyMat2d, ReadonlyMat3, ReadonlyMat4, ReadonlyQuat, ReadonlyQuat2, ReadonlyVec2, ReadonlyVec3, ReadonlyVec4 } from './types'; /** * Quaternion * @module quat */ /** * Creates a new identity quat * * @returns {quat} a new quaternion */ export function create(): quat; ``` - tsc do not resolve reused function from another module so we end with this ``` export const fromValues: typeof vec4.fromValues; // 2693: 'vec4' only refers to a type, but is being used as a value here. ``` - even if we write all imports from another module we have a conflict between module names and types ``` import { ..., vec2, ... } from './types'; export const fromValues: typeof vec4.fromValues; ```
- Loading branch information
Showing
3 changed files
with
25 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,22 @@ | ||
const fs = require("fs"); | ||
const path = require("path"); | ||
|
||
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 module 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 module $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"); | ||
let sourceDir = './dist/types' | ||
let typesSource = fs.readFileSync("./src/types.d.ts", "utf-8"); | ||
let typesResult = typesSource.replace(/declare/g, "export"); | ||
let typesExports = []; | ||
for (const [,exportName] of typesResult.matchAll(/\bexport\s+\w+\s+(\w+)\b/g)) { | ||
typesExports.push(exportName); | ||
} | ||
|
||
for (let sourceFile of fs.readdirSync(sourceDir)) { | ||
let sourcePath = path.join(sourceDir, sourceFile); | ||
let typings = fs.readFileSync(sourcePath, "utf-8"); | ||
if (sourceFile.includes('index') === false) { | ||
typings = `import { ${typesExports.join(', ')} } from './types';\n` + typings; | ||
} | ||
fs.writeFileSync(sourcePath, typings, "utf-8"); | ||
} | ||
|
||
// write after to prevent reading above | ||
fs.writeFileSync(path.join(sourceDir, 'types.d.ts'), typesResult); |