Skip to content

Commit

Permalink
vite typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
DileSoft committed Dec 21, 2024
1 parent 058271b commit f514f12
Show file tree
Hide file tree
Showing 6 changed files with 8 additions and 127 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ package-lock.json
# ioBroker dev-server
.dev-server/
/src/dist
/src-editor/dist
/src-editor/.__mf__temp
26 changes: 0 additions & 26 deletions src-editor/craco.config.js

This file was deleted.

21 changes: 0 additions & 21 deletions src-editor/modulefederation.config.js

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import type { GenericBlock } from '../GenericBlock';
import type { DebugMessage } from '../../types';

const ADAPTERS: Record<string, typeof GenericBlock<any> | null> = {
telegram: ActionTelegram,
// telegram: ActionTelegram,
email: ActionSendEmail,
sayit: ActionSayText,
pushover: ActionPushover,
Expand Down Expand Up @@ -48,84 +48,6 @@ export const ContextWrapperCreate = createContext<RuleContext>({
setEnableSimulation: (_enableSimulation: boolean): void => {},
});

const getOrLoadRemote = (
remote: string,
shareScope: string,
remoteFallbackUrl: string | undefined = undefined,
): Promise<{ get: (module: string) => () => Promise<{ default: typeof GenericBlock<any> }> }> =>
new Promise((resolve, reject) => {
// check if remote exists on window
if (!(window as any)[remote]) {
// search dom to see if remote tag exists, but might still be loading (async)
const existingRemote: HTMLScriptElement | null = document.querySelector(`[data-webpack="${remote}"]`);

// when remote is loaded...
const onload = async (): Promise<void> => {
// check if it was initialized
if (!(window as any)[remote]) {
reject(new Error(`Cannot load Remote "${remote}" to inject`));
return;
}
if (!(window as any)[remote].__initialized) {
// if share scope doesn't exist (like in webpack 4) then expect shareScope to be a manual object
// @ts-expect-error it is a trick and must be so
if (typeof __webpack_share_scopes__ === 'undefined') {
// use default share scope object passed in manually
await (window as any)[remote].init(shareScope);
} else {
// otherwise, init share scope as usual
// @ts-expect-error it is a trick and must be so
await (window as any)[remote].init(__webpack_share_scopes__[shareScope]);
}
// mark remote as initialized
(window as any)[remote].__initialized = true;
}
// resolve promise so marking remote as loaded
resolve((window as any)[remote]);
};

if (existingRemote) {
// if existing remote but not loaded, hook into its onload and wait for it to be ready
existingRemote.onload = onload;
existingRemote.onerror = reject;
// check if remote fallback exists as param passed to function
// TODO: should scan public config for a matching key if no override exists
} else if (remoteFallbackUrl) {
// inject remote if a fallback exists and call the same onload function
const d = document;
const script = d.createElement('script');
script.type = 'text/javascript';
// mark as data-webpack so runtime can track it internally
script.setAttribute('data-webpack', `${remote}`);
script.async = true;
script.onerror = reject;
script.onload = onload;
script.src = remoteFallbackUrl;
d.getElementsByTagName('head')[0].appendChild(script);
} else {
// no remote and no fallback exist, reject
reject(new Error(`Cannot Find Remote ${remote} to inject`));
}
} else {
// remote already instantiated, resolve
resolve((window as any)[remote]);
}
});

function loadComponent(
remote: string,
sharedScope: string,
module: string,
url: string,
): () => Promise<{ default: typeof GenericBlock<any> }> {
return async (): Promise<{ default: typeof GenericBlock<any> }> => {
await getOrLoadRemote(remote, sharedScope, url);
const container = (window as any)[remote];
const factory = await container.get(module);
return factory();
};
}

export const ContextWrapper = ({ children, socket }: { socket: AdminConnection; children: any }): React.JSX.Element => {
const [blocks, setBlocks] = useState<(typeof GenericBlock<any>)[] | null>(null);
const [onUpdate, setOnUpdate] = useState(false);
Expand Down Expand Up @@ -216,7 +138,7 @@ export const ContextWrapper = ({ children, socket }: { socket: AdminConnection;
{
name: obj.common.javascriptRules!.name,
entry: url,
type: 'module'
type: (obj.common.javascriptRules! as any).type
}
],
// force: true // may be needed to sideload remotes after the fact.
Expand Down
3 changes: 3 additions & 0 deletions src-editor/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ import { TouchBackend } from 'react-dnd-touch-backend';

import { GenericBlock } from './Components/RulesEditor/components/GenericBlock';

(React as any).something = 'test';
console.log((React as any).something);

declare global {
interface Window {
GenericBlock: any;
Expand Down
1 change: 1 addition & 0 deletions src-editor/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export default defineConfig({
exposes: {},
remotes: {},
filename: 'remoteEntry.js',
manifest: true,
}),
react(),
vitetsConfigPaths(),
Expand Down

0 comments on commit f514f12

Please sign in to comment.