webpack-federation-stats-plugin 1.1.1 → 1.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1 @@
1
+ github: DanielAmenou
package/README.md CHANGED
@@ -1,114 +1,50 @@
1
1
  # webpack-federation-stats-plugin
2
2
 
3
- [![npm version](https://img.shields.io/npm/v/webpack-federation-stats-plugin)](https://www.npmjs.com/package/webpack-federation-stats-plugin)
4
- [![npm downloads](https://img.shields.io/npm/dt/webpack-federation-stats-plugin)](https://www.npmjs.com/package/webpack-federation-stats-plugin)
5
- [![npm monthly downloads](https://img.shields.io/npm/dm/webpack-federation-stats-plugin)](https://www.npmjs.com/package/webpack-federation-stats-plugin)
6
- [![license](https://img.shields.io/npm/l/webpack-federation-stats-plugin)](https://github.com/DanielAmenou/webpack-federation-stats-plugin/blob/main/LICENSE)
7
-
8
- A Webpack plugin that extracts [Module Federation](https://webpack.js.org/concepts/module-federation/) stats into a JSON file. It maps each exposed module to its required chunks, making it useful for tools like [loadable-components](https://loadable-components.com/) that need to know which chunks to load for a given federated module.
9
-
10
3
  ## Installation
11
4
 
12
- ```bash
13
- npm install --save-dev webpack-federation-stats-plugin
14
- ```
5
+ `npm i --save-dev webpack-federation-stats-plugin`
15
6
 
16
- or
17
-
18
- ```bash
19
- yarn add --dev webpack-federation-stats-plugin
20
- ```
7
+ `yarn add --dev webpack-federation-stats-plugin`
21
8
 
22
9
  ## Usage
23
10
 
24
11
  ```javascript
25
- const FederationStatsPlugin = require("webpack-federation-stats-plugin")
26
-
27
- module.exports = {
28
- plugins: [new FederationStatsPlugin()],
29
- }
30
- ```
31
-
32
- > **Note:** The plugin requires `ModuleFederationPlugin` to be configured in the same Webpack config. It will throw an error if it is not found.
33
-
34
- ## Options
35
-
36
- | Option | Type | Default | Description |
37
- | ----------- | -------- | ------------------------- | ------------------------------------------------ |
38
- | `fileName` | `string` | `"federation-stats.json"` | The name of the output JSON file. |
39
- | `publicUrl` | `string` | — | An optional public URL to include in the output. |
40
-
41
- ### Example with options
42
-
43
- ```javascript
44
- const FederationStatsPlugin = require("webpack-federation-stats-plugin")
12
+ const FederationStatsPlugin = require("webpack-federation-stats-plugin");
45
13
 
46
14
  module.exports = {
47
15
  plugins: [
48
- new FederationStatsPlugin({
49
- fileName: "federation-stats.json",
50
- publicUrl: "https://cdn.example.com/",
51
- }),
16
+ new FederationStatsPlugin({filename: "federation-stats.json"}),
52
17
  ],
53
- }
18
+ };
54
19
  ```
55
20
 
56
- ## Output
57
-
58
- The plugin generates a JSON file that maps each exposed module to the chunk files it needs at runtime. This is the information a consuming application needs to know **which scripts to load** before importing a federated module.
59
-
60
- Given the following Module Federation config:
61
-
62
- ```javascript
63
- const {ModuleFederationPlugin} = require("webpack").container
64
- const FederationStatsPlugin = require("webpack-federation-stats-plugin")
65
-
66
- module.exports = {
67
- plugins: [
68
- new ModuleFederationPlugin({
69
- name: "shop",
70
- exposes: {
71
- "./ProductCard": "./src/components/ProductCard",
72
- "./CartIcon": "./src/components/CartIcon",
73
- "./useCart": "./src/hooks/useCart",
74
- },
75
- }),
76
- new FederationStatsPlugin(),
77
- ],
78
- }
79
- ```
80
-
81
- The plugin will emit a `federation-stats.json` like this:
21
+ ### Example Output
82
22
 
83
23
  ```json
84
24
  {
85
- "name": "shop",
25
+ "name": "AppName",
86
26
  "exposes": {
87
- "ProductCard": ["vendors-node_modules_react-dom_index_js.js", "vendors-node_modules_styled-components_dist_index_js.js", "src_components_ProductCard_index_tsx.js"],
88
- "CartIcon": ["vendors-node_modules_react-dom_index_js.js", "src_components_CartIcon_index_tsx.js"],
89
- "useCart": ["src_hooks_useCart_ts.js"]
27
+ "module1": [
28
+ "vendors-node_modules_babel_runtime_helpers_esm_slicedToArray.js",
29
+ "vendors-node_modules_core-js.js",
30
+ "vendors-node_modules_prop-types_index_js.js",
31
+ ],
32
+ "module2": ["vendors-node_modules_core-js.js",],
33
+ "module3": [
34
+ "vendors-node_modules_babel.js",
35
+ "vendors-node_modules_core-js.js",
36
+ ]
90
37
  }
91
38
  }
92
- ```
93
39
 
94
- Each key under `exposes` corresponds to an exposed module (without the `./` prefix), and its value is the list of chunk files that must be loaded for that module to work.
40
+ ```
95
41
 
96
- ### With `publicUrl`
42
+ ## Support
97
43
 
98
- When a `publicUrl` is provided, it is included in the output so consumers can resolve the full URL for each chunk:
44
+ If you find this project helpful, consider supporting the author:
99
45
 
100
- ```json
101
- {
102
- "name": "shop",
103
- "publicUrl": "https://cdn.example.com/shop/",
104
- "exposes": {
105
- "ProductCard": ["vendors-node_modules_react-dom_index_js.js", "vendors-node_modules_styled-components_dist_index_js.js", "src_components_ProductCard_index_tsx.js"],
106
- "CartIcon": ["vendors-node_modules_react-dom_index_js.js", "src_components_CartIcon_index_tsx.js"],
107
- "useCart": ["src_hooks_useCart_ts.js"]
108
- }
109
- }
110
- ```
46
+ - [GitHub Sponsors](https://github.com/sponsors/DanielAmenou)
111
47
 
112
48
  ## License
113
49
 
114
- [MIT](LICENSE)
50
+ MIT
@@ -3,18 +3,17 @@ const PLUGIN_NAME = "FederationStatsPlugin"
3
3
  const EXTENSION_REGEX = /\.[^/.]+$/
4
4
 
5
5
  class FederationStatsPlugin {
6
- constructor(options = {}) {
7
- this._options = {fileName: "federation-stats.json", ...options}
6
+ constructor(options = {fileName: "federation-stats.json"}) {
7
+ if (!options || !options.fileName) throw new Error("fileName option is required.")
8
+ this._options = options
8
9
  }
9
10
 
10
11
  apply(compiler) {
12
+ const logger = compiler.getInfrastructureLogger(PLUGIN_NAME)
11
13
  const federationPlugin = compiler.options.plugins && compiler.options.plugins.find((plugin) => plugin.constructor.name === "ModuleFederationPlugin")
12
14
 
13
15
  if (!federationPlugin) throw new Error("No ModuleFederationPlugin found.")
14
16
 
15
- const appName = federationPlugin._options.name
16
-
17
- // get exposed modules from the ModuleFederationPlugin
18
17
  const exposedFiles = new Map(Object.entries(federationPlugin._options.exposes || {}).map(([k, v]) => (typeof v === "object" ? [v.import, k] : [v, k])))
19
18
 
20
19
  compiler.hooks.thisCompilation.tap(PLUGIN_NAME, (compilation) => {
@@ -25,9 +24,7 @@ class FederationStatsPlugin {
25
24
  },
26
25
  async () => {
27
26
  const stats = compilation.getStats().toJson({})
28
- // find mf modules
29
- const mfModules = stats.modules.filter((module) => module.issuerName === "container entry" && exposedFiles.has(module.name.replace(EXTENSION_REGEX, "")))
30
-
27
+ const modules = stats.modules.filter((module) => module.issuerName === "container entry" && exposedFiles.has(module.name.replace(EXTENSION_REGEX, "")))
31
28
  const chunksReducer = (chunksArr, current) => {
32
29
  current.siblings.forEach((s) => {
33
30
  const chunk = stats.chunks.find((c) => c.id === s)
@@ -36,21 +33,17 @@ class FederationStatsPlugin {
36
33
  current.files.forEach((f) => chunksArr.push(f))
37
34
  return chunksArr
38
35
  }
39
-
40
- const chunks = mfModules.map((module) => {
36
+ const chunks = modules.map((module) => {
41
37
  const exposedAs = exposedFiles.get(module.name.replace(EXTENSION_REGEX, ""))
42
- const chunks = module.chunks
43
- .map((chunkId) => stats.chunks.find((chunk) => chunk.id === chunkId))
44
- .filter((chunk) => chunk.runtime.includes(appName))
45
- .reduce(chunksReducer, [])
38
+ const chunks = module.chunks.map((chunkId) => stats.chunks.find((chunk) => chunk.id === chunkId)).reduce(chunksReducer, [])
46
39
  return {
47
40
  module: exposedAs,
48
41
  chunks: chunks,
49
- id: module.id,
42
+ id: module.id
50
43
  }
51
44
  })
52
45
 
53
- const exposes = chunks.reduce((result, current) => Object.assign(result, {[current.module.replace("./", "")]: current.chunks}), {})
46
+ const exposes = chunks.reduce((p, c) => Object.assign(p, {[c.module.replace("./", "")]: c.chunks}), {})
54
47
  const name = (federationPlugin._options.library && federationPlugin._options.library.name) || federationPlugin._options.name
55
48
 
56
49
  const statsResult = {
@@ -58,23 +51,18 @@ class FederationStatsPlugin {
58
51
  exposes,
59
52
  }
60
53
 
61
- const publicUrl = this._options.publicUrl
62
- if (publicUrl) {
63
- statsResult.publicUrl = publicUrl
64
- }
65
-
66
54
  const fileName = this._options.fileName
67
55
  const statsBuffer = Buffer.from(JSON.stringify(statsResult), "utf-8")
68
- const mfStats = {
56
+ const statsSource = {
69
57
  source: () => statsBuffer,
70
58
  size: () => statsBuffer.length,
71
59
  }
72
60
 
73
61
  const asset = compilation.getAsset(fileName)
74
62
  if (asset) {
75
- compilation.updateAsset(fileName, mfStats)
63
+ compilation.updateAsset(fileName, statsSource)
76
64
  } else {
77
- compilation.emitAsset(fileName, mfStats)
65
+ compilation.emitAsset(fileName, statsSource)
78
66
  }
79
67
  }
80
68
  )
package/package.json CHANGED
@@ -1,17 +1,18 @@
1
1
  {
2
2
  "name": "webpack-federation-stats-plugin",
3
- "version": "1.1.1",
4
- "types": "index.d.ts",
3
+ "version": "1.1.2",
5
4
  "description": "export module federation stats",
6
- "main": "FederationStatsPlugin.js",
5
+ "main": "index.js",
7
6
  "scripts": {
8
- "test": "echo \"Error: no test specified\" && exit 1",
9
- "publish:beta": "npm version prerelease --preid=beta -m \"beta version - %s\" && npm publish --tag beta",
10
7
  "publish:patch": "npm version patch -m \"patch version - %s\" && npm publish",
11
8
  "publish:minor": "npm version minor -m \"minor version - %s\" && npm publish",
12
9
  "publish:major": "npm version major -m \"major version - %s\" && npm publish",
13
10
  "postpublish": "git push && git push --tags"
14
11
  },
12
+ "funding": {
13
+ "type": "github",
14
+ "url": "https://github.com/sponsors/DanielAmenou"
15
+ },
15
16
  "homepage": "https://github.com/DanielAmenou/webpack-federation-stats-plugin#readme",
16
17
  "repository": {
17
18
  "type": "git",
@@ -20,12 +21,8 @@
20
21
  "keywords": [
21
22
  "webpack",
22
23
  "plugin",
23
- "module federation",
24
- "loadable-components"
24
+ "module federation"
25
25
  ],
26
26
  "author": "Daniel Amenou <amenou.daniel@gmail.com>",
27
- "license": "MIT",
28
- "engines": {
29
- "node": ">=22"
30
- }
27
+ "license": "MIT"
31
28
  }
package/LICENSE DELETED
@@ -1,21 +0,0 @@
1
- MIT License
2
-
3
- Copyright (c) 2026 DanielAmenou
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
package/index.d.ts DELETED
@@ -1,14 +0,0 @@
1
- interface FederationStatsPluginOptions {
2
- /** The name of the output JSON file. Defaults to `"federation-stats.json"`. */
3
- fileName?: string;
4
- /** An optional public URL to include in the output. */
5
- publicUrl?: string;
6
- }
7
-
8
- declare class FederationStatsPlugin {
9
- constructor(options: FederationStatsPluginOptions);
10
-
11
- apply(compiler: any): void;
12
- }
13
-
14
- export = FederationStatsPlugin;