首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Benchmarkjs与Webpack和Babel

使用Benchmarkjs与Webpack和Babel
EN

Stack Overflow用户
提问于 2015-08-27 06:24:10
回答 1查看 882关注 0票数 2

我正试图让一些基本的基准测试正常工作,并且很难找到正确的配置。我正在尝试使用Benchmarkjs与webpack和babel一起将我的代码传输到es5。我创建了一个benchmarks.webpack.js作为入口点,如下所示:

代码语言:javascript
复制
var context = require.context('./src/js', true, /-benchmark\.js$/);
context.keys().forEach(context);
module.exports = context;

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

代码语言:javascript
复制
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的构建,以尝试转换基准:

代码语言:javascript
复制
_.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脚本中运行这个脚本:

代码语言:javascript
复制
  "scripts": {
    "bench": "webpack --config webpack.bench.config.js && node build/benchmark/benchmark.js"
  },

但是,我收到警告,基准依赖的结果是一个表达式,并且没有适合于.json、.txt等文件的加载器。我试着利用Benchmarkjs进行正确的出口,但没有成功。

代码语言:javascript
复制
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 ^\.\/.*$
EN

回答 1

Stack Overflow用户

发布于 2015-08-28 04:58:58

看来benchmark在require上做了一些特别的事情。这对Webpack来说太糟糕了。它有以下几行:

代码语言:javascript
复制
var freeRequire = typeof require == 'function' && require;

...

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

如果注释掉函数内容,错误就会消失。考虑到以这种方式修补它是不理想的,我会直接问基准测试人员这个问题。也许我们遗漏了什么。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32242072

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档