Skip to content

Commit

Permalink
Adapt benchmark to the new build script (#1845)
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanGoncharov committed May 2, 2019
1 parent 316e24e commit d6c973a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 8 deletions.
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

0 comments on commit d6c973a

Please sign in to comment.