{"id":20906215,"url":"https://github.com/webpack-config/instrument-context-module","last_synced_at":"2025-07-26T21:09:57.798Z","repository":{"id":57150570,"uuid":"84887692","full_name":"webpack-config/instrument-context-module","owner":"webpack-config","description":null,"archived":false,"fork":false,"pushed_at":"2017-03-14T00:33:46.000Z","size":28,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-01-19T14:28:38.900Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/webpack-config.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-03-14T00:27:17.000Z","updated_at":"2017-06-12T09:19:51.000Z","dependencies_parsed_at":"2022-09-03T18:22:35.643Z","dependency_job_id":null,"html_url":"https://github.com/webpack-config/instrument-context-module","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/webpack-config%2Finstrument-context-module","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/webpack-config%2Finstrument-context-module/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/webpack-config%2Finstrument-context-module/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/webpack-config%2Finstrument-context-module/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/webpack-config","download_url":"https://codeload.github.com/webpack-config/instrument-context-module/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243300609,"owners_count":20269258,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-18T13:32:31.700Z","updated_at":"2025-03-12T21:42:05.751Z","avatar_url":"https://github.com/webpack-config.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"#Instrument Context Module\n\nA utility for adding instrumentation to [Webpack] dynamic [context modules](https://webpack.js.org/guides/dependency-management/#require-context).\n\n[![license](http://img.shields.io/npm/l/instrument-context-module.svg?style=flat)](https://www.npmjs.com/package/instrument-context-module)\n[![version](http://img.shields.io/npm/v/instrument-context-module.svg?style=flat)](https://www.npmjs.com/package/instrument-context-module)\n[![downloads](http://img.shields.io/npm/dm/instrument-context-module.svg?style=flat)](https://www.npmjs.com/package/instrument-context-module)\n\n## Usage\n\nCertain Webpack plugins use a set of regular expressions to filter modules, a context expression to match a directory and a module expression to filter files within the directory.\n\n`instrumentContextModule` is used to wrap a set of regular expressions used by webpack plugins to enable monitoring the matches found by the plugins.\n\n```js\n\nconst handleContext = (context, contextRegEx, moduleRegEx) =\u003e\n  console.log('in context ' + context);\n\nconst handleModule = (module, context, contextRegEx, moduleRegEx) =\u003e\n  console.log('include module ' + module);\n\nconst wrap = instrumentContextModule(handleContext, handleModule);\n\nconst moduleRegExp = wrap.module(/(foo|bar)\\.js$/);\nconst contextRegExp = wrap.module(/some_module/);\n\nconst plugin = new webpack.ContextReplacementPlugin(\n  contextRegExp,\n  moduleRegExp,\n);\n```\n\nThe `handleContext` function is called for the first module match within a context. It receives as arguments in order:\n - The path of the context.\n - The regular expression that matched the context.\n - The regular expression that matched the module.\n\nThe `handleModule` function is called for every module match. It receives as arguments in order:\n\n - The require path of the module, this may be relative to the context.\n - The path of the context.\n - The regular expression that matched the context.\n - The regular expression that matched the module.\n\n Since the result of `instrumentContextModule` is stateful, a new instance must be created for each plugin to be instrumented.\n\n## Examples\n\n### Logging dynamically included modules.\n\nWith a non-restrictive module regular expression, an instrumented [`ContextReplacementPlugin`] can be used to simply observe and log what is included in a certain context.\n\n**Webpack config**\n\n```js\n// webpack.config.babel.js\n// \nimport {join, relative, dirname} from 'path';\nimport webpack from 'webpack';\nimport instrumentContextModule from 'instrument-context-module';\nimport escapeRegExp from 'escape-string-regexp';\nimport nearest from 'find-nearest-file';\n\nconst ROOT = dirname(nearest('package.json'));\n\nconst logInclude = instrumentContextModule(\n  (context, contextRegEx, moduleRegEx) =\u003e\n    console.log(`🗃  Including ${moduleRegEx} in ${relative(ROOT, context)}`),\n  (module, context) =\u003e console.log(`   ${module.slice(2)}`),\n);\n\nconst iconPath = join(ROOT, 'src', 'asset', 'icon');\n\nexport default {\n\n  // Existing webpack config.\n  \n  context: ROOT,\n  plugins: [\n    new webpack.definePlugin({\n      process.env.ICON_PATH: JSON.stringify(iconPath),\n    }),\n    new webpack.ContextReplacementPlugin(\n      logInclude.context(new RegExp(`^${escapeRegExp(iconPath)}$`, 'i')),\n      logInclude.module(/\\.icon\\.svg$/),\n    )\n  ],\n};\n```\n\n**Component file**\n\n```js\nconst icons = require.context(process.env.ICON_PATH, false, /\\.icon\\.svg$/);\n```\n\n**Console output**\n\n```\n🗃 Including /\\.icon\\.svg$/ in src/asset/icon\n   arrow-down.icon.svg\n   arrow-left.icon.svg\n   arrow-right.icon.svg\n   arrow-up.icon.svg\n   chevron-left.icon.svg\n   ...\n```\n\n--\n\n### Logging dynamically ignored modules.\n\n(Waiting on https://github.com/webpack/webpack/pull/4418)\n\n[`IgnorePlugin`] can be instrumented to log which modules are excluded.\n\n\u003e Note that the arguments to `IgnorePlugin` reversed compared to `ContextReplacementPlugin`.\n\n**Webpack config**\n\n```js\n// webpack.config.babel.js\n// \nimport {join, relative, dirname} from 'path';\nimport webpack from 'webpack';\nimport instrumentContextModule from 'instrument-context-module';\nimport nearest from 'find-nearest-file';\n\nconst ROOT = dirname(nearest('package.json'));\n\nconst LOCALES = ['en', 'fr'];\n\nconst logIngore = instrumentContextModule(\n  (context, contextRegEx, moduleRegEx) =\u003e\n    console.log(`🚫  Ignoring ${moduleRegEx} in ${relative(ROOT, context)}`),\n  (module) =\u003e console.log(`   ${module.slice(2)}`),\n);\n\nexport default {\n\n  // Existing webpack config.\n  \n  context: ROOT,\n  plugins: [\n    new new webpack.IgnorePlugin(\n      logIngore.module(new RegExp(`^\\\\.\\\\/(?!${LOCALES.join('|')}).*\\\\.js$`)),\n      logIngore.context(/moment[\\/\\\\]locale$/),\n    )\n  ],\n};\n```\n\n**Console output**\n\n```\n🚫 Ignoring /^\\.\\/(?!en|fr).*\\.js$/ in node_modules/moment/locale\n   af.js\n   ar-dz.js\n   ar-ly.js\n   ar-ma.js\n   ar-sa.js\n   ar-tn.js\n   ar.js\n   az.js\n   be.js\n   bg.js\n   bn.js\n   bo.js\n   br.js\n   ...\n```\n\n\n[Webpack]: https://webpack.github.io\n[context modules]: https://webpack.js.org/guides/dependency-management/#require-contextextract-text-webpack-plugin\n[`ContextReplacementPlugin`]: https://github.com/webpack/docs/wiki/list-of-plugins#contextreplacementplugin\n[`IgnorePlugin`]: https://github.com/webpack/docs/wiki/list-of-plugins#ignoreplugin\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwebpack-config%2Finstrument-context-module","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwebpack-config%2Finstrument-context-module","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwebpack-config%2Finstrument-context-module/lists"}