Skip to content

Commit

Permalink
fix: Utilities - Remove module side effect (#488)
Browse files Browse the repository at this point in the history
* Remove module side effects

* Remove var

* fix(typescript): generated types doesn't get included in types index (#487)

* fix(typescript): defined exposes declaration files now consumable
* fix(typescript): generateTypesStats now accounts for absolute paths when setting filename

* fix: [node] build error when no remotes are used (#500)

* fix(federatedtypesplugin): modify remote url used to import remote types (#496)

url derived from remote components urls previously failed when fetching types when package has an
organization scope. by taking the split index and taking a substring of the remote url at that index
fixes this issue

fix #495

Co-authored-by: Zack Jackson <zackary.l.jackson@gmail.com>

* feat: [node] build error when no remotes are used

* chore(node): release version 0.10.0

* chore(nextjs-mf): release version 6.0.2

* fix(typescript): unable to download types from multiple remotes (#492)

fixes #455

* Utilities - add license information (#491)

* add license information to utilities

* Utilities - Add LICENSE file

Co-authored-by: Zack Jackson <zackary.l.jackson@gmail.com>

* Regen lock file

* fix(federatedtypesplugin): modify remote url used to import remote types (#496)

url derived from remote components urls previously failed when fetching types when package has an
organization scope. by taking the split index and taking a substring of the remote url at that index
fixes this issue

fix #495

Co-authored-by: Zack Jackson <zackary.l.jackson@gmail.com>

Co-authored-by: dmcgoughpax8 <dmcgough@pax8.com>
Co-authored-by: Zack Jackson <zackary.l.jackson@gmail.com>
Co-authored-by: Austin Howard <ahoward@austinhoward.tech>
Co-authored-by: GitHub Bot <gituser@example.com>
Co-authored-by: jon <jonathanceddy@gmail.com>
  • Loading branch information
6 people authored Jan 12, 2023
1 parent 3f3a68f commit 3554de7
Show file tree
Hide file tree
Showing 3 changed files with 2,014 additions and 1,717 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
"@typescript-eslint/eslint-plugin": "5.47.1",
"@typescript-eslint/parser": "5.47.1",
"commitizen": "^4.2.5",
"concurrently": "7.6.0",
"cz-conventional-changelog": "^3.3.0",
"eslint": "~8.30.0",
"eslint-config-next": "13.0.0",
Expand Down Expand Up @@ -109,4 +110,3 @@
]
}
}

90 changes: 48 additions & 42 deletions packages/utilities/src/utils/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,15 @@ import type {
Remotes,
RuntimeRemotesMap,
RuntimeRemote,
WebpackRemoteContainer,
} from '../types';

let remoteVars = {} as Record<
type RemoteVars = Record<
string,
Promise<any> | string | (() => Promise<any>)
| Promise<WebpackRemoteContainer>
| string
| (() => Promise<WebpackRemoteContainer>)
>;
try {
// @ts-ignore
remoteVars = (process.env.REMOTES || {}) as Record<
string,
Promise<any> | string | (() => Promise<any>)
>;
} catch (e) {
console.error('Error parsing REMOTES environment variable', e);
}

// split the @ syntax into url and global
export const extractUrlAndGlobal = (urlAndGlobal: string): [string, string] => {
Expand All @@ -30,36 +24,43 @@ export const extractUrlAndGlobal = (urlAndGlobal: string): [string, string] => {
return [urlAndGlobal.substring(index + 1), urlAndGlobal.substring(0, index)];
};

export const runtimeRemotes = Object.entries(remoteVars).reduce(function (
acc,
item
) {
const [key, value] = item;
// if its an object with a thenable (eagerly executing function)
if (typeof value === 'object' && typeof value.then === 'function') {
acc[key] = { asyncContainer: value };
}
// if its a function that must be called (lazily executing function)
else if (typeof value === 'function') {
// @ts-ignore
acc[key] = { asyncContainer: value };
}
// if its just a string (global@url)
else if (typeof value === 'string') {
const [url, global] = extractUrlAndGlobal(value);
acc[key] = { global, url };
}
// we dont know or currently support this type
else {
//@ts-ignore
console.log('remotes process', process.env.REMOTES);
throw new Error(`[mf] Invalid value received for runtime_remote "${key}"`);
}
return acc;
},
{} as RuntimeRemotesMap);
const getRuntimeRemotes = () => {
//@ts-ignore
const remoteVars = (process.env.REMOTES || {}) as RemoteVars;

const runtimeRemotes = Object.entries(remoteVars).reduce(function (
acc,
item
) {
const [key, value] = item;
// if its an object with a thenable (eagerly executing function)
if (typeof value === 'object' && typeof value.then === 'function') {
acc[key] = { asyncContainer: value };
}
// if its a function that must be called (lazily executing function)
else if (typeof value === 'function') {
// @ts-ignore
acc[key] = { asyncContainer: value };
}
// if its just a string (global@url)
else if (typeof value === 'string') {
const [url, global] = extractUrlAndGlobal(value);
acc[key] = { global, url };
}
// we dont know or currently support this type
else {
//@ts-ignore
console.log('remotes process', process.env.REMOTES);
throw new Error(
`[mf] Invalid value received for runtime_remote "${key}"`
);
}
return acc;
},
{} as RuntimeRemotesMap);

export const remotes = runtimeRemotes;
return runtimeRemotes;
};

/**
* Return initialized remote container by remote's key or its runtime remote item data.
Expand All @@ -72,6 +73,8 @@ export const remotes = runtimeRemotes;
export const injectScript = (
keyOrRuntimeRemoteItem: string | RuntimeRemote
) => {
const runtimeRemotes = getRuntimeRemotes();

// 1) Load remote container if needed
let asyncContainer: RuntimeRemote['asyncContainer'];
const reference =
Expand All @@ -80,8 +83,11 @@ export const injectScript = (
: keyOrRuntimeRemoteItem;

if (reference.asyncContainer) {
// @ts-ignore
asyncContainer = typeof reference.asyncContainer.then === 'function' ? reference.asyncContainer : reference.asyncContainer();
asyncContainer =
typeof reference.asyncContainer.then === 'function'
? reference.asyncContainer
: // @ts-ignore
reference.asyncContainer();
} else {
// This casting is just to satisfy typescript,
// In reality remoteGlobal will always be a string;
Expand Down
Loading

0 comments on commit 3554de7

Please sign in to comment.