Node.js 为节点/浏览器模块使用性能挂钩的网页包

Node.js 为节点/浏览器模块使用性能挂钩的网页包,node.js,webpack,browser,umd,Node.js,Webpack,Browser,Umd,我正在制作一个我希望在浏览器和节点中可用的模块。它依赖于性能,这给我带来了Webpack和节点中perf_hooks模块的麻烦。无论我做什么,我只能在一个或另一个有效的地方得到它,但不能两者兼而有之 下面是我尝试过的大部分事情。我的问题是,如何配置Webpack,使其在节点中需要性能挂钩,但在浏览器中使用内置的全局性能挂钩 以下是我的基本网页包配置: const path = require('path'); module.exports = { entry: './src/UpdateL

我正在制作一个我希望在浏览器和节点中可用的模块。它依赖于性能,这给我带来了Webpack和节点中perf_hooks模块的麻烦。无论我做什么,我只能在一个或另一个有效的地方得到它,但不能两者兼而有之

下面是我尝试过的大部分事情。我的问题是,如何配置Webpack,使其在节点中需要性能挂钩,但在浏览器中使用内置的全局性能挂钩

以下是我的基本网页包配置:

const path = require('path');

module.exports = {
  entry: './src/UpdateLoop.js',
  mode: 'development',
  output: {
    library: 'UpdateLoop',
    libraryTarget: 'umd',
    path: path.resolve(__dirname, 'dist'),
    filename: 'updateloop.js',
    globalObject: 'this',
  },
};
给我带来麻烦的代码:

const { performance } = require('perf_hooks');
此网页包中的错误包括:

Field 'browser' doesn't contain a valid alias configuration
    resolve as module
      C:\Users\joe.jankowiak\projects\update-loop\src\node_modules doesn't exist or is not a directory
我看到了“fs”执行以下操作的建议:

// configuration.node has an unknown property 'perf_hooks'
  node: {
    perf_hooks: false,
  },

// configuration has an unknown property 'browser'.
  browser: {
    perf_hooks: false,
  },
然后我看到有人推荐使用“决心”:

// Compiles, but complains performance doesn't exist in node or browser.
resolve: {
  fallback: {
    perf_hooks: false,
  }
},
// Works in browser but doesn't work in node. Node complains about using performance before its defined:
performance = performance || require('perf_hooks').performance;
// Doesn't work in either
const performance = performance || require('perf_hooks').performance;
// Trying to check if its node, but with resolve its making perf_hooks null in node
if(typeof __webpack_require__ === 'function') {
  global.performance = require('perf_hooks').performance;
}