Skip to content

Commit

Permalink
Revert "CONSOLE-3905: Throw warnings when guessModuleFilePath is ne…
Browse files Browse the repository at this point in the history
…eded"

This reverts commit b066e5a.
  • Loading branch information
logonoff committed Dec 5, 2024
1 parent b2b0282 commit 0591f54
Showing 1 changed file with 19 additions and 35 deletions.
54 changes: 19 additions & 35 deletions frontend/packages/console-plugin-sdk/src/codegen/active-plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,47 +17,23 @@ import { consolePkgScope, PluginPackage } from './plugin-resolver';

const getExtensionsFilePath = (pkg: PluginPackage) => path.resolve(pkg._path, extensionsFile);

export type ActivePluginsModuleData = {
/** Generated module source code. */
code: string;
/** Diagnostics collected while generating module source code. */
diagnostics: { errors: string[]; warnings: string[] };
/** Absolute file paths representing webpack file dependencies of the generated module. */
fileDependencies: string[];
};

/**
* Guess the file path of the module (e.g., the extension,
* any barrel/index file) based on the given base path.
*
* Returns the base path if no file is found.
*/
const guessModuleFilePath = (
basePath: string,
diagnostics: ActivePluginsModuleData['diagnostics'],
) => {
const guessModuleFilePath = (basePath: string) => {
const extensions = ['.tsx', '.ts', '.jsx', '.js'];

// Sometimes the module is an index file, but the exposed module path only specifies the directory.
// In that case, we need an explicit check for the index file.
const indexModulePaths = ['index.ts', 'index.js'].map((i) => path.resolve(basePath, i));

for (const p of indexModulePaths) {
if (fs.existsSync(p)) {
diagnostics.warnings.push(
`The module ${basePath} refers to an index file ${p}. Index/barrel files are not recommended as they may cause unnecessary code to be loaded. Consider specifying the module file directly.`,
);
return p;
}
}

const pathsToCheck = [...extensions.map((ext) => `${basePath}${ext}`)];
const pathsToCheck = [...indexModulePaths, ...extensions.map((ext) => `${basePath}${ext}`)];

for (const p of pathsToCheck) {
if (fs.existsSync(p)) {
diagnostics.warnings.push(
`The module ${basePath} refers to a file ${p}, but a file extension was not specified.`,
);
return p;
}
}
Expand All @@ -67,16 +43,24 @@ const guessModuleFilePath = (
return basePath;
};

const getExposedModuleFilePath = (
pkg: PluginPackage,
moduleName: string,
diagnostics: ActivePluginsModuleData['diagnostics'],
) => {
const getExposedModuleFilePath = (pkg: PluginPackage, moduleName: string) => {
const modulePath = path.resolve(pkg._path, pkg.consolePlugin.exposedModules[moduleName]);

return path.extname(modulePath)
? modulePath // Path already contains a file extension (no extra guessing needed)
: guessModuleFilePath(modulePath, diagnostics);
// Check if there is a file extension (no extra guessing needed)
if (!path.extname(modulePath)) {
return guessModuleFilePath(modulePath);
}

return modulePath;
};

export type ActivePluginsModuleData = {
/** Generated module source code. */
code: string;
/** Diagnostics collected while generating module source code. */
diagnostics: { errors: string[]; warnings: string[] };
/** Absolute file paths representing webpack file dependencies of the generated module. */
fileDependencies: string[];
};

/**
Expand Down Expand Up @@ -243,7 +227,7 @@ export const getActivePluginsModuleData = (
fileDependencies.push(getExtensionsFilePath(pkg));

Object.keys(pkg.consolePlugin.exposedModules || {}).forEach((moduleName) => {
const moduleFilePath = getExposedModuleFilePath(pkg, moduleName, { errors, warnings });
const moduleFilePath = getExposedModuleFilePath(pkg, moduleName);

if (fs.existsSync(moduleFilePath) && fs.statSync(moduleFilePath).isFile()) {
fileDependencies.push(moduleFilePath);
Expand Down

0 comments on commit 0591f54

Please sign in to comment.