node.js - 将 Benchmarkjs 与 Webpack 和 Babel 结合使用

标签 node.js npm webpack babeljs benchmark.js

我正在尝试进行一些基本的基准测试,但无法确定正确的配置。我正在尝试使用 Benchmarkjs 与 webpack 和 babel 将我的代码转换为 es5。我创建了一个 benchmarks.webpack.js 作为入口点,如下所示:

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

然后我有一个想要运行的基准测试文件 (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 版本来尝试转换基准:

_.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"
  },

但是,我收到警告,指出基准测试依赖项的结果是一个表达式,并且没有适合 .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 ^\.\/.*$

最佳答案

看起来基准require做了一些特殊的事情。这让 Webpack 变得一团糟。它有以下几行:

var freeRequire = typeof require == 'function' && require;

...

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

如果注释掉函数内容,错误就会消失。鉴于以这种方式修补它并不理想,我会直接向基准测试人员询问这一点。也许我们遗漏了一些东西。

关于node.js - 将 Benchmarkjs 与 Webpack 和 Babel 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32242072/

相关文章:

javascript - 为什么 require ('../models/owners.js' ) 旁边还有另一个相邻的参数?

mysql - Nodejs - 在没有迁移的情况下向表中添加列?

angular - 在 monorepo 设置上自动提升 package.json 版本

vue.js - 在 Vue Component 中解析 webpack 图片源(在 Vuepress 中)

javascript - 是否可以在一个 webpack.config 中打包两个不同的模块?

jquery - 如何将具有共享依赖项的多个 TypeScript 文件编译为单个 JavaScript 文件?

javascript - "react doesn' t 存在”在 Node.js 命令提示符中

node.js - OpenShift :/lib64/libz. so.1 上的 NodeJS 应用程序:未找到版本 `ZLIB_1.2.9'

node.js - 尝试使用 npm start 启动 node.js 应用程序,但在浏览器中收到 err_empty_response

javascript - Webpack 找不到 SVG CSS 背景的路径