Skip to content

Commit f426c5d

Browse files
feat: added options to disable compilation of types and download of remote types
1 parent 464c64a commit f426c5d

File tree

3 files changed

+40
-38
lines changed

3 files changed

+40
-38
lines changed

README.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -182,11 +182,14 @@ to avoid failing workflows.
182182

183183
## Plugin Options
184184

185-
| Option | Value | Description |
186-
|----------------------------------:|:--------------------:|:-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
187-
| `cloudbedsRemoteManifestsBaseUrl` | `string` | Base URL for remote manifest files (a.k.a remote entry configs) that is specific to Cloudbeds microapps <br><br> _Examples:_ <br> `http://localhost:4480/remotes/dev` <br> `https://cb-front.cloudbeds-dev.com/remotes/[env]` <br> `use-devbox-name` |
188-
| `syncTypesIntervalInSeconds` | `number`, `0`, `-1` | Synchronize types continusouly with a specified value in seconds. <br><br> `0` - disables continuous synchronization. <br> `-1` - disables the plugin |
189-
| `remoteManifestUrls` | `RemoteManifestUrls` | URLs to remote manifest files. A manifest contains a URL to a remote entry that is substituted in runtime. Multiple remote entries is supported via `registry` field. <br><br> More details available in [this section](#templated-remote-urls) |
185+
| Option | Value | Default | Description |
186+
|-----------------------------------:|:--------------------:|:-------------------:|:-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
187+
| `disableTypeCompilation` | `boolean` | `false` | Disable compilation of types |
188+
| `disableDownladingRemoteTypes` | `boolean` | `false` | Disable downloading of remote types |
189+
| `cloudbedsRemoteManifestsBaseUrl` | `string` | `'/remotes/dev-ga'` | Base URL for remote manifest files (a.k.a remote entry configs) that is specific to Cloudbeds microapps <br><br> _Examples:_ <br> `http://localhost:4480/remotes/dev` <br> `https://cb-front.cloudbeds-dev.com/remotes/[env]` <br> `use-devbox-name` |
190+
| `doNotUseCloudbedsRemoteManifests` | `boolean` | `false` | Disable downloading default remote manifest files for Cloudbeds microapps (`mfd-common-remote-entry.json` and `mfd-remote-entries.json` files) and download only those provided via `remoteManifestUrls` |
191+
| `syncTypesIntervalInSeconds` | `number`, `-1` | `60` | Synchronize types continusouly - compile types after every compilation, download when idle with a specified delay value in seconds. <br><br> `-1` - disables continuous synchronization (compile and download will happen only on startup). |
192+
| `remoteManifestUrls` | `RemoteManifestUrls` | `{}` | URLs to remote manifest files. A manifest contains a URL to a remote entry that is substituted in runtime. Multiple remote entries is supported via `registry` field. <br><br> More details available in [this section](#templated-remote-urls) |
190193

191194
## Templated Remote URLs
192195

src/plugin.ts

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import {
1111
FederationConfig,
1212
ModuleFederationPluginOptions,
1313
ModuleFederationTypesPluginOptions,
14-
SyncTypesOption,
1514
} from './types';
1615

1716
export class ModuleFederationTypesPlugin implements WebpackPluginInstance {
@@ -29,8 +28,8 @@ export class ModuleFederationTypesPlugin implements WebpackPluginInstance {
2928
return;
3029
}
3130

32-
if (this.options?.syncTypesIntervalInSeconds === SyncTypesOption.DisablePlugin) {
33-
logger.log('Plugin disabled');
31+
if (this.options?.disableTypeCompilation && this.options.disableDownladingRemoteTypes) {
32+
logger.log('Plugin disabled as both type compilation and download features are turned off');
3433
return;
3534
}
3635
const distPath = compiler.options.devServer?.static?.directory || compiler.options.output?.path || DIR_DIST;
@@ -40,6 +39,7 @@ export class ModuleFederationTypesPlugin implements WebpackPluginInstance {
4039
});
4140
const federationPluginOptions: ModuleFederationPluginOptions = (federationOptions as any)?._options;
4241
if (!federationPluginOptions?.name) {
42+
logger.log('Plugin disabled as ModuleFederationPlugin is not configured properly. The `name` option is missing.');
4343
return;
4444
}
4545

@@ -48,8 +48,6 @@ export class ModuleFederationTypesPlugin implements WebpackPluginInstance {
4848

4949
// Create types for exposed modules
5050
const compileTypesHook = () => {
51-
if (!exposes) { return; }
52-
5351
const { isSuccess, typeDefinitions } = compileTypes(exposes as string[], outFile);
5452
if (isSuccess) {
5553
rewritePathsWithExposedFederatedModules(federationPluginOptions as FederationConfig, outFile, typeDefinitions);
@@ -60,42 +58,46 @@ export class ModuleFederationTypesPlugin implements WebpackPluginInstance {
6058

6159
// Import types from remote modules
6260
const downloadTypesHook = async () => {
63-
if (!remotes) { return; }
64-
6561
return downloadTypes(remotes as Record<string, string>, remoteManifestUrls);
6662
};
6763

6864
let recompileIntervalId: ReturnType<typeof setInterval>;
6965
const shouldSyncContinuously = (compiler.options.mode === 'development')
70-
&& (this.options?.syncTypesIntervalInSeconds !== SyncTypesOption.StartupOnly)
66+
&& (this.options?.syncTypesIntervalInSeconds !== -1)
7167
const syncTypesIntervalInSeconds = this.options?.syncTypesIntervalInSeconds
7268
|| DEFAULT_SYNC_TYPES_INTERVAL_IN_SECONDS;
7369

7470
const compileTypesContinuouslyHook = () => {
7571
// Reset and create an Interval to recompile and redownload types every 60 seconds after compilation
76-
clearInterval(recompileIntervalId);
77-
recompileIntervalId = setInterval(
78-
() => {
79-
logger.log(new Date().toLocaleString(), 'Downloading types every', syncTypesIntervalInSeconds, 'seconds');
80-
downloadTypesHook();
81-
},
82-
1000 * syncTypesIntervalInSeconds,
83-
);
72+
if (remotes && !this.options?.disableDownladingRemoteTypes) {
73+
clearInterval(recompileIntervalId);
74+
recompileIntervalId = setInterval(
75+
() => {
76+
logger.log(new Date().toLocaleString(), 'Downloading types every', syncTypesIntervalInSeconds, 'seconds');
77+
downloadTypesHook();
78+
},
79+
1000 * syncTypesIntervalInSeconds,
80+
);
81+
}
8482

8583
compileTypesHook();
8684
};
8785

88-
logger.log('Downloading types on startup');
89-
await downloadTypesHook();
86+
if (remotes && !this.options?.disableDownladingRemoteTypes) {
87+
logger.log('Downloading types on startup');
88+
await downloadTypesHook();
89+
}
9090

91-
if (shouldSyncContinuously) {
92-
compiler.hooks.afterEmit.tap(PLUGIN_NAME, () => {
93-
logger.log('Compiling types on afterEmit event');
94-
compileTypesContinuouslyHook();
95-
});
96-
} else {
97-
logger.log('Compile types on startup only');
98-
compileTypesHook();
91+
if (exposes && !this.options?.disableTypeCompilation) {
92+
if (shouldSyncContinuously) {
93+
compiler.hooks.afterEmit.tap(PLUGIN_NAME, () => {
94+
logger.log('Compiling types on afterEmit event');
95+
compileTypesContinuouslyHook();
96+
});
97+
} else {
98+
logger.log('Compile types on startup only');
99+
compileTypesHook();
100+
}
99101
}
100102
}
101103
}

src/types.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,10 @@ export type RemoteManifestUrls = Record<'registry' | string, string>;
2626
export type ModuleFederationPluginOptions = ConstructorParameters<typeof container.ModuleFederationPlugin>[0];
2727

2828
export type ModuleFederationTypesPluginOptions = {
29-
cloudbedsRemoteManifestsBaseUrl?: string | 'use-devbox-name';
29+
cloudbedsRemoteManifestsBaseUrl?: string | 'use-devbox-name',
30+
disableDownladingRemoteTypes?: boolean,
31+
disableTypeCompilation?: boolean,
3032
doNotUseCloudbedsRemoteManifests?: boolean,
3133
remoteManifestUrls?: RemoteManifestUrls,
32-
syncTypesIntervalInSeconds?: number;
33-
}
34-
35-
export enum SyncTypesOption {
36-
StartupOnly = 0,
37-
DisablePlugin = -1,
34+
syncTypesIntervalInSeconds?: number,
3835
}

0 commit comments

Comments
 (0)