Skip to content

Support TS in sub packages #390

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"sideEffects": false,
"main": "dist/cjs/index.js",
"module": "dist/esm/index.js",
"types": "dist/index.d.ts",
"types": "dist/types/index.d.ts",
"homepage": "http://glmatrix.net",
"license": "MIT",
"bugs": {
Expand Down Expand Up @@ -34,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);