issue #3 fixed. Better building process in general

This commit is contained in:
juanelas 2020-03-15 17:06:00 +01:00
parent ccd81abae1
commit df7019fa68
8 changed files with 360 additions and 878 deletions

View File

@ -1,10 +1,10 @@
'use strict'; 'use strict';
const rollup = require('rollup'); const rollup = require('rollup');
const resolve = require('@rollup/plugin-node-resolve');
const replace = require('@rollup/plugin-replace'); const replace = require('@rollup/plugin-replace');
const resolve = require('rollup-plugin-node-resolve'); const commonjs = require('@rollup/plugin-commonjs');
const commonjs = require('rollup-plugin-commonjs'); const multi = require('@rollup/plugin-multi-entry');
const multiEntry = require('rollup-plugin-multi-entry');
const fs = require('fs'); const fs = require('fs');
const path = require('path'); const path = require('path');
@ -17,14 +17,15 @@ const rootDir = path.join(__dirname, '..');
// Let's first create the appropriate html file loading mocha, chai and a bundle of the tests // Let's first create the appropriate html file loading mocha, chai and a bundle of the tests
const templatePath = path.join(rootDir, 'src', 'browser-test-template.html'); const templatePath = path.join(rootDir, pkgJson.directories.src, 'browser-test-template.html');
const dstDir = path.join(rootDir, 'test', 'browser'); const dstDir = path.join(rootDir, pkgJson.directories.test, 'browser');
const dstFileName = path.join(dstDir, 'index.html'); const dstFileName = path.join(dstDir, 'index.html');
const template = fs.readFileSync(templatePath, 'utf-8'); const template = fs.readFileSync(templatePath, 'utf-8');
const bundleFile = path.join(rootDir, pkgJson.directories.dist, pkgJson.name + '-latest.browser.mod.min.js');
const testsJs = ` const testsJs = `
<script type="module"> <script type="module">
import * as ${camelise(pkg_name)} from '${path.relative(templatePath, pkgJson.browser)}' import * as ${camelise(pkg_name)} from '${path.relative(templatePath, bundleFile)}'
window.${camelise(pkg_name)} = ${camelise(pkg_name)}; window.${camelise(pkg_name)} = ${camelise(pkg_name)};
import './tests.js'; import './tests.js';
mocha.run(); mocha.run();
@ -48,7 +49,7 @@ const buildOptions = [
input: path.join(rootDir, 'test', '*.js'), input: path.join(rootDir, 'test', '*.js'),
external: ['chai'], external: ['chai'],
plugins: [ plugins: [
multiEntry({ exports: false }), multi({ exports: false }),
replace({ replace({
'const chai = require(\'chai\');': '', 'const chai = require(\'chai\');': '',
[requireModuleLine]: '', [requireModuleLine]: '',

View File

@ -4,7 +4,7 @@ const path = require('path');
const pkgJson = require('../package.json'); const pkgJson = require('../package.json');
const rootDir = path.join(__dirname, '..'); const rootDir = path.join(__dirname, '..');
const jsFile = path.join(rootDir, 'src', 'main.js'); const jsFile = path.join(rootDir, pkgJson.browser);
const dtsFile = path.join(rootDir, 'types', `${pkgJson.name}.d.ts`); const dtsFile = path.join(rootDir, 'types', `${pkgJson.name}.d.ts`);

View File

@ -2,7 +2,7 @@
const rollup = require('rollup'); const rollup = require('rollup');
const replace = require('@rollup/plugin-replace'); const replace = require('@rollup/plugin-replace');
const minify = require('rollup-plugin-babel-minify'); const terser = require('rollup-plugin-terser');
const fs = require('fs'); const fs = require('fs');
const path = require('path'); const path = require('path');
const pkgJson = require('../package.json'); const pkgJson = require('../package.json');
@ -27,24 +27,26 @@ const buildOptions = [
name: camelise(pkgJson.name) name: camelise(pkgJson.name)
} }
}, },
// { // Browser minified { // Browser minified
// input: { input: {
// input: path.join(srcDir, 'main.js'), input: path.join(srcDir, 'main.js'),
// plugins: [ plugins: [
// replace({ replace({
// 'process.browser': true 'process.browser': true
// }), })
// minify({ ],
// 'comments': false },
// }) output: {
// ], file: path.join(dstDir, `${pkgJson.name}-${pkgJson.version}.browser.min.js`),
// }, format: 'iife',
// output: { name: camelise(pkgJson.name),
// file: path.join(dstDir, `${pkgJson.name}-${pkgJson.version}.browser.min.js`), plugins: [
// format: 'iife', terser.terser({
// name: camelise(pkgJson.name) 'mangle': false
// } })
// }, ]
}
},
{ // Browser esm { // Browser esm
input: { input: {
input: path.join(srcDir, 'main.js'), input: path.join(srcDir, 'main.js'),
@ -65,15 +67,17 @@ const buildOptions = [
plugins: [ plugins: [
replace({ replace({
'process.browser': true 'process.browser': true
}),
minify({
'comments': false
}) })
], ],
}, },
output: { output: {
file: path.join(dstDir, `${pkgJson.name}-${pkgJson.version}.browser.mod.min.js`), file: path.join(dstDir, `${pkgJson.name}-${pkgJson.version}.browser.mod.min.js`),
format: 'esm' format: 'esm',
plugins: [
terser.terser({
'mangle': false
})
]
} }
}, },
{ // Node { // Node

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

974
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -39,17 +39,17 @@
"prepublishOnly": "npm run build" "prepublishOnly": "npm run build"
}, },
"devDependencies": { "devDependencies": {
"@rollup/plugin-commonjs": "^11.0.2",
"@rollup/plugin-multi-entry": "^3.0.0",
"@rollup/plugin-node-resolve": "^7.1.1",
"@rollup/plugin-replace": "^2.3.1", "@rollup/plugin-replace": "^2.3.1",
"chai": ">=4.2.0", "chai": ">=4.2.0",
"eslint": "^6.8.0", "eslint": "^6.8.0",
"jsdoc-to-markdown": "^5.0.3", "jsdoc-to-markdown": "^5.0.3",
"mocha": "^7.1.0", "mocha": "^7.1.0",
"npm-run-all": "^4.1.5", "npm-run-all": "^4.1.5",
"rollup": "^1.32.0", "rollup": "^2.0.6",
"rollup-plugin-babel-minify": "^9.1.0", "rollup-plugin-terser": "^5.3.0",
"rollup-plugin-commonjs": "^10.1.0",
"rollup-plugin-multi-entry": ">=2.1.0",
"rollup-plugin-node-resolve": ">=5.2.0",
"typescript": "^3.8.3" "typescript": "^3.8.3"
} }
} }

View File

@ -1,169 +1,21 @@
/** export function abs(a: any): any;
* Absolute value. abs(a)==a if a>=0. abs(a)==-a if a<0 export function bitLength(a: any): number;
* export function eGcd(a: any, b: any): number | {
* @param {number|bigint} a b: any;
*
* @returns {bigint} the absolute value of a
*/
export function abs(a: number | bigint): bigint;
/**
* Returns the bitlength of a number
*
* @param {number|bigint} a
* @returns {number} - the bit length
*/
export function bitLength(a: number | bigint): number;
/**
* @typedef {Object} egcdReturn A triple (g, x, y), such that ax + by = g = gcd(a, b).
* @property {bigint} g
* @property {bigint} x
* @property {bigint} y
*/
/**
* An iterative implementation of the extended euclidean algorithm or extended greatest common divisor algorithm.
* Take positive integers a, b as input, and return a triple (g, x, y), such that ax + by = g = gcd(a, b).
*
* @param {number|bigint} a
* @param {number|bigint} b
*
* @returns {egcdReturn} A triple (g, x, y), such that ax + by = g = gcd(a, b).
*/
export function eGcd(a: number | bigint, b: number | bigint): egcdReturn;
/**
* Greatest-common divisor of two integers based on the iterative binary algorithm.
*
* @param {number|bigint} a
* @param {number|bigint} b
*
* @returns {bigint} The greatest common divisor of a and b
*/
export function gcd(a: number | bigint, b: number | bigint): bigint;
/**
* The test first tries if any of the first 250 small primes are a factor of the input number and then passes several
* iterations of Miller-Rabin Probabilistic Primality Test (FIPS 186-4 C.3.1)
*
* @param {number|bigint} w An integer to be tested for primality
* @param {number} [iterations = 16] The number of iterations for the primality test. The value shall be consistent with Table C.1, C.2 or C.3
*
* @return {Promise} A promise that resolves to a boolean that is either true (a probably prime number) or false (definitely composite)
*/
export function isProbablyPrime(w: number | bigint, iterations?: number): Promise<any>;
/**
* The least common multiple computed as abs(a*b)/gcd(a,b)
* @param {number|bigint} a
* @param {number|bigint} b
*
* @returns {bigint} The least common multiple of a and b
*/
export function lcm(a: number | bigint, b: number | bigint): bigint;
/**
* Maximum. max(a,b)==a if a>=b. max(a,b)==b if a<=b
*
* @param {number|bigint} a
* @param {number|bigint} b
*
* @returns {bigint} maximum of numbers a and b
*/
export function max(a: number | bigint, b: number | bigint): bigint;
/**
* Minimum. min(a,b)==b if a>=b. min(a,b)==a if a<=b
*
* @param {number|bigint} a
* @param {number|bigint} b
*
* @returns {bigint} minimum of numbers a and b
*/
export function min(a: number | bigint, b: number | bigint): bigint;
/**
* Modular inverse.
*
* @param {number|bigint} a The number to find an inverse for
* @param {number|bigint} n The modulo
*
* @returns {bigint} the inverse modulo n or NaN if it does not exist
*/
export function modInv(a: number | bigint, n: number | bigint): bigint;
/**
* Modular exponentiation b**e mod n. Currently using the right-to-left binary method
*
* @param {number|bigint} b base
* @param {number|bigint} e exponent
* @param {number|bigint} n modulo
*
* @returns {bigint} b**e mod n
*/
export function modPow(b: number | bigint, e: number | bigint, n: number | bigint): bigint;
/**
* A probably-prime (Miller-Rabin), cryptographically-secure, random-number generator.
* The browser version uses web workers to parallelise prime look up. Therefore, it does not lock the UI
* main process, and it can be much faster (if several cores or cpu are available).
* The node version can also use worker_threads if they are available (enabled by default with Node 11 and
* and can be enabled at runtime executing node --experimental-worker with node >=10.5.0).
*
* @param {number} bitLength The required bit length for the generated prime
* @param {number} [iterations = 16] The number of iterations for the Miller-Rabin Probabilistic Primality Test
*
* @returns {Promise} A promise that resolves to a bigint probable prime of bitLength bits.
*/
export function prime(bitLength: number, iterations?: number): Promise<any>;
/**
* A probably-prime (Miller-Rabin), cryptographically-secure, random-number generator.
* The sync version is NOT RECOMMENDED since it won't use workers and thus it'll be slower and may freeze thw window in browser's javascript. Please consider using prime() instead.
*
* @param {number} bitLength The required bit length for the generated prime
* @param {number} [iterations = 16] The number of iterations for the Miller-Rabin Probabilistic Primality Test
*
* @returns {bigint} A bigint probable prime of bitLength bits.
*/
export function primeSync(bitLength: number, iterations?: number): bigint;
/**
* Returns a cryptographically secure random integer between [min,max]
* @param {bigint} max Returned value will be <= max
* @param {bigint} [min = BigInt(1)] Returned value will be >= min
*
* @returns {bigint} A cryptographically secure random bigint between [min,max]
*/
export function randBetween(max: bigint, min?: bigint): bigint;
/**
* Secure random bits for both node and browsers. Node version uses crypto.randomFill() and browser one self.crypto.getRandomValues()
*
* @param {number} bitLength The desired number of random bits
* @param {boolean} [forceLength = false] If we want to force the output to have a specific bit length. It basically forces the msb to be 1
*
* @returns {Buffer|Uint8Array} A Buffer/UInt8Array (Node.js/Browser) filled with cryptographically secure random bits
*/
export function randBits(bitLength: number, forceLength?: boolean): Uint8Array | Buffer;
/**
* Secure random bytes for both node and browsers. Node version uses crypto.randomFill() and browser one self.crypto.getRandomValues()
*
* @param {number} byteLength The desired number of random bytes
* @param {boolean} [forceLength = false] If we want to force the output to have a bit length of 8*byteLength. It basically forces the msb to be 1
*
* @returns {Promise} A promise that resolves to a Buffer/UInt8Array (Node.js/Browser) filled with cryptographically secure random bytes
*/
export function randBytes(byteLength: number, forceLength?: boolean): Promise<any>;
/**
* Secure random bytes for both node and browsers. Node version uses crypto.randomFill() and browser one self.crypto.getRandomValues()
*
* @param {number} byteLength The desired number of random bytes
* @param {boolean} [forceLength = false] If we want to force the output to have a bit length of 8*byteLength. It basically forces the msb to be 1
*
* @returns {Buffer|Uint8Array} A Buffer/UInt8Array (Node.js/Browser) filled with cryptographically secure random bytes
*/
export function randBytesSync(byteLength: number, forceLength?: boolean): Uint8Array | Buffer;
/**
* Finds the smallest positive element that is congruent to a in modulo n
* @param {number|bigint} a An integer
* @param {number|bigint} n The modulo
*
* @returns {bigint} The smallest positive representation of a in modulo n
*/
export function toZn(a: number | bigint, n: number | bigint): bigint;
/**
* A triple (g, x, y), such that ax + by = g = gcd(a, b).
*/
export type egcdReturn = {
g: bigint;
x: bigint; x: bigint;
y: bigint; y: bigint;
}; };
export function gcd(a: any, b: any): any;
export function isProbablyPrime(w: any, iterations?: number): Promise<any>;
export function lcm(a: any, b: any): number | bigint;
export function max(a: any, b: any): any;
export function min(a: any, b: any): any;
export function modInv(a: any, n: any): any;
export function modPow(b: any, e: any, n: any): any;
export function prime(bitLength: any, iterations?: number): Promise<any>;
export function primeSync(bitLength: any, iterations?: number): bigint;
export function randBetween(max: any, min?: bigint): bigint;
export function randBits(bitLength: any, forceLength?: boolean): Uint8Array;
export function randBytes(byteLength: any, forceLength?: boolean): Promise<any>;
export function randBytesSync(byteLength: any, forceLength?: boolean): Uint8Array;
export function toZn(a: any, n: any): any;