From a08dfc6e70a9b5839e90df6a8d37f8a861b1cadf Mon Sep 17 00:00:00 2001 From: K Sashi Kumar Date: Mon, 12 Feb 2018 18:49:46 +0530 Subject: [PATCH] Review comments: Move targetList to common helper Add provision to get cli args when src/cli.js is run separately --- src/cli-flags-helper.js | 38 ++++++++++++++++++++++++++++++++++++++ src/suite.js | 31 +++++-------------------------- webpack.config.js | 28 ++-------------------------- 3 files changed, 45 insertions(+), 52 deletions(-) create mode 100644 src/cli-flags-helper.js diff --git a/src/cli-flags-helper.js b/src/cli-flags-helper.js new file mode 100644 index 00000000..49f8c936 --- /dev/null +++ b/src/cli-flags-helper.js @@ -0,0 +1,38 @@ +const targetList = new Set([ + "acorn", + "babel", + "babylon", + "buble", + "chai", + "coffeescript", + "espree", + "esprima", + "jshint", + "lebab", + "prepack", + "prettier", + "source-map", + "typescript", + "uglify-es", + "uglify-js" +]); + +function getOnlyFlag(argv) { + if (process.argv.indexOf("--only") != -1) { + return process.argv[process.argv.indexOf("--only") + 1]; + } +} + +module.exports = { + getTarget: function() { + let onlyArg = getOnlyFlag(); + if (targetList.has(onlyArg)) { + return [onlyArg]; + } else if (typeof ONLY != "undefined" && targetList.has(ONLY)) { + return [ONLY]; + } else { + return [...targetList]; + } + }, + targetList: targetList +}; diff --git a/src/suite.js b/src/suite.js index b7a31f05..e2612273 100644 --- a/src/suite.js +++ b/src/suite.js @@ -3,6 +3,7 @@ // found in the LICENSE file. const Benchmark = require("benchmark"); +const getTarget = require("./cli-flags-helper").getTarget; // We need to run deterministically, so we set 'maxTime' to 0, which // disables the variable iteration count feature of benchmark.js, @@ -14,34 +15,12 @@ const defaultOptions = { minSamples: 20 }; -const targetList = [ - "acorn", - "babel", - "babylon", - "buble", - "chai", - "coffeescript", - "espree", - "esprima", - "jshint", - "lebab", - "prepack", - "prettier", - "source-map", - "typescript", - "uglify-es", - "uglify-js" -]; - const suite = new Benchmark.Suite(); -const targetItems = ONLY ? [ONLY] : null; - -const requireList = (targetItems || targetList).map(val => { - return require(`./${val}-benchmark`); -}); -requireList.forEach(options => { - suite.add(Object.assign({}, options, defaultOptions)); +getTarget().forEach(target => { + suite.add( + Object.assign({}, require(`./${target}-benchmark`), defaultOptions) + ); }); module.exports = suite; diff --git a/webpack.config.js b/webpack.config.js index af3bd826..a025d137 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -6,34 +6,10 @@ const CopyWebpackPlugin = require("copy-webpack-plugin"); const HtmlWebpackPlugin = require("html-webpack-plugin"); const path = require("path"); const webpack = require("webpack"); - -const targetList = [ - "acorn", - "babel", - "babylon", - "buble", - "chai", - "coffeescript", - "espree", - "esprima", - "jshint", - "lebab", - "prepack", - "prettier", - "source-map", - "typescript", - "uglify-es", - "uglify-js" -]; +const targetList = require("./src/cli-flags-helper").targetList; function getTarget(env) { - return ( - env && - env.only && - targetList.find(elem => { - return elem == env.only; - }) - ); + return env && targetList.has(env.only) && env.only; } module.exports = env => [