Skip to content

Commit 68c384d

Browse files
fix: node_module types for CLI tool
1 parent 5537e22 commit 68c384d

File tree

2 files changed

+12
-23
lines changed

2 files changed

+12
-23
lines changed

src/helpers/compileTypes.ts

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -98,30 +98,22 @@ export function rewritePathsWithExposedFederatedModules(
9898
outFile: string,
9999
typings: string,
100100
): void {
101-
const declareRegex = /declare module "(.*)"/g;
102-
const moduleImportPaths: string[] = [];
101+
const regexDeclareModule = /declare module "(.*)"/g;
102+
const declaredModulePaths: string[] = [];
103103

104+
// Collect all instances of `declare module "..."`
104105
let execResults: null | string[] = [];
105-
while ((execResults = declareRegex.exec(typings)) !== null) {
106-
moduleImportPaths.push(execResults[1]);
106+
while ((execResults = regexDeclareModule.exec(typings)) !== null) {
107+
declaredModulePaths.push(execResults[1]);
107108
}
108109

109110
let typingsUpdated: string = typings;
110111

111-
// Support aliases in paths (e.g. @/)
112-
// Aliases are not included in emitted declarations thus they have to be removed from the exposed path
113-
const aliasPaths = Object.values(getTSConfigCompilerOptions().paths || {}).map(alias => alias[0]);
114-
function substituteAliases(modulePath: string): string {
115-
aliasPaths.forEach(aliasPath => {
116-
modulePath = modulePath.replace(aliasPath, '');
117-
});
118-
return modulePath;
119-
}
120-
121112
// Replace and prefix paths by exposed remote names
122-
moduleImportPaths.forEach((importPath) => {
113+
declaredModulePaths.forEach((importPath) => {
114+
// Aliases are not included in the emitted declarations hence the need to use `endsWith`
123115
const [exposedModuleKey, ...exposedModuleNameAliases] = Object.keys(federationConfig.exposes)
124-
.filter(key => federationConfig.exposes[key].endsWith(substituteAliases(importPath)))
116+
.filter(key => federationConfig.exposes[key].endsWith(importPath))
125117
.map(key => key.replace(/^\.\//, ''));
126118

127119
let federatedModulePath = exposedModuleKey
@@ -143,6 +135,8 @@ export function rewritePathsWithExposedFederatedModules(
143135
].join('\n');
144136
});
145137

138+
typingsUpdated = includeTypesFromNodeModules(federationConfig, typingsUpdated);
139+
146140
mkdirp.sync(path.dirname(outFile));
147141
fs.writeFileSync(outFile, typingsUpdated.replace(/\r\n/g, '\n'));
148142
}

src/plugin.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,7 @@ import {
88
DIR_EMITTED_TYPES
99
} from './constants';
1010
import { getRemoteManifestUrls } from './helpers/cloudbedsRemoteManifests';
11-
import {
12-
compileTypes,
13-
includeTypesFromNodeModules,
14-
rewritePathsWithExposedFederatedModules
15-
} from './helpers/compileTypes';
11+
import { compileTypes, rewritePathsWithExposedFederatedModules } from './helpers/compileTypes';
1612
import { downloadTypes } from './helpers/downloadTypes';
1713
import { getLoggerHint, setLogger } from './helpers/logger';
1814
import { isEveryUrlValid } from './helpers/validation';
@@ -66,8 +62,7 @@ export class ModuleFederationTypesPlugin implements WebpackPluginInstance {
6662
const compileTypesHook = () => {
6763
const { isSuccess, typeDefinitions } = compileTypes(exposes as string[], outFile);
6864
if (isSuccess) {
69-
const typings = includeTypesFromNodeModules(federationPluginOptions as FederationConfig, typeDefinitions);
70-
rewritePathsWithExposedFederatedModules(federationPluginOptions as FederationConfig, outFile, typings);
65+
rewritePathsWithExposedFederatedModules(federationPluginOptions as FederationConfig, outFile, typeDefinitions);
7166
} else {
7267
logger.warn('Failed to compile types for exposed modules.', getLoggerHint(compiler));
7368
}

0 commit comments

Comments
 (0)