Skip to content

Commit

Permalink
fix: replace original remote name with aliased in downloaded types
Browse files Browse the repository at this point in the history
  • Loading branch information
steven-pribilinskiy committed Sep 13, 2022
1 parent 2a96245 commit cdc3fd0
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions src/helpers/downloadTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,31 @@ async function downloadRemoteEntryManifest(url: string): Promise<unknown> {
return JSON.parse(json);
}

async function downloadRemoteEntryTypes(remoteName: string, dtsUrl: string, dirDownloadedTypes: string): Promise<void> {
async function downloadRemoteEntryTypes(
remoteName: string,
remoteLocation: string,
dtsUrl: string,
dirDownloadedTypes: string,
): Promise<void> {
const logger = getLogger();
const types = (await download(dtsUrl, downloadOptions)).toString();
const remoteOriginalName = remoteLocation.split('@')[0];
const outDir = path.join(dirDownloadedTypes, remoteName);
const outFile = path.join(outDir, 'index.d.ts');
let shouldWriteFile = true;

mkdirp.sync(outDir);

let types = (await download(dtsUrl, downloadOptions)).toString();

// Replace original remote name (as defined in remote microapp's WMF config's `name` field)
// with a name (an alias) that is used in `remotes` object. Usually these are same.
if (remoteName !== remoteOriginalName) {
types = types.replace(
new RegExp(`declare module "${remoteOriginalName}(.*)"`, 'g'),
(_, $1) => `declare module "${remoteName}${$1}"`
);
}

// Prevent webpack from recompiling the bundle by not writing the file if it has not changed
if (fs.existsSync(outFile)) {
const typesFormer = fs.readFileSync(outFile).toString();
Expand Down Expand Up @@ -99,6 +115,7 @@ export async function downloadTypes(
const remoteEntryBaseUrl = remoteEntryUrl.split('/').slice(0, -1).join('/');
const promiseDownload = downloadRemoteEntryTypes(
remoteName,
remoteLocation,
`${remoteEntryBaseUrl}/${dirEmittedTypes}/index.d.ts`,
dirDownloadedTypes,
)
Expand Down

0 comments on commit cdc3fd0

Please sign in to comment.