webpack-federation-types-plugin 1.2.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.
@@ -5,6 +5,7 @@ const fs = require("fs-extra")
5
5
  const path = require("path")
6
6
  const ms = require("ms")
7
7
 
8
+ let isFirstCompilation = true
8
9
  const PLUGIN_NAME = "FederationTypesPlugin"
9
10
  const MF_TYPES_DIR = "federation-types"
10
11
  const DECLARATION_FILE_EXT = ".d.ts"
@@ -58,13 +59,25 @@ class FederationTypesPlugin {
58
59
  if (!federationPlugin) throw new Error("No ModuleFederationPlugin found.")
59
60
  const logger = compiler.getInfrastructureLogger(PLUGIN_NAME)
60
61
 
61
- const exposes = Object.fromEntries(Object.entries(federationPlugin._options.exposes || {}).map(([k, v]) => [k.replace("./", ""), v]))
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
+ ))
62
77
 
63
78
  const modulesPathsMap = Object.fromEntries(
64
79
  Object.entries(exposes).map(([name, currentPath]) => {
65
- const absPath = path.resolve(process.cwd(), currentPath)
66
- const extension = path.extname(currentPath) ? "" : fs.existsSync(absPath + ".jsx") ? ".jsx" : ".js"
67
- return [name, path.join(process.cwd(), currentPath + extension).replace(/\\/g, "/")]
80
+ return [name, path.join(process.cwd(), currentPath).replace(/\\/g, "/")]
68
81
  })
69
82
  )
70
83
 
@@ -127,8 +140,8 @@ class FederationTypesPlugin {
127
140
  // get types index from the current remote
128
141
  .get(federationTypesUrl + "index.json")
129
142
  .catch((error) => {
130
- if (error.response?.status === 404) logger.warn(`WARNING: remote ${remoteName} has no types`)
131
- else logger.error("Failed to get remote types index", error.message)
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)
132
145
  })
133
146
  .then((response) => response?.data)
134
147
  .then((modulesNames) => {
@@ -166,6 +179,8 @@ class FederationTypesPlugin {
166
179
  compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {
167
180
  const pluginOptions = this._options || {}
168
181
  compilation.hooks.beforeCodeGeneration.tap(PLUGIN_NAME, () => {
182
+ if (!isFirstCompilation) return
183
+ isFirstCompilation = false
169
184
  if (pluginOptions.exposeTypes !== false) createTypesDefinitions(compilation)
170
185
  if (compilation.options.mode === "development" && pluginOptions.importTypes !== false) {
171
186
  getTypesDefinitions()
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.2.0",
4
+ "version": "1.2.1",
5
5
  "main": "FederationTypesPlugin.js",
6
6
  "types": "types.d.ts",
7
7
  "scripts": {