与Webpack和Babel一起使用Benchmarkjs

我试图得到一些基本的基准testing工作,并无法找出正确的configuration。 我试图使用webpack和babel Benchmarkjs来将我的代码转换成es5。 我创build了一个benchmarks.webpack.js作为入口点,如下所示:

var context = require.context('./src/js', true, /-benchmark\.js$/); context.keys().forEach(context); module.exports = context; 

然后我有一个我想运行的基准testing文件( test-benchmark.js ):

 import benchmark from 'benchmark'; import benchmarks from 'beautify-benchmark'; let suite = new benchmark.Suite; suite.add('RegExp#test', function() { /o/.test('Hello World!'); }) .add('String#indexOf', function() { 'Hello World!'.indexOf('o') > -1; }) .on('cycle', function(event) { benchmarks.add(event.target); }) .on('complete', function() { benchmarks.log(); }) .run(); 

我更新了我的webpack版本来尝试和testing基准:

 _.assign(config, { devtool: 'eval-cheap-module-source-map', output: { path: path.join(__dirname, 'build/benchmark'), filename: 'benchmark.js', publicPath: '/' }, entry: [ './benchmarks.webpack.js' ], plugins: [ ], module: { loaders: [ { test: /\.js$/, loaders: ['babel?stage=0'], include: path.join(__dirname, 'src/js') }, ] }, }); 

最后,我想能够从一个npm脚本运行这个:

  "scripts": { "bench": "webpack --config webpack.bench.config.js && node build/benchmark/benchmark.js" }, 

但是,我收到警告说,基准testing依赖性的结果是一个expression式,没有合适的.json,.txt等文件的加载器。 我试图篡改Benchmarkjs正确导出,但没有成功。

 WARNING in ./~/benchmark/benchmark.js Critical dependencies: 1122:34-49 the request of a dependency is an expression @ ./~/benchmark/benchmark.js 1122:34-49 WARNING in ./~/benchmark/package.json Module parse failed: /home/bill/dev/levelstory/react-client-redux/node_modules/benchmark/package.json Line 2: Unexpected token : You may need an appropriate loader to handle this file type. | { | "name": "benchmark", | "version": "1.0.0", | "description": "A benchmarking library that works on nearly all JavaScript platforms, supports high-resolution timers, and returns statistically significant results.", @ ./~/benchmark ^\.\/.*$ WARNING in ./~/benchmark/LICENSE.txt Module parse failed: /home/bill/dev/levelstory/react-client-redux/node_modules/benchmark/LICENSE.txt Line 1: Unexpected number You may need an appropriate loader to handle this file type. | Copyright 2010-2012 Mathias Bynens <http://mathiasbynens.be/> | Based on JSLitmus.js, copyright Robert Kieffer <http://broofa.com/> | Modified by John-David Dalton <http://allyoucanleet.com/> @ ./~/benchmark ^\.\/.*$ 

基准testing看起来有些特殊require 。 这为Webpack混淆了。 它有以下几行:

 var freeRequire = typeof require == 'function' && require; ... function req(id) { try { var result = freeExports && freeRequire(id); } catch(e) { } return result || null; } 

如果注释掉function内容,错误就会消失。 考虑到用这种方法打补丁并不理想,我会直接向基准testing人员询问。 也许我们错过了一些东西。