Skip to content

Commit b5952f8

Browse files
committed
Support TS in sub packages
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; ```
1 parent 139f77f commit b5952f8

File tree

3 files changed

+25
-58
lines changed

3 files changed

+25
-58
lines changed

package.json

+5-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"sideEffects": false,
77
"main": "dist/cjs/index.js",
88
"module": "dist/esm/index.js",
9-
"types": "dist/index.d.ts",
9+
"types": "dist/types/index.d.ts",
1010
"homepage": "http://glmatrix.net",
1111
"license": "MIT",
1212
"bugs": {
@@ -34,15 +34,17 @@
3434
"build-umd": "rollup -c",
3535
"build-esm": "cross-env BABEL_ENV=esm babel src -d dist/esm",
3636
"build-cjs": "babel src -d dist/cjs",
37-
"build-dts": "tsc --allowJs --declaration --emitDeclarationOnly --module amd --outFile ./dist/index.d.ts ./src/index.js ./src/types.d.ts && node ./utils/bundle-dts.js && tsc --noEmit ./dist/index.d.ts",
38-
"build": "del dist && npm run update-license-version && npm run build-umd && npm run build-esm && npm run build-cjs && npm run build-dts && node ./utils/build.js",
37+
"build-dts": "tsc --allowJs --declaration --emitDeclarationOnly --module amd --outDir dist/types ./src/index.js ./src/types.d.ts && node ./utils/bundle-dts.js",
38+
"build-pick": "cherry-pick --cwd dist --input-dir ../src --esm-dir esm --cjs-dir cjs --types-dir types",
39+
"build": "del dist && npm run update-license-version && npm run build-umd && npm run build-esm && npm run build-cjs && npm run build-dts && npm run build-pick && node ./utils/build.js",
3940
"prepare": "npm run build"
4041
},
4142
"devDependencies": {
4243
"@babel/cli": "^7.8.4",
4344
"@babel/core": "^7.9.0",
4445
"@babel/preset-env": "^7.9.0",
4546
"@babel/register": "^7.9.0",
47+
"cherry-pick": "^0.5.0",
4648
"cross-env": "^7.0.2",
4749
"del-cli": "^3.0.0",
4850
"jsdoc": "^3.6.3",

utils/build.js

+1-18
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,8 @@ delete pkg.scripts;
1212
delete pkg.devDependencies;
1313
pkg.main = 'cjs/index.js'
1414
pkg.module = 'esm/index.js'
15+
pkg.types = 'types/index.d.ts'
1516
fs.writeFileSync('dist/package.json', JSON.stringify(pkg, null, 2));
1617

1718
copyFileSync('README.md', 'dist/README.md');
1819
copyFileSync('LICENSE.md', 'dist/LICENSE.md');
19-
20-
const files = fs.readdirSync('src')
21-
.filter(file => !file.includes('common') && !file.includes('index'))
22-
.forEach(file => {
23-
const name = file.endsWith('.js') ? file.slice(0, -3) : file;
24-
const filePkg = {
25-
name: `gl-matrix/${name}`,
26-
main: `../cjs/${file}`,
27-
module: `../esm/${file}`,
28-
};
29-
if(!fs.existsSync(`dist/${name}`)) {
30-
fs.mkdirSync(`dist/${name}`);
31-
}
32-
fs.writeFileSync(
33-
`dist/${name}/package.json`,
34-
JSON.stringify(filePkg, null, 2)
35-
);
36-
});

utils/bundle-dts.js

+19-37
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,22 @@
11
const fs = require("fs");
22
const path = require("path");
33

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

Comments
 (0)