2019-04-19 07:42:28 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const rollup = require('rollup');
|
2020-03-15 16:06:00 +00:00
|
|
|
const resolve = require('@rollup/plugin-node-resolve');
|
2019-12-09 12:14:47 +00:00
|
|
|
const replace = require('@rollup/plugin-replace');
|
2020-03-15 16:06:00 +00:00
|
|
|
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');
|
2020-02-29 00:07:20 +00:00
|
|
|
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
|
2020-03-15 16:06:00 +00:00
|
|
|
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');
|
2020-03-15 16:06:00 +00:00
|
|
|
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">
|
2020-03-15 16:06:00 +00:00
|
|
|
import * as ${camelise(pkg_name)} from '${path.relative(templatePath, bundleFile)}'
|
2020-02-29 00:07:20 +00:00
|
|
|
window.${camelise(pkg_name)} = ${camelise(pkg_name)};
|
2019-04-19 07:42:28 +00:00
|
|
|
import './tests.js';
|
|
|
|
mocha.run();
|
|
|
|
</script>
|
|
|
|
`;
|
2020-02-29 00:07:20 +00:00
|
|
|
|
|
|
|
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: [
|
2020-03-15 16:06:00 +00:00
|
|
|
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();
|
|
|
|
});
|
|
|
|
}
|