webpack-federation-types-plugin 1.1.0 → 1.2.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/FederationTypesPlugin.js +30 -7
- package/README.md +6 -0
- package/TODO +5 -0
- package/package.json +2 -1
package/FederationTypesPlugin.js
CHANGED
|
@@ -3,7 +3,9 @@ const ts = require("typescript")
|
|
|
3
3
|
const axios = require("axios")
|
|
4
4
|
const fs = require("fs-extra")
|
|
5
5
|
const path = require("path")
|
|
6
|
+
const ms = require("ms")
|
|
6
7
|
|
|
8
|
+
let isFirstCompilation = true
|
|
7
9
|
const PLUGIN_NAME = "FederationTypesPlugin"
|
|
8
10
|
const MF_TYPES_DIR = "federation-types"
|
|
9
11
|
const DECLARATION_FILE_EXT = ".d.ts"
|
|
@@ -26,6 +28,9 @@ const optionsSchema = {
|
|
|
26
28
|
importTypes: {
|
|
27
29
|
type: "boolean",
|
|
28
30
|
},
|
|
31
|
+
getTypesInterval: {
|
|
32
|
+
type: "string",
|
|
33
|
+
},
|
|
29
34
|
},
|
|
30
35
|
}
|
|
31
36
|
|
|
@@ -48,18 +53,31 @@ class FederationTypesPlugin {
|
|
|
48
53
|
* @param {import("webpack").Compiler} compiler
|
|
49
54
|
*/
|
|
50
55
|
apply(compiler) {
|
|
56
|
+
let getTypesInterval
|
|
51
57
|
const federationPlugin =
|
|
52
58
|
compiler.options.plugins && compiler.options.plugins.find((plugin) => plugin.constructor.name === "ModuleFederationPlugin")
|
|
53
59
|
if (!federationPlugin) throw new Error("No ModuleFederationPlugin found.")
|
|
54
60
|
const logger = compiler.getInfrastructureLogger(PLUGIN_NAME)
|
|
55
61
|
|
|
56
|
-
const exposes = Object.fromEntries(Object.entries(federationPlugin._options.exposes || {}).map(([
|
|
62
|
+
const exposes = Object.fromEntries(Object.entries(federationPlugin._options.exposes || {}).map(([moduleName, currentPath]) => {
|
|
63
|
+
let isIndexFile = false
|
|
64
|
+
const absPath = path.resolve(process.cwd(), currentPath)
|
|
65
|
+
const hasExtension = !!path.extname(currentPath)
|
|
66
|
+
const extension = hasExtension ? "" : fs.existsSync(absPath + ".jsx") ? ".jsx" : fs.existsSync(absPath + ".js") ? ".js" : ""
|
|
67
|
+
if (!hasExtension && extension === "") {
|
|
68
|
+
if ( fs.existsSync(path.resolve(absPath, "index.js"))) {
|
|
69
|
+
isIndexFile = true
|
|
70
|
+
}
|
|
71
|
+
else logger.error(`Couldn't find ${currentPath}`)
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return [moduleName.replace("./", ""), currentPath + (isIndexFile ? "/index.js" : "") + extension]
|
|
75
|
+
}
|
|
76
|
+
))
|
|
57
77
|
|
|
58
78
|
const modulesPathsMap = Object.fromEntries(
|
|
59
79
|
Object.entries(exposes).map(([name, currentPath]) => {
|
|
60
|
-
|
|
61
|
-
const extension = path.extname(currentPath) ? "" : fs.existsSync(absPath + ".jsx") ? ".jsx" : ".js"
|
|
62
|
-
return [name, path.join(process.cwd(), currentPath + extension).replace(/\\/g, "/")]
|
|
80
|
+
return [name, path.join(process.cwd(), currentPath).replace(/\\/g, "/")]
|
|
63
81
|
})
|
|
64
82
|
)
|
|
65
83
|
|
|
@@ -122,8 +140,8 @@ class FederationTypesPlugin {
|
|
|
122
140
|
// get types index from the current remote
|
|
123
141
|
.get(federationTypesUrl + "index.json")
|
|
124
142
|
.catch((error) => {
|
|
125
|
-
if (error.response?.status === 404) logger.warn(`WARNING: remote ${remoteName} has no types`)
|
|
126
|
-
else logger.error(
|
|
143
|
+
if (error.response?.status === 404) logger.warn(`WARNING: The remote ${remoteName} has no types`)
|
|
144
|
+
else logger.error(`Failed to get remote types from ${remotePublicUrl}.`, error.message)
|
|
127
145
|
})
|
|
128
146
|
.then((response) => response?.data)
|
|
129
147
|
.then((modulesNames) => {
|
|
@@ -161,10 +179,15 @@ class FederationTypesPlugin {
|
|
|
161
179
|
compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {
|
|
162
180
|
const pluginOptions = this._options || {}
|
|
163
181
|
compilation.hooks.beforeCodeGeneration.tap(PLUGIN_NAME, () => {
|
|
182
|
+
if (!isFirstCompilation) return
|
|
183
|
+
isFirstCompilation = false
|
|
164
184
|
if (pluginOptions.exposeTypes !== false) createTypesDefinitions(compilation)
|
|
165
185
|
if (compilation.options.mode === "development" && pluginOptions.importTypes !== false) {
|
|
166
186
|
getTypesDefinitions()
|
|
167
|
-
|
|
187
|
+
if (pluginOptions.getTypesInterval) {
|
|
188
|
+
clearInterval(getTypesInterval)
|
|
189
|
+
getTypesInterval = setInterval(() => getTypesDefinitions(compilation), ms(pluginOptions.getTypesInterval))
|
|
190
|
+
}
|
|
168
191
|
}
|
|
169
192
|
})
|
|
170
193
|
})
|
package/README.md
CHANGED
package/TODO
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
cts\wedding\node_modules\@types\appJAMain\getJAData.d.ts'
|
|
2
|
+
(Use `node --trace-warnings ...` to show where the warning was created)
|
|
3
|
+
(node:10492) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 15)
|
|
4
|
+
(node:10492) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
|
|
5
|
+
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "webpack-federation-types-plugin",
|
|
3
3
|
"description": "This plugin adds type definitions for remote modules",
|
|
4
|
-
"version": "1.1
|
|
4
|
+
"version": "1.2.1",
|
|
5
5
|
"main": "FederationTypesPlugin.js",
|
|
6
6
|
"types": "types.d.ts",
|
|
7
7
|
"scripts": {
|
|
@@ -26,6 +26,7 @@
|
|
|
26
26
|
"dependencies": {
|
|
27
27
|
"axios": "^1.2.2",
|
|
28
28
|
"fs-extra": "^11.1.0",
|
|
29
|
+
"ms": "^2.1.3",
|
|
29
30
|
"schema-utils": "^4.0.0",
|
|
30
31
|
"typescript": "^4.9.4"
|
|
31
32
|
}
|