如何找到最常用的JS文件?

时间:2022-04-17 19:53:50

I have a project with a lot of inter-related files that require each other. I want to know which is the most frequently used dependency (JS file) excluding npm modules, with each use defined as a 'require' or 'import' statement from the dependent file.

我有一个项目,里面有很多相互关联的文件,需要相互配合。我想知道除了npm模块之外,最常用的依赖项(JS文件)是什么,每个使用都定义为从属文件中的“require”或“import”语句。

Example: I have common methods stored in src/general/utils.js, and is imported by multiple files throughout my application. As such, that would be my most frequently used file utils.js.

示例:我有一些通用方法存储在src/general/utils中。在我的应用程序中有多个文件导入。因此,这将是我最常用的文件utils.js。

1 个解决方案

#1


2  

A couple of things first.

先说几件事。

It's important to note that the import command is not native to nodeJS. Rather it's a feature provided by transpilers such as those used by webpack, all of which will convert import commands into the NodeJS supported require() provided by node's internal Module module.

需要注意的是,导入命令不是nodeJS的原生命令。相反,它是由传输器提供的特性,比如webpack使用的特性,所有这些特性都将导入命令转换为node内部模块提供的NodeJS支持的require()。

Under the covers, Module.require() uses a cache to hold all previously required modules to optimize requires so they are only included once.

在幕后,modu .require()使用缓存保存所有以前需要的模块来优化需求,因此它们只包含一次。

To your question, one approach to counting modules loaded by the nodeJS runtime would be to override the portion of require() that locates and loads required modules either from internal sources (like events, fs, etc.), the file system (for first time requires), or the require cache (for subsequent requires of already loaded modules).

你的问题,一种方法计算模块由nodeJS加载运行时将覆盖的部分需要(),定位和加载所需的模块内部来源(如事件、fs等),文件系统(第一次需要),或需要缓存(后续需要已经加载的模块)。

This can be accomplished by creating and requiring the following module as the first require used by your application:

这可以通过创建并要求您的应用程序首先使用以下模块来实现:

// filename: module-override.js
const Module = require('module')

Module.MODULES = {
  LOADED: {},
  CACHED: {}
}
Module.__load = Module._load // save original _load()

Module._load = function (request, parent, isMain) { // redefine _load()
  const filename = Module._resolveFilename(request, parent, isMain)
  const key = filename.replace(/^\/Users\/USERNAME/, '...')
  if (!key.match(/node_modules/)) { // ignore paths with node_modules
    let val
    if (Module._cache[filename]) { // exists in Module_cache
      val = Module.MODULES.CACHED[key] || 0
      Module.MODULES.CACHED[key] = val + 1
    }
    val = Module.MODULES.LOADED[key] || 0
    Module.MODULES.LOADED[key] = val + 1
  }
  return Module.__load(request, parent, isMain) // the original _load()
}

Note: You can comment out the line which defines the key. I added that so the output of stringifying Module.MODULES did not contain any locally identifying information.

注意:可以注释定义键的行。我添加了这个,所以stringi化模块的输出。模块不包含任何本地标识信息。

Once you require this module, it will collect a count of all required modules in Module.MODULES.

一旦您需要这个模块,它将收集模块中所有需要的模块的计数。

Module.MODULES.LOADED will keep track of how many times a module has been required while Module.MODULES.CACHED will do the same for requires that have been fulfilled from the internal module cache.

Module.MODULES。加载将跟踪模块在模块. modules时需要多少次。缓存将执行从内部模块缓存中完成的要求。

Assume that I have two modules under ./lib named utils.js and Schedule.js where

假设我在./lib下有两个名为utils的模块。js和进度。js的地方

  • ./lib/utils requires util, path, events, debug, json-stringify-safe andlodash, and
  • /lib/utils需要util、path、events、debug、json-stringify-safe和lodash,以及。
  • Schedule.js requires path, colors and ./lib/utils
  • 时间表。js需要路径、颜色和./lib/utils

And that I have a top-level file that requires both ./lib/Schedule and ./lib/utils.

我有一个需要./lib/Schedule和./lib/utils的*文件。

After all the requires have completed, I can then call

所有的要求都完成之后,我就可以打电话了

console.log(JSON.stringify(Module.MODULES))

which will print to the console:

将打印到控制台:

{ 
  LOADED: {
    '..././lib/Schedule.js': 1,
     path: 2,
     '..././lib/utils.js': 2,
     util: 2,
     events: 1,
     tty: 1
   },
  CACHED: {
    '..././lib/utils.js': 1
  }
}

which tells me that

这告诉我,

  • ./lib/Schedule.js was loaded once from the file system,
  • / lib /时间表。js从文件系统加载一次,
  • path, util, events, and tty were loaded from nodeJS's internal modules, and
  • path、util、事件和tty都是从nodeJS的内部模块和
  • utils was loaded twice, once from its file and once from the internal require cache
  • utils被加载两次,一次来自文件,一次来自内部需要缓存

Without the test to weed out all modules found under /node_modules/ the output will be:

如果不进行删除/node_modules/中找到的所有模块的测试,输出将是:

{
  LOADED: {
    '.../lib/Schedule.js': 1,
    path: 2,
    '.../node_modules/colors/lib/index.js': 1,
    '.../node_modules/colors/lib/colors.js': 6,
    '.../node_modules/colors/lib/styles.js': 1,
    '.../node_modules/colors/lib/system/supports-colors.js': 1,
    '.../node_modules/colors/lib/custom/trap.js': 1,
    '.../node_modules/colors/lib/custom/zalgo.js': 1,
    '.../node_modules/colors/lib/maps/america.js': 1,
    '.../node_modules/colors/lib/maps/zebra.js': 1,
    '.../node_modules/colors/lib/maps/rainbow.js': 1,
    '.../node_modules/colors/lib/maps/random.js': 1,
    '.../node_modules/colors/lib/extendStringPrototype.js': 1,
    '.../lib/utils.js': 2,
    util: 2,
    events: 1,
    '/.../node_modules/lodash/lodash.js': 1,
    '/.../node_modules/json-stringify-safe/stringify.js': 1,
    '.../node_modules/debug/src/index.js': 1,
    '.../node_modules/debug/src/node.js': 1,
    tty: 1,
    '.../node_modules/debug/src/debug.js': 1,
    '.../node_modules/ms/index.js': 1,
    '/.../node_modules/supports-color/index.js': 1 },
  CACHED: {
    '.../node_modules/colors/lib/colors.js': 5,
    '.../lib/utils.js': 1
  }
}

#1


2  

A couple of things first.

先说几件事。

It's important to note that the import command is not native to nodeJS. Rather it's a feature provided by transpilers such as those used by webpack, all of which will convert import commands into the NodeJS supported require() provided by node's internal Module module.

需要注意的是,导入命令不是nodeJS的原生命令。相反,它是由传输器提供的特性,比如webpack使用的特性,所有这些特性都将导入命令转换为node内部模块提供的NodeJS支持的require()。

Under the covers, Module.require() uses a cache to hold all previously required modules to optimize requires so they are only included once.

在幕后,modu .require()使用缓存保存所有以前需要的模块来优化需求,因此它们只包含一次。

To your question, one approach to counting modules loaded by the nodeJS runtime would be to override the portion of require() that locates and loads required modules either from internal sources (like events, fs, etc.), the file system (for first time requires), or the require cache (for subsequent requires of already loaded modules).

你的问题,一种方法计算模块由nodeJS加载运行时将覆盖的部分需要(),定位和加载所需的模块内部来源(如事件、fs等),文件系统(第一次需要),或需要缓存(后续需要已经加载的模块)。

This can be accomplished by creating and requiring the following module as the first require used by your application:

这可以通过创建并要求您的应用程序首先使用以下模块来实现:

// filename: module-override.js
const Module = require('module')

Module.MODULES = {
  LOADED: {},
  CACHED: {}
}
Module.__load = Module._load // save original _load()

Module._load = function (request, parent, isMain) { // redefine _load()
  const filename = Module._resolveFilename(request, parent, isMain)
  const key = filename.replace(/^\/Users\/USERNAME/, '...')
  if (!key.match(/node_modules/)) { // ignore paths with node_modules
    let val
    if (Module._cache[filename]) { // exists in Module_cache
      val = Module.MODULES.CACHED[key] || 0
      Module.MODULES.CACHED[key] = val + 1
    }
    val = Module.MODULES.LOADED[key] || 0
    Module.MODULES.LOADED[key] = val + 1
  }
  return Module.__load(request, parent, isMain) // the original _load()
}

Note: You can comment out the line which defines the key. I added that so the output of stringifying Module.MODULES did not contain any locally identifying information.

注意:可以注释定义键的行。我添加了这个,所以stringi化模块的输出。模块不包含任何本地标识信息。

Once you require this module, it will collect a count of all required modules in Module.MODULES.

一旦您需要这个模块,它将收集模块中所有需要的模块的计数。

Module.MODULES.LOADED will keep track of how many times a module has been required while Module.MODULES.CACHED will do the same for requires that have been fulfilled from the internal module cache.

Module.MODULES。加载将跟踪模块在模块. modules时需要多少次。缓存将执行从内部模块缓存中完成的要求。

Assume that I have two modules under ./lib named utils.js and Schedule.js where

假设我在./lib下有两个名为utils的模块。js和进度。js的地方

  • ./lib/utils requires util, path, events, debug, json-stringify-safe andlodash, and
  • /lib/utils需要util、path、events、debug、json-stringify-safe和lodash,以及。
  • Schedule.js requires path, colors and ./lib/utils
  • 时间表。js需要路径、颜色和./lib/utils

And that I have a top-level file that requires both ./lib/Schedule and ./lib/utils.

我有一个需要./lib/Schedule和./lib/utils的*文件。

After all the requires have completed, I can then call

所有的要求都完成之后,我就可以打电话了

console.log(JSON.stringify(Module.MODULES))

which will print to the console:

将打印到控制台:

{ 
  LOADED: {
    '..././lib/Schedule.js': 1,
     path: 2,
     '..././lib/utils.js': 2,
     util: 2,
     events: 1,
     tty: 1
   },
  CACHED: {
    '..././lib/utils.js': 1
  }
}

which tells me that

这告诉我,

  • ./lib/Schedule.js was loaded once from the file system,
  • / lib /时间表。js从文件系统加载一次,
  • path, util, events, and tty were loaded from nodeJS's internal modules, and
  • path、util、事件和tty都是从nodeJS的内部模块和
  • utils was loaded twice, once from its file and once from the internal require cache
  • utils被加载两次,一次来自文件,一次来自内部需要缓存

Without the test to weed out all modules found under /node_modules/ the output will be:

如果不进行删除/node_modules/中找到的所有模块的测试,输出将是:

{
  LOADED: {
    '.../lib/Schedule.js': 1,
    path: 2,
    '.../node_modules/colors/lib/index.js': 1,
    '.../node_modules/colors/lib/colors.js': 6,
    '.../node_modules/colors/lib/styles.js': 1,
    '.../node_modules/colors/lib/system/supports-colors.js': 1,
    '.../node_modules/colors/lib/custom/trap.js': 1,
    '.../node_modules/colors/lib/custom/zalgo.js': 1,
    '.../node_modules/colors/lib/maps/america.js': 1,
    '.../node_modules/colors/lib/maps/zebra.js': 1,
    '.../node_modules/colors/lib/maps/rainbow.js': 1,
    '.../node_modules/colors/lib/maps/random.js': 1,
    '.../node_modules/colors/lib/extendStringPrototype.js': 1,
    '.../lib/utils.js': 2,
    util: 2,
    events: 1,
    '/.../node_modules/lodash/lodash.js': 1,
    '/.../node_modules/json-stringify-safe/stringify.js': 1,
    '.../node_modules/debug/src/index.js': 1,
    '.../node_modules/debug/src/node.js': 1,
    tty: 1,
    '.../node_modules/debug/src/debug.js': 1,
    '.../node_modules/ms/index.js': 1,
    '/.../node_modules/supports-color/index.js': 1 },
  CACHED: {
    '.../node_modules/colors/lib/colors.js': 5,
    '.../lib/utils.js': 1
  }
}

相关文章