From 355429ad2cdf381af500dadddfa65b34051a89ff Mon Sep 17 00:00:00 2001 From: Steven Prybylynskyi Date: Wed, 31 Jan 2024 14:27:59 +0100 Subject: [PATCH] fix(bin): fallback for webpack config path --- src/bin/download-federated-types.ts | 4 ++-- src/bin/helpers/getWebpackConfigPathFromArgs.ts | 16 ++++++++++++++++ src/bin/helpers/index.ts | 1 + src/bin/make-federated-types.ts | 15 +++++++++------ 4 files changed, 28 insertions(+), 8 deletions(-) create mode 100644 src/bin/helpers/getWebpackConfigPathFromArgs.ts diff --git a/src/bin/download-federated-types.ts b/src/bin/download-federated-types.ts index f5e2ae6..2c8310a 100644 --- a/src/bin/download-federated-types.ts +++ b/src/bin/download-federated-types.ts @@ -13,7 +13,7 @@ import { } from '../helpers'; import { - assertRunningFromRoot, getOptionsFromWebpackConfig, + assertRunningFromRoot, getOptionsFromWebpackConfig, getWebpackConfigPathFromArgs, } from './helpers'; assertRunningFromRoot(); @@ -23,7 +23,7 @@ type Argv = { }; const argv = parseArgs(process.argv.slice(2)); -const webpackConfigPath = argv['webpack-config'] || 'webpack.config.js'; +const webpackConfigPath = getWebpackConfigPathFromArgs(argv['webpack-config']); const { mfPluginOptions, mfTypesPluginOptions } = getOptionsFromWebpackConfig(webpackConfigPath); diff --git a/src/bin/helpers/getWebpackConfigPathFromArgs.ts b/src/bin/helpers/getWebpackConfigPathFromArgs.ts new file mode 100644 index 0000000..43a00ba --- /dev/null +++ b/src/bin/helpers/getWebpackConfigPathFromArgs.ts @@ -0,0 +1,16 @@ +import { existsSync } from 'fs'; + +export function getWebpackConfigPathFromArgs(webpackConfigPath?: string): string { + if (!webpackConfigPath) { + if (existsSync('webpack.config.ts')) { + webpackConfigPath = 'webpack.config.ts'; + } else if (existsSync('webpack.config.js')) { + webpackConfigPath = 'webpack.config.js'; + } else { + console.error(`Could not find webpack.config.ts file at ${process.cwd()}`); + process.exit(1); + } + } + + return webpackConfigPath; +} diff --git a/src/bin/helpers/index.ts b/src/bin/helpers/index.ts index 80ec9d2..de3c805 100644 --- a/src/bin/helpers/index.ts +++ b/src/bin/helpers/index.ts @@ -1,3 +1,4 @@ export * from './assertRunningFromRoot'; export * from './getFederationConfig'; export * from './getOptionsFromWebpackConfig'; +export * from './getWebpackConfigPathFromArgs'; diff --git a/src/bin/make-federated-types.ts b/src/bin/make-federated-types.ts index a994103..093ed69 100644 --- a/src/bin/make-federated-types.ts +++ b/src/bin/make-federated-types.ts @@ -14,7 +14,7 @@ import { setLogger } from '../helpers'; import { FederationConfig } from '../models'; import { - assertRunningFromRoot, getFederationConfig, getOptionsFromWebpackConfig, + assertRunningFromRoot, getFederationConfig, getOptionsFromWebpackConfig, getWebpackConfigPathFromArgs, } from './helpers'; assertRunningFromRoot(); @@ -36,12 +36,15 @@ const argv = parseArgs(process.argv.slice(2), { } as Partial, }); -const webpackConfigPath = argv['webpack-config'] || 'webpack.config.js'; -const federationConfig = webpackConfigPath - ? getOptionsFromWebpackConfig(webpackConfigPath).mfPluginOptions as unknown as FederationConfig - : getFederationConfig(argv['federation-config']); -const compileFiles = Object.values(federationConfig.exposes); +let federationConfig: FederationConfig; +if (argv['federation-config']) { + federationConfig = getFederationConfig(argv['federation-config']); +} else { + const webpackConfigPath = getWebpackConfigPathFromArgs(argv['webpack-config']); + federationConfig = getOptionsFromWebpackConfig(webpackConfigPath).mfPluginOptions as FederationConfig; +} +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;