Skip to content

Commit

Permalink
Support TS in sub packages
Browse files Browse the repository at this point in the history
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
TrySound committed May 9, 2020
1 parent 87231b9 commit 88d42af
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 57 deletions.
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"sideEffects": false,
"main": "dist/cjs/index.js",
"module": "dist/esm/index.js",
"types": "dist/types/index.d.ts",
"homepage": "http://glmatrix.net",
"license": "MIT",
"bugs": {
Expand Down Expand Up @@ -33,15 +34,17 @@
"build-umd": "rollup -c",
"build-esm": "cross-env BABEL_ENV=esm babel src -d dist/esm",
"build-cjs": "babel src -d dist/cjs",
"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",
"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",
"build-dts": "tsc --allowJs --declaration --emitDeclarationOnly --module amd --outDir dist/types ./src/index.js ./src/types.d.ts && node ./utils/bundle-dts.js",
"build-pick": "cherry-pick --cwd dist --input-dir ../src --esm-dir esm --cjs-dir cjs --types-dir types",
"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",
"prepare": "npm run build"
},
"devDependencies": {
"@babel/cli": "^7.8.4",
"@babel/core": "^7.9.0",
"@babel/preset-env": "^7.9.0",
"@babel/register": "^7.9.0",
"cherry-pick": "^0.5.0",
"cross-env": "^7.0.2",
"del-cli": "^3.0.0",
"jsdoc": "^3.6.3",
Expand Down
19 changes: 1 addition & 18 deletions utils/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,8 @@ delete pkg.scripts;
delete pkg.devDependencies;
pkg.main = 'cjs/index.js'
pkg.module = 'esm/index.js'
pkg.types = 'types/index.d.ts'
fs.writeFileSync('dist/package.json', JSON.stringify(pkg, null, 2));

copyFileSync('README.md', 'dist/README.md');
copyFileSync('LICENSE.md', 'dist/LICENSE.md');

const files = fs.readdirSync('src')
.filter(file => !file.includes('common') && !file.includes('index'))
.forEach(file => {
const name = file.endsWith('.js') ? file.slice(0, -3) : file;
const filePkg = {
name: `gl-matrix/${name}`,
main: `../cjs/${file}`,
module: `../esm/${file}`,
};
if(!fs.existsSync(`dist/${name}`)) {
fs.mkdirSync(`dist/${name}`);
}
fs.writeFileSync(
`dist/${name}/package.json`,
JSON.stringify(filePkg, null, 2)
);
});
56 changes: 19 additions & 37 deletions utils/bundle-dts.js
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);

0 comments on commit 88d42af

Please sign in to comment.