Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adapt benchmark to the new build script #1845

Merged
merged 1 commit into from May 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -11,4 +11,5 @@ package-lock.json
node_modules
coverage
dist
benchmarkDist
npm
44 changes: 36 additions & 8 deletions resources/benchmark.js
Expand Up @@ -9,14 +9,20 @@

'use strict';

const { Benchmark } = require('benchmark');
const { execSync } = require('child_process');
const os = require('os');
const fs = require('fs');
const path = require('path');
const { execSync } = require('child_process');
const { Benchmark } = require('benchmark');

const {
copyFile,
writeFile,
rmdirRecursive,
mkdirRecursive,
readdirRecursive,
} = require('./fs-utils');

// Like build:cjs, but includes __tests__ and copies other files.
const BUILD_CMD = 'babel src --copy-files --out-dir dist/';
const LOCAL = 'local';

function LOCAL_DIR(...paths) {
Expand All @@ -43,8 +49,7 @@ function prepareRevision(revision) {
console.log(`🍳 Preparing ${revision}...`);

if (revision === LOCAL) {
execSync(`yarn run ${BUILD_CMD}`);
return LOCAL_DIR('dist');
return babelBuild(LOCAL_DIR());
} else {
if (!fs.existsSync(TEMP_DIR())) {
fs.mkdirSync(TEMP_DIR());
Expand All @@ -66,12 +71,35 @@ function prepareRevision(revision) {
execSync(
`cp -R "${LOCAL_DIR()}/src/__fixtures__/" "${dir}/src/__fixtures__/"`
);
execSync(`yarn run ${BUILD_CMD}`, { cwd: dir });

return path.join(dir, 'dist');
return babelBuild(dir);
}
}

function babelBuild(dir) {
const oldCWD = process.cwd();
process.chdir(dir);

rmdirRecursive('./benchmarkDist');
mkdirRecursive('./benchmarkDist');

const babel = require('@babel/core');
for (const filepath of readdirRecursive('./src')) {
const srcPath = path.join('./src', filepath);
const distPath = path.join('./benchmarkDist', filepath);

if (filepath.endsWith('.js')) {
const cjs = babel.transformFileSync(srcPath, { envName: 'cjs' });
writeFile(distPath, cjs.code);
} else {
copyFile(srcPath, distPath);
}
}

process.chdir(oldCWD);
return path.join(dir, 'benchmarkDist');
}

function findFiles(cwd, pattern) {
const out = execSync(`find . -path '${pattern}'`, { cwd, encoding: 'utf8' });
return out.split('\n').filter(Boolean);
Expand Down