Skip to content

Commit

Permalink
feat: added options to disable compilation of types and download of r…
Browse files Browse the repository at this point in the history
…emote types
  • Loading branch information
steven-pribilinskiy committed Jul 22, 2022
1 parent 464c64a commit f426c5d
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 38 deletions.
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,11 +182,14 @@ to avoid failing workflows.

## Plugin Options

| Option | Value | Description |
|----------------------------------:|:--------------------:|:-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `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` |
| `syncTypesIntervalInSeconds` | `number`, `0`, `-1` | Synchronize types continusouly with a specified value in seconds. <br><br> `0` - disables continuous synchronization. <br> `-1` - disables the plugin |
| `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) |
| Option | Value | Default | Description |
|-----------------------------------:|:--------------------:|:-------------------:|:-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `disableTypeCompilation` | `boolean` | `false` | Disable compilation of types |
| `disableDownladingRemoteTypes` | `boolean` | `false` | Disable downloading of remote types |
| `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` |
| `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` |
| `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). |
| `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) |

## Templated Remote URLs

Expand Down
54 changes: 28 additions & 26 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
FederationConfig,
ModuleFederationPluginOptions,
ModuleFederationTypesPluginOptions,
SyncTypesOption,
} from './types';

export class ModuleFederationTypesPlugin implements WebpackPluginInstance {
Expand All @@ -29,8 +28,8 @@ export class ModuleFederationTypesPlugin implements WebpackPluginInstance {
return;
}

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

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

// Create types for exposed modules
const compileTypesHook = () => {
if (!exposes) { return; }

const { isSuccess, typeDefinitions } = compileTypes(exposes as string[], outFile);
if (isSuccess) {
rewritePathsWithExposedFederatedModules(federationPluginOptions as FederationConfig, outFile, typeDefinitions);
Expand All @@ -60,42 +58,46 @@ export class ModuleFederationTypesPlugin implements WebpackPluginInstance {

// Import types from remote modules
const downloadTypesHook = async () => {
if (!remotes) { return; }

return downloadTypes(remotes as Record<string, string>, remoteManifestUrls);
};

let recompileIntervalId: ReturnType<typeof setInterval>;
const shouldSyncContinuously = (compiler.options.mode === 'development')
&& (this.options?.syncTypesIntervalInSeconds !== SyncTypesOption.StartupOnly)
&& (this.options?.syncTypesIntervalInSeconds !== -1)
const syncTypesIntervalInSeconds = this.options?.syncTypesIntervalInSeconds
|| DEFAULT_SYNC_TYPES_INTERVAL_IN_SECONDS;

const compileTypesContinuouslyHook = () => {
// Reset and create an Interval to recompile and redownload types every 60 seconds after compilation
clearInterval(recompileIntervalId);
recompileIntervalId = setInterval(
() => {
logger.log(new Date().toLocaleString(), 'Downloading types every', syncTypesIntervalInSeconds, 'seconds');
downloadTypesHook();
},
1000 * syncTypesIntervalInSeconds,
);
if (remotes && !this.options?.disableDownladingRemoteTypes) {
clearInterval(recompileIntervalId);
recompileIntervalId = setInterval(
() => {
logger.log(new Date().toLocaleString(), 'Downloading types every', syncTypesIntervalInSeconds, 'seconds');
downloadTypesHook();
},
1000 * syncTypesIntervalInSeconds,
);
}

compileTypesHook();
};

logger.log('Downloading types on startup');
await downloadTypesHook();
if (remotes && !this.options?.disableDownladingRemoteTypes) {
logger.log('Downloading types on startup');
await downloadTypesHook();
}

if (shouldSyncContinuously) {
compiler.hooks.afterEmit.tap(PLUGIN_NAME, () => {
logger.log('Compiling types on afterEmit event');
compileTypesContinuouslyHook();
});
} else {
logger.log('Compile types on startup only');
compileTypesHook();
if (exposes && !this.options?.disableTypeCompilation) {
if (shouldSyncContinuously) {
compiler.hooks.afterEmit.tap(PLUGIN_NAME, () => {
logger.log('Compiling types on afterEmit event');
compileTypesContinuouslyHook();
});
} else {
logger.log('Compile types on startup only');
compileTypesHook();
}
}
}
}
11 changes: 4 additions & 7 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,10 @@ export type RemoteManifestUrls = Record<'registry' | string, string>;
export type ModuleFederationPluginOptions = ConstructorParameters<typeof container.ModuleFederationPlugin>[0];

export type ModuleFederationTypesPluginOptions = {
cloudbedsRemoteManifestsBaseUrl?: string | 'use-devbox-name';
cloudbedsRemoteManifestsBaseUrl?: string | 'use-devbox-name',
disableDownladingRemoteTypes?: boolean,
disableTypeCompilation?: boolean,
doNotUseCloudbedsRemoteManifests?: boolean,
remoteManifestUrls?: RemoteManifestUrls,
syncTypesIntervalInSeconds?: number;
}

export enum SyncTypesOption {
StartupOnly = 0,
DisablePlugin = -1,
syncTypesIntervalInSeconds?: number,
}

0 comments on commit f426c5d

Please sign in to comment.