From 5d2e2a7fd03bf022dfe9fb8225a8da0de8f386fa Mon Sep 17 00:00:00 2001 From: juanelas Date: Tue, 3 Mar 2020 09:36:04 +0100 Subject: [PATCH] TypeScript definitions. Browser module defaults to minified ES6 --- README.md | 2 +- build/build.dts.js | 34 +++++++ package-lock.json | 6 ++ package.json | 10 +- src/readme-template.hbs | 2 +- test/browser/index.html | 2 +- types/bigint-crypto-utils.d.ts | 169 +++++++++++++++++++++++++++++++++ 7 files changed, 219 insertions(+), 6 deletions(-) create mode 100644 build/build.dts.js create mode 100644 types/bigint-crypto-utils.d.ts diff --git a/README.md b/README.md index a362aad..8729c28 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ bigint-crypto-utils can be imported to your project with `npm`: npm install bigint-crypto-utils ``` -NPM installation defaults to the ES6 module for browsers and the CJS one for Node.js. +NPM installation defaults to the minified ES6 module for browsers and the CJS one for Node.js. For web browsers, you can also directly download the [IIFE file](https://raw.githubusercontent.com/juanelas/bigint-crypto-utils/master/dist/bigint-crypto-utils-latest.browser.js) or the [ES6 module](https://raw.githubusercontent.com/juanelas/bigint-crypto-utils/master/dist/bigint-crypto-utils-latest.browser.mod.min.js) from GitHub. diff --git a/build/build.dts.js b/build/build.dts.js new file mode 100644 index 0000000..6b3abee --- /dev/null +++ b/build/build.dts.js @@ -0,0 +1,34 @@ +const fs = require('fs'); +const ts = require('typescript'); +const path = require('path'); +const pkgJson = require('../package.json'); + +const rootDir = path.join(__dirname, '..'); +const jsFile = path.join(rootDir, 'src', 'main.js'); +const dtsFile = path.join(rootDir, 'types', `${pkgJson.name}.d.ts`); + + +const compilerOptions = { + 'declaration': true, + 'noEmit': false, + 'emitDeclarationOnly': true, + 'allowJs': true +}; + +(function compile(jsFile, dtsFile, options) { // Create a Program with an in-memory emit + const host = ts.createCompilerHost(options); + // host.writeFile = (fileName, contents) => createdFiles[fileName] = contents; + host.writeFile = (fileName, contents) => { + fs.writeFileSync(dtsFile, contents); + }; + + // Prepare and emit the d.ts files + const program = ts.createProgram([jsFile], options, host); + program.emit(); + + // Loop through all the input files + // fileNames.forEach(file => { + // console.log("### JavaScript\n"); + // console.log(host.readFile(file)); + // }); +})(jsFile, dtsFile, compilerOptions); diff --git a/package-lock.json b/package-lock.json index 3d10de7..796e03e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3211,6 +3211,12 @@ "integrity": "sha512-4dbzIzqvjtgiM5rw1k5rEHtBANKmdudhGyBEajN01fEyhaAIhsoKNy6y7+IN93IfpFtwY9iqi7kD+xwKhQsNJA==", "dev": true }, + "typescript": { + "version": "3.8.3", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-3.8.3.tgz", + "integrity": "sha512-MYlEfn5VrLNsgudQTVJeNaQFUAI7DkhnOjdpAp4T+ku1TfQClewlbSuTVHiA+8skNBgaf02TL/kLOvig4y3G8w==", + "dev": true + }, "typical": { "version": "2.6.1", "resolved": "https://registry.npmjs.org/typical/-/typical-2.6.1.tgz", diff --git a/package.json b/package.json index b3641d1..26ed39d 100644 --- a/package.json +++ b/package.json @@ -20,18 +20,21 @@ }, "repository": "github:juanelas/bigint-crypto-utils", "main": "./dist/bigint-crypto-utils-latest.node.js", - "browser": "./dist/bigint-crypto-utils-latest.browser.mod.js", + "browser": "./dist/bigint-crypto-utils-latest.browser.mod.min.js", + "types": "./types/bigint-crypto-utils.d.ts", "directories": { "build": "./build", "dist": "./dist", "src": "./src", - "test": "./test" + "test": "./test", + "types": "/types" }, "scripts": { "test": "mocha --timeout 600000", "build:js": "node build/build.rollup.js", "build:browserTests": "node build/build.browser.tests.js", "build:docs": "jsdoc2md --template=./src/readme-template.hbs --files ./src/main.js > README.md", + "build:dts": "node build/build.dts.js", "build": "run-s build:**", "prepublishOnly": "npm run build" }, @@ -46,6 +49,7 @@ "rollup-plugin-babel-minify": "^9.1.0", "rollup-plugin-commonjs": "^10.1.0", "rollup-plugin-multi-entry": ">=2.1.0", - "rollup-plugin-node-resolve": ">=5.2.0" + "rollup-plugin-node-resolve": ">=5.2.0", + "typescript": "^3.8.3" } } diff --git a/src/readme-template.hbs b/src/readme-template.hbs index 8cfb5b2..b4b549a 100644 --- a/src/readme-template.hbs +++ b/src/readme-template.hbs @@ -14,7 +14,7 @@ bigint-crypto-utils can be imported to your project with `npm`: npm install bigint-crypto-utils ``` -NPM installation defaults to the ES6 module for browsers and the CJS one for Node.js. +NPM installation defaults to the minified ES6 module for browsers and the CJS one for Node.js. For web browsers, you can also directly download the [IIFE file](https://raw.githubusercontent.com/juanelas/bigint-crypto-utils/master/dist/bigint-crypto-utils-latest.browser.js) or the [ES6 module](https://raw.githubusercontent.com/juanelas/bigint-crypto-utils/master/dist/bigint-crypto-utils-latest.browser.mod.min.js) from GitHub. diff --git a/test/browser/index.html b/test/browser/index.html index 1660a98..fa25c43 100644 --- a/test/browser/index.html +++ b/test/browser/index.html @@ -12,7 +12,7 @@