Skip to content

Commit

Permalink
style: fix lint issues
Browse files Browse the repository at this point in the history
  • Loading branch information
steven-pribilinskiy committed Dec 28, 2023
1 parent 6b0f812 commit b969be0
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 38 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@
"dist"
],
"bin": {
"make-federated-types": "dist/make-federated-types.js"
"make-federated-types": "dist/make-federated-types.js",
"download-federated-types": "dist/download-federated-types.js"
},
"scripts": {
"build": "tsc",
"lint": "eslint src/**/*.ts",
"lint": "eslint 'src/**/*.ts'",
"test": "jest"
},
"dependencies": {
Expand Down
2 changes: 1 addition & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export const DEFAULT_DIR_DIST = 'dist';
export const DEFAULT_DIR_EMITTED_TYPES = '@types';

export const DEFAULT_DIR_GLOBAL_TYPES = 'src/@types';
export const DEFAULT_DIR_DOWNLOADED_TYPES = `src/@types/remotes`;
export const DEFAULT_DIR_DOWNLOADED_TYPES = 'src/@types/remotes';

export const DEFAULT_DOWNLOAD_TYPES_INTERVAL_IN_SECONDS = 60;

Expand Down
11 changes: 5 additions & 6 deletions src/helpers/compileTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,11 @@ export function rewritePathsWithExposedFederatedModules(
const declaredModulePaths: string[] = [];

// Collect all instances of `declare module "..."`
let execResults: null | string[] = [];
while (true) {
execResults = regexDeclareModule.exec(typings);
if (execResults === null) {
break;
}
for (
let execResults: null | string[] = regexDeclareModule.exec(typings);
execResults !== null;
execResults = regexDeclareModule.exec(typings)
) {
declaredModulePaths.push(execResults[1]);
}

Expand Down
22 changes: 14 additions & 8 deletions src/make-federated-types.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
#!/usr/bin/env node

import parseArgs from 'minimist';
import path from 'path';

import { DEFAULT_DIR_DIST, DEFAULT_DIR_EMITTED_TYPES, DEFAULT_DIR_GLOBAL_TYPES, TS_CONFIG_FILE } from './constants';
import { compileTypes, rewritePathsWithExposedFederatedModules } from './helpers/compileTypes';
import { assertRunningFromRoot, getFederationConfig } from './helpers/cli';
import parseArgs from 'minimist';

import {
DEFAULT_DIR_DIST, DEFAULT_DIR_EMITTED_TYPES, DEFAULT_DIR_GLOBAL_TYPES, TS_CONFIG_FILE,
} from './constants';
import {
compileTypes, rewritePathsWithExposedFederatedModules,
} from './helpers/compileTypes';
import {
assertRunningFromRoot, getFederationConfig,
} from './helpers/cli';

assertRunningFromRoot();

Expand All @@ -14,15 +21,14 @@ type Argv = {
'federation-config': string,
'output-types-folder': string,
'tsconfig': string,

}

const argv = parseArgs<Argv>(process.argv.slice(2), {
alias: {
'global-types': 'g',
'output-types-folder': 'o',
'federation-config': 'c',
'tsconfig': 't',
tsconfig: 't',
} as Partial<Argv>,
});

Expand All @@ -32,13 +38,13 @@ const compileFiles = Object.values(federationConfig.exposes);
const outDir = argv['output-types-folder'] || path.join(DEFAULT_DIR_DIST, DEFAULT_DIR_EMITTED_TYPES);
const outFile = path.join(outDir, 'index.d.ts');
const dirGlobalTypes = argv['global-types'] || DEFAULT_DIR_GLOBAL_TYPES;
const tsconfigPath = argv['tsconfig'] || TS_CONFIG_FILE;
const tsconfigPath = argv.tsconfig || TS_CONFIG_FILE;

console.log(`Emitting types for ${compileFiles.length} exposed module(s)`);

const { isSuccess, typeDefinitions } = compileTypes(
tsconfigPath,
compileFiles as string[],
compileFiles,
outFile,
dirGlobalTypes,
);
Expand Down
48 changes: 30 additions & 18 deletions src/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import path from 'path';
import { Compiler, WebpackPluginInstance } from 'webpack';

import {
Compiler, WebpackPluginInstance,
} from 'webpack';

import {
CLOUDBEDS_DEPLOYMENT_ENV_WITH_DISABLED_REMOTE_TYPES_DOWNLOAD,
Expand All @@ -11,9 +14,13 @@ import {
TS_CONFIG_FILE,
} from './constants';
import { getRemoteManifestUrls } from './helpers/cloudbedsRemoteManifests';
import { compileTypes, rewritePathsWithExposedFederatedModules } from './helpers/compileTypes';
import {
compileTypes, rewritePathsWithExposedFederatedModules,
} from './helpers/compileTypes';
import { downloadTypes } from './helpers/downloadTypes';
import { getLoggerHint, setLogger } from './helpers/logger';
import {
getLoggerHint, setLogger,
} from './helpers/logger';
import { isEveryUrlValid } from './helpers/validation';
import {
FederationConfig,
Expand All @@ -22,16 +29,16 @@ import {
} from './types';

let isCompiledOnce = false;
let isDownloadedOnce = false;
const isDownloadedOnce = false;

const DEFAULT_MODULE_FEDERATION_PLUGIN_NAME = 'ModuleFederationPlugin';

export class ModuleFederationTypesPlugin implements WebpackPluginInstance {
constructor(public options?: ModuleFederationTypesPluginOptions) {}

async apply(compiler: Compiler): Promise<void> {
apply(compiler: Compiler): void {
const PLUGIN_NAME = this.constructor.name;
let logger = setLogger(compiler.getInfrastructureLogger(PLUGIN_NAME));
const logger = setLogger(compiler.getInfrastructureLogger(PLUGIN_NAME));

const remoteEntryUrls = this.options?.remoteEntryUrls;
const remoteManifestUrls = getRemoteManifestUrls(this.options);
Expand All @@ -58,13 +65,17 @@ export class ModuleFederationTypesPlugin implements WebpackPluginInstance {
}

// Allow for other module federation plugins such as this "NextFederationPlugin"
const moduleFederationPluginName = this.options?.moduleFederationPluginName ?? DEFAULT_MODULE_FEDERATION_PLUGIN_NAME;
const moduleFederationPluginName = this.options?.moduleFederationPluginName
?? DEFAULT_MODULE_FEDERATION_PLUGIN_NAME;

// Get ModuleFederationPlugin config
const federationOptions = compiler.options.plugins.find((plugin) => {
return plugin!.constructor.name === moduleFederationPluginName;
});
const federationOptions = compiler.options.plugins.find(
plugin => plugin!.constructor.name === moduleFederationPluginName,
);

// eslint-disable-next-line no-underscore-dangle
const federationPluginOptions: ModuleFederationPluginOptions = (federationOptions as any)?._options;

if (!federationPluginOptions?.name) {
logger.warn(
`Plugin disabled as ${moduleFederationPluginName} is not configured properly. The 'name' option is missing.`,
Expand Down Expand Up @@ -101,21 +112,19 @@ export class ModuleFederationTypesPlugin implements WebpackPluginInstance {
};

// Import types from remote modules
const downloadRemoteTypes = async () => {
return downloadTypes(
dirEmittedTypes,
dirDownloadedTypes,
const downloadRemoteTypes = async () => downloadTypes(
dirEmittedTypes,
dirDownloadedTypes,
remotes as Dict<string>,
remoteEntryUrls,
remoteManifestUrls,
);
};
);

// Determine whether compilation of types should be performed continuously
// followed by downloading of types when idle for a certain period of time
let recompileIntervalId: ReturnType<typeof setInterval>;
const shouldSyncContinuously = (compiler.options.mode === 'development')
&& (this.options?.downloadTypesWhenIdleIntervalInSeconds !== -1)
&& (this.options?.downloadTypesWhenIdleIntervalInSeconds !== -1);
const downloadTypesWhenIdleIntervalInSeconds = this.options?.downloadTypesWhenIdleIntervalInSeconds
|| DEFAULT_DOWNLOAD_TYPES_INTERVAL_IN_SECONDS;

Expand All @@ -127,7 +136,9 @@ export class ModuleFederationTypesPlugin implements WebpackPluginInstance {
() => {
logger.log(
new Date().toLocaleString(),
'Downloading types every', downloadTypesWhenIdleIntervalInSeconds, 'seconds',
'Downloading types every',
downloadTypesWhenIdleIntervalInSeconds,
'seconds',
);
downloadRemoteTypes();
},
Expand All @@ -143,6 +154,7 @@ export class ModuleFederationTypesPlugin implements WebpackPluginInstance {
logger.log('Downloading types on startup');
return downloadRemoteTypes();
});
// eslint-disable-next-line @typescript-eslint/no-misused-promises
compiler.hooks.watchRun.tap(PLUGIN_NAME, () => {
if (!isDownloadedOnce) {
logger.log('Downloading types on startup');
Expand Down
6 changes: 3 additions & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ export type ModuleFederationPluginOptions = ConstructorParameters<typeof contain

export type ModuleFederationTypesPluginOptions = {
dirEmittedTypes?: string,
dirGlobalTypes?: string;
dirDownloadedTypes?: string;
dirGlobalTypes?: string,
dirDownloadedTypes?: string,

disableDownladingRemoteTypes?: boolean,
disableTypeCompilation?: boolean,
downloadTypesWhenIdleIntervalInSeconds?: number,
moduleFederationPluginName?: string
moduleFederationPluginName?: string,
remoteEntryUrls?: RemoteEntryUrls,
remoteManifestUrls?: RemoteManifestUrls,
remoteManifestUrl?: string,
Expand Down

0 comments on commit b969be0

Please sign in to comment.