mirror of
https://github.com/tenrok/OverlayScrollbars.git
synced 2026-06-23 14:10:35 +03:00
improve structure & switch to yarn
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
const PuppeteerEnvironment = require('jest-environment-puppeteer');
|
||||
const pti = require('puppeteer-to-istanbul');
|
||||
const { setupRollupTest, cleanupRollupTest } = require('./jest-puppeteer.rollup.js');
|
||||
|
||||
class PuppeteerRollupEnvironment extends PuppeteerEnvironment {
|
||||
constructor(envConfig, envContext) {
|
||||
super(envConfig, envContext);
|
||||
|
||||
this.ctx = envContext;
|
||||
this.cfg = envConfig;
|
||||
}
|
||||
|
||||
async setup() {
|
||||
// setup
|
||||
await setupRollupTest(this.cfg.rootDir, this.ctx.testPath, this.cfg.cache && this.cfg.cacheDirectory);
|
||||
await super.setup();
|
||||
|
||||
// coverage
|
||||
const { page } = this.global;
|
||||
await Promise.all([page.coverage.startCSSCoverage(), page.coverage.startJSCoverage()]);
|
||||
}
|
||||
|
||||
async teardown() {
|
||||
// coverage
|
||||
const { page } = this.global;
|
||||
const [jsCoverage, cssCoverage] = await Promise.all([page.coverage.stopJSCoverage(), page.coverage.stopCSSCoverage()]);
|
||||
pti.write([...jsCoverage, ...cssCoverage], { includeHostname: true, storagePath: './.pptr' });
|
||||
|
||||
// cleanup
|
||||
cleanupRollupTest(this.ctx.testPath, this.cfg.cache);
|
||||
await super.teardown();
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = PuppeteerRollupEnvironment;
|
||||
@@ -0,0 +1,11 @@
|
||||
module.exports = {
|
||||
build: '.build',
|
||||
html: {
|
||||
input: 'index.html',
|
||||
output: 'build.html',
|
||||
},
|
||||
js: {
|
||||
input: 'index',
|
||||
output: 'build',
|
||||
},
|
||||
};
|
||||
@@ -0,0 +1,175 @@
|
||||
const fs = require('fs');
|
||||
const crypto = require('crypto');
|
||||
const path = require('path');
|
||||
const del = require('del');
|
||||
const rollup = require('rollup');
|
||||
const rollupPluginHtml = require('@rollup/plugin-html');
|
||||
const rollupPluginStyles = require('rollup-plugin-styles');
|
||||
const deploymentConfig = require('./jest-puppeteer.rollup.config.js');
|
||||
|
||||
const rollupConfigName = 'rollup.config.js';
|
||||
const rollupNodeEnv = 'build';
|
||||
const cacheFilePrefix = 'jest-puppeteer-overlayscrollbars-cache-';
|
||||
const cacheEncoding = 'utf8';
|
||||
const cacheHash = 'md5';
|
||||
|
||||
const makeHtmlAttributes = (attributes) => {
|
||||
if (!attributes) {
|
||||
return '';
|
||||
}
|
||||
|
||||
const keys = Object.keys(attributes);
|
||||
// eslint-disable-next-line no-param-reassign
|
||||
// eslint-disable-next-line no-return-assign
|
||||
return keys.reduce((result, key) => (result += ` ${key}="${attributes[key]}"`), '');
|
||||
};
|
||||
|
||||
const genHtmlTemplateFunc = (content) => ({ attributes, files, meta, publicPath, title }) => {
|
||||
const scripts = (files.js || [])
|
||||
.map(({ fileName }) => `<script src="${publicPath}${fileName}"${makeHtmlAttributes(attributes.script)}></script>`)
|
||||
.join('\n');
|
||||
|
||||
const links = (files.css || [])
|
||||
.map(({ fileName }) => `<link href="${publicPath}${fileName}" rel="stylesheet"${makeHtmlAttributes(attributes.link)}>`)
|
||||
.join('\n');
|
||||
|
||||
const metas = meta.map((input) => `<meta${makeHtmlAttributes(input)}>`).join('\n');
|
||||
|
||||
return `<!doctype html>
|
||||
<html${makeHtmlAttributes(attributes.html)}>
|
||||
<head>
|
||||
${metas}
|
||||
<title>${title}</title>
|
||||
${links}
|
||||
</head>
|
||||
<body>
|
||||
${content || ''}
|
||||
${scripts}
|
||||
</body>
|
||||
</html>`;
|
||||
};
|
||||
|
||||
const getAllFilesFrom = (dir, except) => {
|
||||
const result = [];
|
||||
fs.readdirSync(dir).forEach((dirOrFile) => {
|
||||
if (!except.includes(dirOrFile)) {
|
||||
const dirOrFileResolved = path.resolve(dir, dirOrFile);
|
||||
if (fs.statSync(dirOrFileResolved).isDirectory()) {
|
||||
result.push(...getAllFilesFrom(dirOrFileResolved));
|
||||
}
|
||||
result.push(dirOrFileResolved);
|
||||
}
|
||||
});
|
||||
return result;
|
||||
};
|
||||
|
||||
const createCacheObj = (testPath) => {
|
||||
const testFileName = path.basename(testPath);
|
||||
const testFiles = getAllFilesFrom(path.dirname(testPath), [deploymentConfig.build, testFileName]);
|
||||
const obj = {};
|
||||
|
||||
testFiles.forEach((dir) => {
|
||||
obj[dir] = crypto.createHash(cacheHash).update(fs.readFileSync(dir, cacheEncoding), cacheEncoding).digest('hex');
|
||||
});
|
||||
|
||||
return obj;
|
||||
};
|
||||
|
||||
const filesChanged = (testPath, cacheDir) => {
|
||||
let result = true;
|
||||
const cacheObjString = JSON.stringify(createCacheObj(testPath));
|
||||
const getCacheFile = path.resolve(cacheDir, cacheFilePrefix + crypto.createHash(cacheHash).update(testPath, cacheEncoding).digest('hex'));
|
||||
if (fs.existsSync(getCacheFile)) {
|
||||
result = cacheObjString !== fs.readFileSync(getCacheFile, cacheEncoding);
|
||||
}
|
||||
|
||||
if (result) {
|
||||
fs.writeFileSync(getCacheFile, cacheObjString);
|
||||
}
|
||||
|
||||
return result;
|
||||
};
|
||||
|
||||
const setupRollupTest = async (rootDir, testPath, cacheDir) => {
|
||||
const testDir = path.dirname(testPath);
|
||||
const testName = path.basename(testDir);
|
||||
const changed = cacheDir ? filesChanged(testPath, cacheDir) : true;
|
||||
const buildFolderExists = fs.existsSync(path.resolve(testDir, deploymentConfig.build));
|
||||
|
||||
if (changed || !buildFolderExists) {
|
||||
const env = process.env.NODE_ENV;
|
||||
process.env.NODE_ENV = rollupNodeEnv;
|
||||
|
||||
const rollupConfigPath = path.resolve(rootDir, rollupConfigName);
|
||||
|
||||
if (fs.existsSync(rollupConfigPath)) {
|
||||
const rollupConfig = require(rollupConfigPath); // eslint-disable-line
|
||||
|
||||
if (typeof rollupConfig === 'function') {
|
||||
try {
|
||||
const htmlFilePath = path.resolve(testDir, deploymentConfig.html.input);
|
||||
const htmlFileContent = fs.existsSync(htmlFilePath) ? fs.readFileSync(htmlFilePath, 'utf8') : null;
|
||||
|
||||
let rollupConfigObj = rollupConfig(undefined, {
|
||||
project: rootDir,
|
||||
overwrite: (rollupConfigDefaults) => ({
|
||||
input: path.resolve(testDir, deploymentConfig.js.input),
|
||||
dist: path.resolve(testDir, deploymentConfig.build),
|
||||
file: deploymentConfig.js.output,
|
||||
types: null,
|
||||
minVersions: false,
|
||||
esmBuild: false,
|
||||
sourcemap: true,
|
||||
name: testName,
|
||||
pipeline: [
|
||||
rollupPluginStyles(),
|
||||
...rollupConfigDefaults.pipeline,
|
||||
rollupPluginHtml({
|
||||
title: `Jest-Puppeteer: ${testName}`,
|
||||
fileName: deploymentConfig.html.output,
|
||||
template: genHtmlTemplateFunc(htmlFileContent),
|
||||
meta: [{ charset: 'utf-8' }, { 'http-equiv': 'X-UA-Compatible', content: 'IE=edge' }],
|
||||
}),
|
||||
],
|
||||
}),
|
||||
silent: true,
|
||||
fast: true,
|
||||
});
|
||||
|
||||
if (!Array.isArray(rollupConfigObj)) {
|
||||
rollupConfigObj = [rollupConfigObj];
|
||||
}
|
||||
|
||||
for (let i = 0; i < rollupConfigObj.length; i++) {
|
||||
const inputConfig = rollupConfigObj[i];
|
||||
let { output } = inputConfig;
|
||||
// eslint-disable-next-line no-await-in-loop
|
||||
const bundle = await rollup.rollup(inputConfig);
|
||||
|
||||
if (!Array.isArray(output)) {
|
||||
output = [output];
|
||||
}
|
||||
|
||||
for (let v = 0; v < output.length; v++) {
|
||||
const outputConfig = output[i];
|
||||
// eslint-disable-next-line no-await-in-loop
|
||||
await bundle.write(outputConfig);
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
console.warn(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
process.env.NODE_ENV = env;
|
||||
}
|
||||
};
|
||||
|
||||
const cleanupRollupTest = (testPath, cache) => {
|
||||
if (!cache) {
|
||||
del(path.resolve(path.dirname(testPath), deploymentConfig.build));
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = { setupRollupTest, cleanupRollupTest };
|
||||
@@ -0,0 +1,8 @@
|
||||
const path = require('path');
|
||||
const express = require('express');
|
||||
|
||||
const app = express();
|
||||
|
||||
app.use(express.static(path.join(__dirname, '../')));
|
||||
|
||||
app.listen(process.env.TEST_SERVER_PORT);
|
||||
@@ -0,0 +1,10 @@
|
||||
const path = require('path');
|
||||
const jestPuppeteerConfig = require('../jest-puppeteer.config.base');
|
||||
|
||||
module.exports = {
|
||||
process: (src, filePath, config) => {
|
||||
const deploymentPath = path.relative(path.dirname(config.globals.baseConfig), filePath);
|
||||
const split = deploymentPath.split(path.sep);
|
||||
return `module.exports = ${JSON.stringify(`http://localhost:${jestPuppeteerConfig.server.port}/${path.posix.join(...split)}`)}`;
|
||||
},
|
||||
};
|
||||
Reference in New Issue
Block a user