Skip to content

Commit

Permalink
feat: recursive search for shared version
Browse files Browse the repository at this point in the history
  • Loading branch information
robdonn committed Sep 17, 2024
1 parent 13975e1 commit 7b2c6da
Showing 1 changed file with 59 additions and 26 deletions.
85 changes: 59 additions & 26 deletions packages/enhanced/src/lib/sharing/ConsumeSharedPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ const WebpackError = require(
const createSchemaValidation = require(
normalizeWebpackPath('webpack/lib/util/create-schema-validation'),
) as typeof import('webpack/lib/util/create-schema-validation');
const { join, dirname } = require(
normalizeWebpackPath('webpack/lib/util/fs'),
) as typeof import('webpack/lib/util/fs');

const validate = createSchemaValidation(
//eslint-disable-next-line
Expand Down Expand Up @@ -234,43 +237,73 @@ class ConsumeSharedPlugin {
packageName = match[0];
}

getDescriptionFile(
compilation.inputFileSystem,
context,
['package.json'],
(err, result) => {
if (err) {
requiredVersionWarning(
`Unable to read description file: ${err}`,
);
return resolve(undefined);
}
//@ts-ignore
const { data, path: descriptionPath } = result;
if (!data) {
const getDescriptionFileCallback: Parameters<
typeof getDescriptionFile
>[3] = (err, result) => {
if (err) {
requiredVersionWarning(
`Unable to read description file: ${err}`,
);
return resolve(undefined);
}
//@ts-ignore
const { data, path: descriptionPath } = result;
const currentDirectory = dirname(
compilation.inputFileSystem,
descriptionPath,
);
const parentDirectory = dirname(
compilation.inputFileSystem,
currentDirectory,
);
if (!data) {
if (currentDirectory === parentDirectory) {
requiredVersionWarning(
`Unable to find description file in ${context}.`,
);
return resolve(undefined);
}
//@ts-ignore
if (data.name === packageName) {
// Package self-referencing
return resolve(undefined);
}
const requiredVersion = getRequiredVersionFromDescriptionFile(
data,
packageName,

return getDescriptionFile(
compilation.inputFileSystem,
parentDirectory,
['package.json'],
getDescriptionFileCallback,
);
if (typeof requiredVersion !== 'string') {
}
//@ts-ignore
if (data.name === packageName) {
// Package self-referencing
return resolve(undefined);
}
const requiredVersion = getRequiredVersionFromDescriptionFile(
data,
packageName,
);
if (typeof requiredVersion !== 'string') {
if (currentDirectory === parentDirectory) {
requiredVersionWarning(
`Unable to find required version for "${packageName}" in description file (${descriptionPath}). It need to be in dependencies, devDependencies or peerDependencies.`,
);
return resolve(undefined);
}
// @ts-ignore webpack internal semver has some issue, use runtime semver , related issue: https://github.com/webpack/webpack/issues/17756
resolve(requiredVersion);
},

return getDescriptionFile(
compilation.inputFileSystem,
parentDirectory,
['package.json'],
getDescriptionFileCallback,
);
}
// @ts-ignore webpack internal semver has some issue, use runtime semver , related issue: https://github.com/webpack/webpack/issues/17756
resolve(requiredVersion);
};

getDescriptionFile(
compilation.inputFileSystem,
context,
['package.json'],
getDescriptionFileCallback,
);
}),
]).then(([importResolved, requiredVersion]) => {
Expand Down

0 comments on commit 7b2c6da

Please sign in to comment.