webpack-federation-stats-plugin 1.0.3-beta.0 → 1.1.1
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.
- package/FederationStatsPlugin.js +43 -36
- package/LICENSE +1 -1
- package/README.md +91 -17
- package/index.d.ts +4 -2
- package/package.json +5 -2
package/FederationStatsPlugin.js
CHANGED
|
@@ -3,20 +3,19 @@ const PLUGIN_NAME = "FederationStatsPlugin"
|
|
|
3
3
|
const EXTENSION_REGEX = /\.[^/.]+$/
|
|
4
4
|
|
|
5
5
|
class FederationStatsPlugin {
|
|
6
|
-
constructor(options = {
|
|
7
|
-
|
|
8
|
-
this._options = options
|
|
6
|
+
constructor(options = {}) {
|
|
7
|
+
this._options = {fileName: "federation-stats.json", ...options}
|
|
9
8
|
}
|
|
10
9
|
|
|
11
10
|
apply(compiler) {
|
|
12
|
-
const federationPlugin = compiler.options.plugins.find((plugin) => plugin.constructor.name === "ModuleFederationPlugin")
|
|
11
|
+
const federationPlugin = compiler.options.plugins && compiler.options.plugins.find((plugin) => plugin.constructor.name === "ModuleFederationPlugin")
|
|
13
12
|
|
|
14
|
-
if (!federationPlugin)
|
|
15
|
-
throw new Error("No ModuleFederationPlugin found.")
|
|
16
|
-
}
|
|
13
|
+
if (!federationPlugin) throw new Error("No ModuleFederationPlugin found.")
|
|
17
14
|
|
|
18
15
|
const appName = federationPlugin._options.name
|
|
19
|
-
|
|
16
|
+
|
|
17
|
+
// get exposed modules from the ModuleFederationPlugin
|
|
18
|
+
const exposedFiles = new Map(Object.entries(federationPlugin._options.exposes || {}).map(([k, v]) => (typeof v === "object" ? [v.import, k] : [v, k])))
|
|
20
19
|
|
|
21
20
|
compiler.hooks.thisCompilation.tap(PLUGIN_NAME, (compilation) => {
|
|
22
21
|
compilation.hooks.processAssets.tapPromise(
|
|
@@ -25,51 +24,59 @@ class FederationStatsPlugin {
|
|
|
25
24
|
stage: compilation.constructor.PROCESS_ASSETS_STAGE_REPORT,
|
|
26
25
|
},
|
|
27
26
|
async () => {
|
|
28
|
-
const stats = compilation.getStats().toJson()
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
return exposedFiles.has(moduleName)
|
|
32
|
-
})
|
|
27
|
+
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, "")))
|
|
33
30
|
|
|
34
31
|
const chunksReducer = (chunksArr, current) => {
|
|
35
32
|
current.siblings.forEach((s) => {
|
|
36
33
|
const chunk = stats.chunks.find((c) => c.id === s)
|
|
37
|
-
|
|
38
|
-
chunk.files.forEach((f) => chunksArr.push(f))
|
|
39
|
-
}
|
|
34
|
+
chunk.files.forEach((f) => chunksArr.push(f))
|
|
40
35
|
})
|
|
41
36
|
current.files.forEach((f) => chunksArr.push(f))
|
|
42
37
|
return chunksArr
|
|
43
38
|
}
|
|
44
39
|
|
|
45
|
-
const chunks =
|
|
46
|
-
const
|
|
47
|
-
|
|
48
|
-
if (exposedAs.startsWith("./")) {
|
|
49
|
-
exposedAs = exposedAs.substring(2)
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
const moduleChunks = module.chunks
|
|
40
|
+
const chunks = mfModules.map((module) => {
|
|
41
|
+
const exposedAs = exposedFiles.get(module.name.replace(EXTENSION_REGEX, ""))
|
|
42
|
+
const chunks = module.chunks
|
|
53
43
|
.map((chunkId) => stats.chunks.find((chunk) => chunk.id === chunkId))
|
|
54
|
-
.filter((chunk) => chunk
|
|
44
|
+
.filter((chunk) => chunk.runtime.includes(appName))
|
|
55
45
|
.reduce(chunksReducer, [])
|
|
46
|
+
return {
|
|
47
|
+
module: exposedAs,
|
|
48
|
+
chunks: chunks,
|
|
49
|
+
id: module.id,
|
|
50
|
+
}
|
|
51
|
+
})
|
|
56
52
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
}, {})
|
|
53
|
+
const exposes = chunks.reduce((result, current) => Object.assign(result, {[current.module.replace("./", "")]: current.chunks}), {})
|
|
54
|
+
const name = (federationPlugin._options.library && federationPlugin._options.library.name) || federationPlugin._options.name
|
|
60
55
|
|
|
61
56
|
const statsResult = {
|
|
62
|
-
name
|
|
63
|
-
exposes
|
|
64
|
-
|
|
57
|
+
name,
|
|
58
|
+
exposes,
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const publicUrl = this._options.publicUrl
|
|
62
|
+
if (publicUrl) {
|
|
63
|
+
statsResult.publicUrl = publicUrl
|
|
65
64
|
}
|
|
66
65
|
|
|
67
66
|
const fileName = this._options.fileName
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
67
|
+
const statsBuffer = Buffer.from(JSON.stringify(statsResult), "utf-8")
|
|
68
|
+
const mfStats = {
|
|
69
|
+
source: () => statsBuffer,
|
|
70
|
+
size: () => statsBuffer.length,
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const asset = compilation.getAsset(fileName)
|
|
74
|
+
if (asset) {
|
|
75
|
+
compilation.updateAsset(fileName, mfStats)
|
|
76
|
+
} else {
|
|
77
|
+
compilation.emitAsset(fileName, mfStats)
|
|
78
|
+
}
|
|
79
|
+
}
|
|
73
80
|
)
|
|
74
81
|
})
|
|
75
82
|
}
|
package/LICENSE
CHANGED
package/README.md
CHANGED
|
@@ -1,40 +1,114 @@
|
|
|
1
1
|
# webpack-federation-stats-plugin
|
|
2
2
|
|
|
3
|
+
[](https://www.npmjs.com/package/webpack-federation-stats-plugin)
|
|
4
|
+
[](https://www.npmjs.com/package/webpack-federation-stats-plugin)
|
|
5
|
+
[](https://www.npmjs.com/package/webpack-federation-stats-plugin)
|
|
6
|
+
[](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
|
+
|
|
3
10
|
## Installation
|
|
4
11
|
|
|
5
|
-
|
|
12
|
+
```bash
|
|
13
|
+
npm install --save-dev webpack-federation-stats-plugin
|
|
14
|
+
```
|
|
6
15
|
|
|
7
|
-
|
|
16
|
+
or
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
yarn add --dev webpack-federation-stats-plugin
|
|
20
|
+
```
|
|
8
21
|
|
|
9
22
|
## Usage
|
|
10
23
|
|
|
11
24
|
```javascript
|
|
12
|
-
const FederationStatsPlugin = require("webpack-federation-stats-plugin")
|
|
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")
|
|
45
|
+
|
|
46
|
+
module.exports = {
|
|
47
|
+
plugins: [
|
|
48
|
+
new FederationStatsPlugin({
|
|
49
|
+
fileName: "federation-stats.json",
|
|
50
|
+
publicUrl: "https://cdn.example.com/",
|
|
51
|
+
}),
|
|
52
|
+
],
|
|
53
|
+
}
|
|
54
|
+
```
|
|
55
|
+
|
|
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")
|
|
13
65
|
|
|
14
66
|
module.exports = {
|
|
15
67
|
plugins: [
|
|
16
|
-
new
|
|
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(),
|
|
17
77
|
],
|
|
18
|
-
}
|
|
78
|
+
}
|
|
19
79
|
```
|
|
20
80
|
|
|
21
|
-
|
|
81
|
+
The plugin will emit a `federation-stats.json` like this:
|
|
22
82
|
|
|
23
83
|
```json
|
|
24
84
|
{
|
|
25
|
-
"name": "
|
|
85
|
+
"name": "shop",
|
|
26
86
|
"exposes": {
|
|
27
|
-
"
|
|
28
|
-
|
|
29
|
-
|
|
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
|
-
]
|
|
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"]
|
|
37
90
|
}
|
|
38
91
|
}
|
|
92
|
+
```
|
|
93
|
+
|
|
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.
|
|
39
95
|
|
|
96
|
+
### With `publicUrl`
|
|
97
|
+
|
|
98
|
+
When a `publicUrl` is provided, it is included in the output so consumers can resolve the full URL for each chunk:
|
|
99
|
+
|
|
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
|
+
}
|
|
40
110
|
```
|
|
111
|
+
|
|
112
|
+
## License
|
|
113
|
+
|
|
114
|
+
[MIT](LICENSE)
|
package/index.d.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
interface FederationStatsPluginOptions {
|
|
2
|
-
|
|
3
|
-
|
|
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;
|
|
4
6
|
}
|
|
5
7
|
|
|
6
8
|
declare class FederationStatsPlugin {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "webpack-federation-stats-plugin",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.1",
|
|
4
4
|
"types": "index.d.ts",
|
|
5
5
|
"description": "export module federation stats",
|
|
6
6
|
"main": "FederationStatsPlugin.js",
|
|
@@ -24,5 +24,8 @@
|
|
|
24
24
|
"loadable-components"
|
|
25
25
|
],
|
|
26
26
|
"author": "Daniel Amenou <amenou.daniel@gmail.com>",
|
|
27
|
-
"license": "MIT"
|
|
27
|
+
"license": "MIT",
|
|
28
|
+
"engines": {
|
|
29
|
+
"node": ">=22"
|
|
30
|
+
}
|
|
28
31
|
}
|