bigint-crypto-utils/build/build.browser.tests.js

96 lines
3.0 KiB
JavaScript
Raw Normal View History

2019-04-19 07:42:28 +00:00
'use strict';
const rollup = require('rollup');
const resolve = require('@rollup/plugin-node-resolve');
const replace = require('@rollup/plugin-replace');
const commonjs = require('@rollup/plugin-commonjs');
const multi = require('@rollup/plugin-multi-entry');
2019-04-19 07:42:28 +00:00
const fs = require('fs');
const path = require('path');
const pkgJson = require('../package.json');
const pkg_name = pkgJson.name;
const mocha_version = pkgJson.devDependencies.mocha.replace(/[\^~*><=]/g, '');
const chai_version = pkgJson.devDependencies.chai.replace(/[\^~*><=]/g, '');
2019-04-19 07:42:28 +00:00
const rootDir = path.join(__dirname, '..');
// Let's first create the appropriate html file loading mocha, chai and a bundle of the tests
const templatePath = path.join(rootDir, pkgJson.directories.src, 'browser-test-template.html');
const dstDir = path.join(rootDir, pkgJson.directories.test, 'browser');
2019-04-19 07:42:28 +00:00
const dstFileName = path.join(dstDir, 'index.html');
const template = fs.readFileSync(templatePath, 'utf-8');
const bundleFile = path.join(rootDir, pkgJson.directories.dist, pkgJson.name + '-latest.browser.mod.min.js');
2019-04-19 07:42:28 +00:00
const testsJs = `
<script type="module">
import * as ${camelise(pkg_name)} from '${path.relative(templatePath, bundleFile)}'
window.${camelise(pkg_name)} = ${camelise(pkg_name)};
2019-04-19 07:42:28 +00:00
import './tests.js';
mocha.run();
</script>
`;
fs.writeFileSync(dstFileName,
template.replace(/{{TESTS}}/g, testsJs).replace(/{{PKG_NAME}}/g, pkgJson.name).replace(/{{MOCHA_VERSION}}/g, mocha_version).replace(/{{CHAI_VERSION}}/g, chai_version)
);
2019-04-19 07:42:28 +00:00
/*
Now we create a bundle of all the tests called test.js
require/import of the module and chai are removed since they will be loaded from the html file
*/
const distFile = path.relative(path.join(rootDir, 'test'), pkgJson.main);
const requireModuleLine = `const ${camelise(pkgJson.name)} = require('${path.join(path.dirname(distFile), path.basename(distFile, '.js'))}');`;
const buildOptions = [
{ // Browser
input: {
input: path.join(rootDir, 'test', '*.js'),
external: ['chai'],
plugins: [
multi({ exports: false }),
2019-04-19 07:42:28 +00:00
replace({
'const chai = require(\'chai\');': '',
[requireModuleLine]: '',
'delimiters': ['', ''],
'process.browser': true
}),
resolve({
browser: true,
}),
commonjs()
],
},
output: {
file: path.join(rootDir, 'test', 'browser', 'tests.js'),
format: 'esm'
}
}
];
for (const options of buildOptions) {
build(options);
}
/* --- HELPLER FUNCTIONS --- */
async function build(options) {
// create a bundle
const bundle = await rollup.rollup(options.input);
// generate code
await bundle.generate(options.output);
// or write the bundle to disk
await bundle.write(options.output);
}
function camelise(str) {
return str.replace(/-([a-z])/g,
function (m, w) {
return w.toUpperCase();
});
}