diff --git a/package.json b/package.json index c1ac92e0..41635c79 100644 --- a/package.json +++ b/package.json @@ -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": { @@ -33,8 +34,9 @@ "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": { @@ -42,6 +44,7 @@ "@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", diff --git a/utils/build.js b/utils/build.js index 5d28d06a..0b68da4b 100644 --- a/utils/build.js +++ b/utils/build.js @@ -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) - ); - }); diff --git a/utils/bundle-dts.js b/utils/bundle-dts.js index c7bcd554..9077d608 100644 --- a/utils/bundle-dts.js +++ b/utils/bundle-dts.js @@ -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);