improve structure & switch to yarn

This commit is contained in:
Rene Haas
2020-09-10 12:16:21 +02:00
parent 43efd22c47
commit 30a5a11862
46 changed files with 9070 additions and 14897 deletions
+4 -3
View File
@@ -1,5 +1,6 @@
node_modules node_modules
coverage
dist dist
__build__ types
types .build
.coverage
.pptr
+2 -1
View File
@@ -1,4 +1,5 @@
const resolve = require('./resolve.config'); const resolve = require('./resolve.config');
const puppeteerRollupConfig = require('./config/jest-puppeteer.rollup.config.js');
module.exports = { module.exports = {
extends: ['plugin:@typescript-eslint/recommended', 'plugin:react/recommended', 'airbnb', 'prettier'], extends: ['plugin:@typescript-eslint/recommended', 'plugin:react/recommended', 'airbnb', 'prettier'],
@@ -69,7 +70,7 @@ module.exports = {
'no-void': 'off', 'no-void': 'off',
'no-empty-function': 'off', 'no-empty-function': 'off',
'no-new-func': 'off', 'no-new-func': 'off',
'import/no-unresolved': ['error', { ignore: ['./__build__/build.html$'] }], 'import/no-unresolved': ['error', { ignore: [`./${puppeteerRollupConfig.build}/${puppeteerRollupConfig.html.output}$`] }],
}, },
globals: { globals: {
page: true, page: true,
+3 -5
View File
@@ -4,11 +4,9 @@
node_modules/ node_modules/
# generated # generated
coverage/ .build/
__build__/ .coverage/
.pptr/
# coverage
/.nyc_output
# local env files # local env files
.env.local .env.local
+1
View File
@@ -0,0 +1 @@
registry="https://registry.npmjs.org"
+4 -2
View File
@@ -1,4 +1,6 @@
node_modules node_modules
coverage
dist dist
types types
.coverage
.build
.pptr
@@ -12,7 +12,7 @@ class PuppeteerRollupEnvironment extends PuppeteerEnvironment {
async setup() { async setup() {
// setup // setup
await setupRollupTest(this.ctx.testPath, this.cfg.cache, this.cfg.cacheDirectory); await setupRollupTest(this.cfg.rootDir, this.ctx.testPath, this.cfg.cache && this.cfg.cacheDirectory);
await super.setup(); await super.setup();
// coverage // coverage
@@ -24,7 +24,7 @@ class PuppeteerRollupEnvironment extends PuppeteerEnvironment {
// coverage // coverage
const { page } = this.global; const { page } = this.global;
const [jsCoverage, cssCoverage] = await Promise.all([page.coverage.stopJSCoverage(), page.coverage.stopCSSCoverage()]); const [jsCoverage, cssCoverage] = await Promise.all([page.coverage.stopJSCoverage(), page.coverage.stopCSSCoverage()]);
pti.write([...jsCoverage, ...cssCoverage], { includeHostname: true, storagePath: './.nyc_output' }); pti.write([...jsCoverage, ...cssCoverage], { includeHostname: true, storagePath: './.pptr' });
// cleanup // cleanup
cleanupRollupTest(this.ctx.testPath, this.cfg.cache); cleanupRollupTest(this.ctx.testPath, this.cfg.cache);
@@ -1,5 +1,5 @@
module.exports = { module.exports = {
build: '__build__', build: '.build',
html: { html: {
input: 'index.html', input: 'index.html',
output: 'build.html', output: 'build.html',
@@ -5,10 +5,9 @@ const del = require('del');
const rollup = require('rollup'); const rollup = require('rollup');
const rollupPluginHtml = require('@rollup/plugin-html'); const rollupPluginHtml = require('@rollup/plugin-html');
const rollupPluginStyles = require('rollup-plugin-styles'); const rollupPluginStyles = require('rollup-plugin-styles');
const rollupConfig = require('./rollup.config.js'); const deploymentConfig = require('./jest-puppeteer.rollup.config.js');
const resolve = require('./resolve.config.json');
const config = require('./jest-puppeteer.rollup.config.js');
const rollupConfigName = 'rollup.config.js';
const rollupNodeEnv = 'build'; const rollupNodeEnv = 'build';
const cacheFilePrefix = 'jest-puppeteer-overlayscrollbars-cache-'; const cacheFilePrefix = 'jest-puppeteer-overlayscrollbars-cache-';
const cacheEncoding = 'utf8'; const cacheEncoding = 'utf8';
@@ -66,7 +65,7 @@ const getAllFilesFrom = (dir, except) => {
const createCacheObj = (testPath) => { const createCacheObj = (testPath) => {
const testFileName = path.basename(testPath); const testFileName = path.basename(testPath);
const testFiles = getAllFilesFrom(path.dirname(testPath), [config.build, testFileName]); const testFiles = getAllFilesFrom(path.dirname(testPath), [deploymentConfig.build, testFileName]);
const obj = {}; const obj = {};
testFiles.forEach((dir) => { testFiles.forEach((dir) => {
@@ -91,43 +90,32 @@ const filesChanged = (testPath, cacheDir) => {
return result; return result;
}; };
const getRollupInfos = (testPath) => { const setupRollupTest = async (rootDir, testPath, cacheDir) => {
const projectRootPath = path.resolve(__dirname, resolve.projectRoot);
const testDir = path.dirname(testPath); const testDir = path.dirname(testPath);
const input = path.resolve(testDir, config.js.input);
const dist = path.resolve(testDir, config.build);
const testName = path.basename(testDir); const testName = path.basename(testDir);
const changed = cacheDir ? filesChanged(testPath, cacheDir) : true;
const buildFolderExists = fs.existsSync(path.resolve(testDir, deploymentConfig.build));
return { if (changed || !buildFolderExists) {
projectRootPath, const env = process.env.NODE_ENV;
testDir, process.env.NODE_ENV = rollupNodeEnv;
testName,
input,
dist,
};
};
const setupRollupTest = async (testPath, cache, cacheDir) => { const rollupConfigPath = path.resolve(rootDir, rollupConfigName);
const { projectRootPath, input, dist, testName, testDir } = getRollupInfos(testPath);
const changed = !cache || filesChanged(testPath, cacheDir);
if (changed || !fs.existsSync(path.resolve(testDir, config.build))) { if (fs.existsSync(rollupConfigPath)) {
const testPathSplit = path.relative(projectRootPath, testPath).split(path.sep); const rollupConfig = require(rollupConfigPath); // eslint-disable-line
if (testPathSplit.length > 0) {
const [project] = testPathSplit;
const env = process.env.NODE_ENV;
try { if (typeof rollupConfig === 'function') {
process.env.NODE_ENV = rollupNodeEnv; try {
const htmlFilePath = path.resolve(testDir, config.html.input); const htmlFilePath = path.resolve(testDir, deploymentConfig.html.input);
const htmlFileContent = fs.existsSync(htmlFilePath) ? fs.readFileSync(htmlFilePath, 'utf8') : null; const htmlFileContent = fs.existsSync(htmlFilePath) ? fs.readFileSync(htmlFilePath, 'utf8') : null;
let rollupConfigObj = rollupConfig(
{ 'config-project': project }, let rollupConfigObj = rollupConfig(undefined, {
{ project: rootDir,
overwrite: { overwrite: (rollupConfigDefaults) => ({
input, input: path.resolve(testDir, deploymentConfig.js.input),
dist, dist: path.resolve(testDir, deploymentConfig.build),
file: config.js.output, file: deploymentConfig.js.output,
types: null, types: null,
minVersions: false, minVersions: false,
esmBuild: false, esmBuild: false,
@@ -135,53 +123,52 @@ const setupRollupTest = async (testPath, cache, cacheDir) => {
name: testName, name: testName,
pipeline: [ pipeline: [
rollupPluginStyles(), rollupPluginStyles(),
...rollupConfig.defaults.pipeline, ...rollupConfigDefaults.pipeline,
rollupPluginHtml({ rollupPluginHtml({
title: `Jest-Puppeteer: ${testName}`, title: `Jest-Puppeteer: ${testName}`,
fileName: config.html.output, fileName: deploymentConfig.html.output,
template: genHtmlTemplateFunc(htmlFileContent), template: genHtmlTemplateFunc(htmlFileContent),
meta: [{ charset: 'utf-8' }, { 'http-equiv': 'X-UA-Compatible', content: 'IE=edge' }], meta: [{ charset: 'utf-8' }, { 'http-equiv': 'X-UA-Compatible', content: 'IE=edge' }],
}), }),
], ],
}, }),
silent: true, silent: true,
fast: true, fast: true,
check: false, });
}
);
if (!Array.isArray(rollupConfigObj)) { if (!Array.isArray(rollupConfigObj)) {
rollupConfigObj = [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++) { for (let i = 0; i < rollupConfigObj.length; i++) {
const outputConfig = output[i]; const inputConfig = rollupConfigObj[i];
let { output } = inputConfig;
// eslint-disable-next-line no-await-in-loop // eslint-disable-next-line no-await-in-loop
await bundle.write(outputConfig); 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);
} }
} catch (e) {
console.warn(e);
} }
process.env.NODE_ENV = env;
} }
process.env.NODE_ENV = env;
} }
}; };
const cleanupRollupTest = (testPath, cache) => { const cleanupRollupTest = (testPath, cache) => {
if (!cache) { if (!cache) {
const { dist } = getRollupInfos(testPath); del(path.resolve(path.dirname(testPath), deploymentConfig.build));
del(dist);
} }
}; };
@@ -3,6 +3,6 @@ const express = require('express');
const app = express(); const app = express();
app.use(express.static(path.join(__dirname, '.'))); app.use(express.static(path.join(__dirname, '../')));
app.listen(process.env.TEST_SERVER_PORT); app.listen(process.env.TEST_SERVER_PORT);
+10
View File
@@ -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)}`)}`;
},
};
@@ -1,5 +1,8 @@
const path = require('path');
const { TEST_SERVER_PORT } = process.env; const { TEST_SERVER_PORT } = process.env;
const port = TEST_SERVER_PORT ? Number(TEST_SERVER_PORT) : 8080; const port = TEST_SERVER_PORT ? Number(TEST_SERVER_PORT) : 8080;
const testServerPath = path.resolve(__dirname, './config/jest-test-server.js');
process.env.TEST_SERVER_PORT = port; process.env.TEST_SERVER_PORT = port;
@@ -7,7 +10,7 @@ module.exports = {
browser: 'chromium', browser: 'chromium',
browserContext: 'incognito', browserContext: 'incognito',
server: { server: {
command: `cross-env TEST_SERVER_PORT=${port} node jest-test-server`, command: `cross-env TEST_SERVER_PORT=${port} node ${testServerPath}`,
port, port,
launchTimeout: 4000, launchTimeout: 4000,
}, },
-11
View File
@@ -1,11 +0,0 @@
const path = require('path');
module.exports = {
process: (src, filePath) => {
const deploymentPath = path.relative(__dirname, filePath);
const split = deploymentPath.split(path.sep);
const { TEST_SERVER_PORT } = process.env;
return `module.exports = ${JSON.stringify(`http://localhost:${TEST_SERVER_PORT}/${path.posix.join(...split)}`)}`;
},
};
+14 -7
View File
@@ -1,5 +1,9 @@
const path = require('path');
const resolve = require('./resolve.config'); const resolve = require('./resolve.config');
const puppeteerRollupConfig = require('./jest-puppeteer.rollup.config.js'); const puppeteerRollupConfig = require('./config/jest-puppeteer.rollup.config.js');
const testEnvironmentPath = path.resolve(__dirname, './config/jest-puppeteer.env.js');
const testServerLoaderPath = path.resolve(__dirname, './config/jest-test-server.loader.js');
// For a detailed explanation regarding each configuration property, visit: // For a detailed explanation regarding each configuration property, visit:
// https://jestjs.io/docs/en/configuration.html // https://jestjs.io/docs/en/configuration.html
@@ -7,31 +11,34 @@ const puppeteerRollupConfig = require('./jest-puppeteer.rollup.config.js');
const base = { const base = {
clearMocks: true, clearMocks: true,
collectCoverage: true, collectCoverage: true,
coverageDirectory: 'coverage', coverageDirectory: './.coverage',
moduleDirectories: resolve.directories, moduleDirectories: resolve.directories,
moduleFileExtensions: resolve.extensions.map((ext) => ext.replace(/\./, '')), moduleFileExtensions: resolve.extensions.map((ext) => ext.replace(/\./, '')),
testPathIgnorePatterns: ['\\\\node_modules\\\\'], testPathIgnorePatterns: ['\\\\node_modules\\\\'],
verbose: true, globals: {
baseConfig: __filename,
},
}; };
module.exports = { module.exports = {
...base,
projects: [ projects: [
{ {
...base, ...base,
displayName: 'jsdom', displayName: 'jsdom',
testMatch: ['**/__tests__/jsdom/**/*.test.[jt]s?(x)'], testMatch: ['**/tests/jsdom/**/*.test.[jt]s?(x)'],
}, },
{ {
...base, ...base,
preset: 'jest-puppeteer', preset: 'jest-puppeteer',
displayName: 'puppeteer', displayName: 'puppeteer',
setupFilesAfterEnv: ['expect-puppeteer'], setupFilesAfterEnv: ['expect-puppeteer'],
testMatch: ['**/__tests__/puppeteer/**/*.test.[jt]s?(x)'], testMatch: ['**/tests/puppeteer/**/*.test.[jt]s?(x)'],
testEnvironment: './jest-puppeteer.env.js', testEnvironment: testEnvironmentPath,
coveragePathIgnorePatterns: ['/node_modules/', `/${puppeteerRollupConfig.build}/`], coveragePathIgnorePatterns: ['/node_modules/', `/${puppeteerRollupConfig.build}/`],
transform: { transform: {
'^.+\\.[jt]sx?$': 'babel-jest', '^.+\\.[jt]sx?$': 'babel-jest',
[`^.+${puppeteerRollupConfig.build}.+${puppeteerRollupConfig.html.output}?$`]: './jest-test-server.loader.js', [`^.+${puppeteerRollupConfig.build}.+${puppeteerRollupConfig.html.output}?$`]: testServerLoaderPath,
}, },
}, },
], ],
-14764
View File
File diff suppressed because it is too large Load Diff
+4 -2
View File
@@ -55,8 +55,10 @@
"utf-8-validate": "^5.0.2" "utf-8-validate": "^5.0.2"
}, },
"scripts": { "scripts": {
"test": "jest --coverage --runInBand --detectOpenHandles", "test": "cd ./packages/overlayscrollbars && yarn test",
"build": "cross-env NODE_ENV=build rollup -c --config-project='overlayscrollbars' && cross-env NODE_ENV=build rollup -c --config-project='overlayscrollbars-jquery'", "test:jsdom": "cd ./packages/overlayscrollbars && yarn test:jsdom",
"test:pptr": "cd ./packages/overlayscrollbars && yarn test:pptr",
"build": "cd ./packages/overlayscrollbars && yarn build",
"lint": "npx eslint --fix ." "lint": "npx eslint --fix ."
} }
} }
@@ -0,0 +1 @@
module.exports = require('../../babel.config.base');
@@ -1,7 +0,0 @@
{
"name": "OverlayScrollbars",
"exports": "auto",
"globals": {
"jquery": "jQuery"
}
}
+1 -1
View File
@@ -3,7 +3,7 @@
? (module.exports = factory()) ? (module.exports = factory())
: typeof define === 'function' && define.amd : typeof define === 'function' && define.amd
? define(factory) ? define(factory)
: ((global = global || self), (global.OverlayScrollbars = factory())); : ((global = typeof globalThis !== 'undefined' ? globalThis : global || self), (global.OverlayScrollbars = factory()));
})(this, function () { })(this, function () {
'use strict'; 'use strict';
File diff suppressed because one or more lines are too long
@@ -0,0 +1 @@
module.exports = require('../../jest-puppeteer.config.base');
@@ -0,0 +1 @@
module.exports = require('../../jest.config.base');
+8 -1
View File
@@ -1,5 +1,12 @@
{ {
"name": "overlayscrollbars",
"private": true, "private": true,
"description": "OverlayScrollbars version 2", "description": "OverlayScrollbars version 2",
"version": "0.0.1" "version": "0.0.1",
"scripts": {
"test": "jest --coverage --runInBand --detectOpenHandles",
"test:jsdom": "jest --coverage --runInBand --detectOpenHandles --selectProjects jsdom",
"test:pptr": "jest --coverage --runInBand --detectOpenHandles --selectProjects puppeteer",
"build": "cross-env NODE_ENV=build rollup -c"
}
} }
@@ -0,0 +1,11 @@
const base = require('../../rollup.config.base');
const config = {
name: 'OverlayScrollbars',
exports: 'auto',
globals: {
jquery: 'jQuery',
},
};
module.exports = (_, ...args) => base(config, ...args);
@@ -1,4 +1,4 @@
import url from './__build__/build.html'; import url from './.build/build.html';
describe('Environment', () => { describe('Environment', () => {
beforeAll(async () => { beforeAll(async () => {
+1 -2
View File
@@ -1,5 +1,4 @@
{ {
"extensions": [".json", ".js", "jsx", ".mjs", ".ts", ".tsx"], "extensions": [".json", ".js", "jsx", ".mjs", ".ts", ".tsx"],
"directories": ["node_modules", "src"], "directories": ["node_modules", "src"]
"projectRoot": "./packages"
} }
+22 -23
View File
@@ -9,13 +9,12 @@ const fs = require('fs');
const path = require('path'); const path = require('path');
const resolve = require('./resolve.config.json'); const resolve = require('./resolve.config.json');
const buildConfigNames = ['build.config.js', 'build.config.json']; const rollupConfigDefaults = {
const buildConfigDefaults = {
input: './src/index', input: './src/index',
src: './src', src: './src',
dist: './dist', dist: './dist',
types: './types', types: './types',
tests: './__tests__', tests: './tests',
cache: [], cache: [],
minVersions: true, minVersions: true,
sourcemap: true, sourcemap: true,
@@ -60,30 +59,31 @@ const appendExtension = (file) => {
return file; return file;
}; };
const getBuildConfig = (projectPath) => { const resolvePath = (basePath, pathToResolve, appendExt) => {
const buildConfigName = buildConfigNames.find((name) => fs.existsSync(path.resolve(projectPath, name))); const result = pathToResolve ? (path.isAbsolute(pathToResolve) ? pathToResolve : path.resolve(basePath, pathToResolve)) : null;
return buildConfigName ? path.resolve(projectPath, buildConfigName) : '';
};
const resolvePath = (projectPath, rPath, appendExt) => {
const result = rPath ? (path.isAbsolute(rPath) ? rPath : path.resolve(projectPath, rPath)) : null;
return result && appendExt ? appendExtension(result) : result; return result && appendExt ? appendExtension(result) : result;
}; };
const rollupConfig = (config, { overwrite: overwriteBuildConfig, silent, fast, check = true } = {}) => { const resolveConfig = (config) => {
const { 'config-project': project } = config; if (typeof config === 'function') {
return config(rollupConfigDefaults) || {};
}
return config;
};
const projectPath = path.resolve(__dirname, resolve.projectRoot, project); const rollupConfig = (config = {}, { project = process.cwd(), overwrite = {}, silent, fast } = {}) => {
const packageJSONPath = path.resolve(projectPath, 'package.json'); const projectPath = resolvePath(__dirname, project);
const tsconfigJSONPath = path.resolve(projectPath, 'tsconfig.json'); const projectName = path.basename(project);
const buildConfigPath = getBuildConfig(projectPath);
const packageJSONPath = resolvePath(projectPath, 'package.json');
const tsconfigJSONPath = resolvePath(projectPath, 'tsconfig.json');
const isTypeScriptProject = fs.existsSync(tsconfigJSONPath); const isTypeScriptProject = fs.existsSync(tsconfigJSONPath);
const buildConfig = { const buildConfig = {
...buildConfigDefaults, ...rollupConfigDefaults,
...{ name: project, file: project }, ...{ name: projectName, file: projectName },
...require(buildConfigPath), ...resolveConfig(config),
...(overwriteBuildConfig || {}), ...resolveConfig(overwrite),
}; };
const { input, src, dist, types, tests, file, cache, minVersions, sourcemap, esmBuild, name, exports, globals, pipeline } = buildConfig; const { input, src, dist, types, tests, file, cache, minVersions, sourcemap, esmBuild, name, exports, globals, pipeline } = buildConfig;
@@ -127,7 +127,8 @@ const rollupConfig = (config, { overwrite: overwriteBuildConfig, silent, fast, c
commonjs: rollupCommonjs(), commonjs: rollupCommonjs(),
typescript: isTypeScriptProject typescript: isTypeScriptProject
? rollupTypescript({ ? rollupTypescript({
check, check: !fast,
clean: true,
useTsconfigDeclarationDir: true, useTsconfigDeclarationDir: true,
tsconfig: tsconfigJSONPath, tsconfig: tsconfigJSONPath,
tsconfigOverride: { tsconfigOverride: {
@@ -224,6 +225,4 @@ const rollupConfig = (config, { overwrite: overwriteBuildConfig, silent, fast, c
return builds; return builds;
}; };
rollupConfig.defaults = buildConfigDefaults;
module.exports = rollupConfig; module.exports = rollupConfig;
+8924
View File
File diff suppressed because it is too large Load Diff