Skip to content

Commit

Permalink
fix: types
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Oct 23, 2024
1 parent 8b864db commit e11f2ea
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 24 deletions.
3 changes: 2 additions & 1 deletion lib/BannerPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ const createSchemaValidation = require("./util/create-schema-validation");
/** @typedef {import("./TemplatedPathPlugin").TemplatePath} TemplatePath */

const validate = createSchemaValidation(
require("../schemas/plugins/BannerPlugin.check.js"),
/** @type {(function(typeof import("../schemas/plugins/BannerPlugin.json")): boolean)} */
(require("../schemas/plugins/BannerPlugin.check.js")),
() => require("../schemas/plugins/BannerPlugin.json"),
{
name: "Banner Plugin",
Expand Down
26 changes: 16 additions & 10 deletions lib/container/HoistContainerReferencesPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ const { STAGE_ADVANCED } = require("../OptimizationStages");
const memoize = require("../util/memoize");
const { forEachRuntime } = require("../util/runtime");

/** @typedef {import("../Dependency")} Dependency */
/** @typedef {import("../Compiler")} Compiler */
/** @typedef {import("../Compilation")} Compilation */
/** @typedef {import("../Module")} Module */

const getModuleFederationPlugin = memoize(() =>
require("./ModuleFederationPlugin")
);
Expand All @@ -23,7 +28,7 @@ const PLUGIN_NAME = "HoistContainerReferences";
class HoistContainerReferences {
/**
* Apply the plugin to the compiler.
* @param {import("../Compiler")} compiler The webpack compiler instance.
* @param {Compiler} compiler The webpack compiler instance.
*/
apply(compiler) {
compiler.hooks.thisCompilation.tap(PLUGIN_NAME, compilation => {
Expand Down Expand Up @@ -64,9 +69,9 @@ class HoistContainerReferences {

/**
* Hoist modules in chunks.
* @param {import("../Compilation")} compilation The webpack compilation instance.
* @param {Set<import("../Dependency")>} depsToTrace Set of container entry dependencies.
* @param {Set<import("../Dependency")>} entryExternalsToHoist Set of container entry dependencies to hoist.
* @param {Compilation} compilation The webpack compilation instance.
* @param {Set<Dependency>} depsToTrace Set of container entry dependencies.
* @param {Set<Dependency>} entryExternalsToHoist Set of container entry dependencies to hoist.
*/
hoistModulesInChunks(compilation, depsToTrace, entryExternalsToHoist) {
const { chunkGraph, moduleGraph } = compilation;
Expand Down Expand Up @@ -157,8 +162,8 @@ class HoistContainerReferences {

/**
* Clean up chunks by disconnecting unused modules.
* @param {import("../Compilation")} compilation The webpack compilation instance.
* @param {Set<import("../Module")>} modules Set of modules to clean up.
* @param {Compilation} compilation The webpack compilation instance.
* @param {Set<Module>} modules Set of modules to clean up.
*/
cleanUpChunks(compilation, modules) {
const { chunkGraph } = compilation;
Expand All @@ -185,11 +190,11 @@ class HoistContainerReferences {

/**
* Helper method to collect all referenced modules recursively.
* @param {import("../Compilation")} compilation The webpack compilation instance.
* @param {import("../Module")} module The module to start collecting from.
* @param {Compilation} compilation The webpack compilation instance.
* @param {Module} module The module to start collecting from.
* @param {string} type The type of modules to collect ("initial", "external", or "all").
* @param {boolean} includeInitial Should include the referenced module passed
* @returns {Set<import("../Module")>} Set of collected modules.
* @returns {Set<Module>} Set of collected modules.
*/
function getAllReferencedModules(compilation, module, type, includeInitial) {
const collectedModules = new Set(includeInitial ? [module] : []);
Expand All @@ -214,7 +219,8 @@ function getAllReferencedModules(compilation, module, type, includeInitial) {
// Handle 'initial' type (skipping async blocks)
if (type === "initial") {
const parentBlock = compilation.moduleGraph.getParentBlock(
connection.dependency
/** @type {Dependency} */
(connection.dependency)
);
if (parentBlock instanceof AsyncDependenciesBlock) {
continue;
Expand Down
7 changes: 4 additions & 3 deletions lib/container/ModuleFederationPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ const HoistContainerReferences = require("./HoistContainerReferencesPlugin");
/** @typedef {import("../../declarations/plugins/container/ModuleFederationPlugin").ModuleFederationPluginOptions} ModuleFederationPluginOptions */
/** @typedef {import("../../declarations/plugins/container/ModuleFederationPlugin").Shared} Shared */
/** @typedef {import("../Compiler")} Compiler */
/** @typedef {import("../Dependency")} Dependency */

/**
* @typedef {object} CompilationHooks
* @property {SyncHook} addContainerEntryDependency
* @property {SyncHook} addFederationRuntimeDependency
* @property {SyncHook<Dependency>} addContainerEntryDependency
* @property {SyncHook<Dependency>} addFederationRuntimeDependency
*/

const validate = createSchemaValidation(
Expand Down Expand Up @@ -96,7 +97,7 @@ class ModuleFederationPlugin {
: Object.keys(options.exposes).length > 0)
) {
new ContainerPlugin({
name: options.name,
name: /** @type {string} */ (options.name),
library,
filename: options.filename,
runtime: options.runtime,
Expand Down
3 changes: 1 addition & 2 deletions lib/css/CssModulesPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -334,8 +334,7 @@ class CssModulesPlugin {
let inheritance;

if (
(parent.cssLayer !== null &&
parent.cssLayer !== undefined) ||
parent.cssLayer !== undefined ||
parent.supports ||
parent.media
) {
Expand Down
29 changes: 21 additions & 8 deletions lib/util/create-schema-validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,31 @@ const memoize = require("./memoize");

const getValidate = memoize(() => require("schema-utils").validate);

/** @typedef {import("schema-utils/declarations/validate").ValidationErrorConfiguration} ValidationErrorConfiguration */
/** @typedef {import("./fs").JsonObject} JsonObject */

/**
* @template {object | object[]} T
* @param {(function(T): boolean) | undefined} check check
* @param {() => JsonObject} getSchema get schema fn
* @param {ValidationErrorConfiguration} options options
* @returns {function(T): void} validate
*/
const createSchemaValidation = (check, getSchema, options) => {
getSchema = memoize(getSchema);
return value => {
if (check && !check(value)) {
getValidate()(getSchema(), value, options);
if (check) {
require("util").deprecate(
() => {},
"webpack bug: Pre-compiled schema reports error while real schema is happy. This has performance drawbacks.",
"DEP_WEBPACK_PRE_COMPILED_SCHEMA_INVALID"
)();
}
getValidate()(
getSchema(),
/** @type {object | object[]} */
(value),
options
);
require("util").deprecate(
() => {},
"webpack bug: Pre-compiled schema reports error while real schema is happy. This has performance drawbacks.",
"DEP_WEBPACK_PRE_COMPILED_SCHEMA_INVALID"
)();
}
};
};
Expand Down

0 comments on commit e11f2ea

Please sign in to comment.