refactor(types): more

This commit is contained in:
alexander.akait 2023-06-16 22:44:20 +03:00
parent a911bd9fa1
commit e226101c55
14 changed files with 116 additions and 52 deletions

View File

@ -63,7 +63,7 @@ const makeSerializable = require("./util/makeSerializable");
* @property {RawChunkGroupOptions=} groupOptions
* @property {string=} typePrefix
* @property {string=} category
* @property {string[][]=} referencedExports exports referenced from modules (won't be mangled)
* @property {(string[][] | null)=} referencedExports exports referenced from modules (won't be mangled)
* @property {string=} layer
*/

View File

@ -83,7 +83,7 @@ class Entrypoint extends ChunkGroup {
* @returns {Chunk} chunk
*/
getEntrypointChunk() {
return this._entrypointChunk;
return /** @type {Chunk} */ (this._entrypointChunk);
}
/**

View File

@ -180,6 +180,10 @@ AMDDefineDependency.Template = class AMDDefineDependencyTemplate extends (
this.replace(dep, source, definition, content);
}
/**
* @param {AMDDefineDependency} dependency dependency
* @returns {string} variable name
*/
localModuleVar(dependency) {
return (
dependency.localModule &&
@ -188,6 +192,10 @@ AMDDefineDependency.Template = class AMDDefineDependencyTemplate extends (
);
}
/**
* @param {AMDDefineDependency} dependency dependency
* @returns {string} branch
*/
branch(dependency) {
const localModuleVar = this.localModuleVar(dependency) ? "l" : "";
const arrayRange = dependency.arrayRange ? "a" : "";
@ -196,6 +204,12 @@ AMDDefineDependency.Template = class AMDDefineDependencyTemplate extends (
return localModuleVar + arrayRange + objectRange + functionRange;
}
/**
* @param {AMDDefineDependency} dependency dependency
* @param {ReplaceSource} source source
* @param {string} definition definition
* @param {string} text text
*/
replace(dependency, source, definition, text) {
const localModuleVar = this.localModuleVar(dependency);
if (localModuleVar) {
@ -216,18 +230,34 @@ AMDDefineDependency.Template = class AMDDefineDependencyTemplate extends (
let current = dependency.range[0];
if (dependency.arrayRange) {
source.replace(current, dependency.arrayRange[0] - 1, texts.shift());
source.replace(
current,
dependency.arrayRange[0] - 1,
/** @type {string} */ (texts.shift())
);
current = dependency.arrayRange[1];
}
if (dependency.objectRange) {
source.replace(current, dependency.objectRange[0] - 1, texts.shift());
source.replace(
current,
dependency.objectRange[0] - 1,
/** @type {string} */ (texts.shift())
);
current = dependency.objectRange[1];
} else if (dependency.functionRange) {
source.replace(current, dependency.functionRange[0] - 1, texts.shift());
source.replace(
current,
dependency.functionRange[0] - 1,
/** @type {string} */ (texts.shift())
);
current = dependency.functionRange[1];
}
source.replace(current, dependency.range[1] - 1, texts.shift());
source.replace(
current,
dependency.range[1] - 1,
/** @type {string} */ (texts.shift())
);
if (texts.length > 0) throw new Error("Implementation error");
}
};

View File

@ -16,8 +16,13 @@ const DynamicExports = require("./DynamicExports");
const LocalModuleDependency = require("./LocalModuleDependency");
const { addLocalModule, getLocalModule } = require("./LocalModulesHelpers");
/** @typedef {import("estree").CallExpression} CallExpression */
/** @typedef {import("../javascript/JavascriptParser")} JavascriptParser */
/**
* @param {CallExpression} expr expression
* @returns {boolean} true if it's a bound function expression
*/
const isBoundFunctionExpression = expr => {
if (expr.type !== "CallExpression") return false;
if (expr.callee.type !== "MemberExpression") return false;

View File

@ -37,6 +37,8 @@ const CommonJsExportRequireDependency = require("./CommonJsExportRequireDependen
/** @typedef {import("../../declarations/WebpackOptions").JavascriptParserOptions} JavascriptParserOptions */
/** @typedef {import("../Compilation")} Compilation */
/** @typedef {import("../Compiler")} Compiler */
/** @typedef {import("../Dependency").DependencyLocation} DependencyLocation */
/** @typedef {import("../Module").BuildInfo} BuildInfo */
/** @typedef {import("../javascript/JavascriptParser")} Parser */
const PLUGIN_NAME = "CommonJsPlugin";
@ -200,12 +202,13 @@ class CommonJsPlugin {
parser.hooks.expression
.for(RuntimeGlobals.moduleLoaded)
.tap(PLUGIN_NAME, expr => {
parser.state.module.buildInfo.moduleConcatenationBailout =
/** @type {BuildInfo} */
(parser.state.module.buildInfo).moduleConcatenationBailout =
RuntimeGlobals.moduleLoaded;
const dep = new RuntimeRequirementsDependency([
RuntimeGlobals.moduleLoaded
]);
dep.loc = expr.loc;
dep.loc = /** @type {DependencyLocation} */ (expr.loc);
parser.state.module.addPresentationalDependency(dep);
return true;
});
@ -213,12 +216,13 @@ class CommonJsPlugin {
parser.hooks.expression
.for(RuntimeGlobals.moduleId)
.tap(PLUGIN_NAME, expr => {
parser.state.module.buildInfo.moduleConcatenationBailout =
/** @type {BuildInfo} */
(parser.state.module.buildInfo).moduleConcatenationBailout =
RuntimeGlobals.moduleId;
const dep = new RuntimeRequirementsDependency([
RuntimeGlobals.moduleId
]);
dep.loc = expr.loc;
dep.loc = /** @type {DependencyLocation} */ (expr.loc);
parser.state.module.addPresentationalDependency(dep);
return true;
});

View File

@ -18,6 +18,8 @@ const {
} = require("./HarmonyImportDependencyParserPlugin");
const HarmonyImportSideEffectDependency = require("./HarmonyImportSideEffectDependency");
/** @typedef {import("../javascript/JavascriptParser")} JavascriptParser */
const { HarmonyStarExportsList } = HarmonyExportImportedSpecifierDependency;
module.exports = class HarmonyExportDependencyParserPlugin {

View File

@ -330,9 +330,9 @@ class HarmonyExportImportedSpecifierDependency extends HarmonyImportDependency {
* @param {string[]} ids the requested export name of the imported module
* @param {string | null} name the export name of for this module
* @param {Set<string>} activeExports other named exports in the module
* @param {ReadonlyArray<HarmonyExportImportedSpecifierDependency> | Iterable<HarmonyExportImportedSpecifierDependency>} otherStarExports other star exports in the module before this import
* @param {ReadonlyArray<HarmonyExportImportedSpecifierDependency> | Iterable<HarmonyExportImportedSpecifierDependency> | null} otherStarExports other star exports in the module before this import
* @param {number} exportPresenceMode mode of checking export names
* @param {HarmonyStarExportsList} allStarExports all star exports in the module
* @param {HarmonyStarExportsList | null} allStarExports all star exports in the module
* @param {Assertions=} assertions import assertions
*/
constructor(

View File

@ -11,7 +11,6 @@ const HarmonyImportDependency = require("./HarmonyImportDependency");
/** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
/** @typedef {import("../Dependency")} Dependency */
/** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
/** @typedef {import("../InitFragment")} InitFragment */
/** @typedef {import("../Module")} Module */
/** @typedef {import("../ModuleGraph")} ModuleGraph */
/** @typedef {import("../ModuleGraphConnection")} ModuleGraphConnection */
@ -74,7 +73,7 @@ HarmonyImportSideEffectDependency.Template = class HarmonyImportSideEffectDepend
apply(dependency, source, templateContext) {
const { moduleGraph, concatenationScope } = templateContext;
if (concatenationScope) {
const module = moduleGraph.getModule(dependency);
const module = /** @type {Module} */ (moduleGraph.getModule(dependency));
if (concatenationScope.isModuleInScope(module)) {
return;
}

View File

@ -19,6 +19,8 @@ const HarmonyImportDependency = require("./HarmonyImportDependency");
/** @typedef {import("../Dependency").ReferencedExport} ReferencedExport */
/** @typedef {import("../Dependency").UpdateHashContext} UpdateHashContext */
/** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
/** @typedef {import("../Module")} Module */
/** @typedef {import("../Module").BuildMeta} BuildMeta */
/** @typedef {import("../ModuleGraph")} ModuleGraph */
/** @typedef {import("../ModuleGraphConnection")} ModuleGraphConnection */
/** @typedef {import("../ModuleGraphConnection").ConnectionState} ConnectionState */
@ -66,9 +68,9 @@ class HarmonyImportSpecifierDependency extends HarmonyImportDependency {
this.directImport = undefined;
this.shorthand = undefined;
this.asiSafe = undefined;
/** @type {Set<string> | boolean} */
/** @type {Set<string> | boolean | undefined} */
this.usedByExports = undefined;
/** @type {Set<string>} */
/** @type {Set<string> | undefined} */
this.referencedPropertiesInDestructuring = undefined;
}
@ -143,11 +145,14 @@ class HarmonyImportSpecifierDependency extends HarmonyImportDependency {
let namespaceObjectAsContext = this.namespaceObjectAsContext;
if (ids[0] === "default") {
const selfModule = moduleGraph.getParentModule(this);
const importedModule = moduleGraph.getModule(this);
const importedModule =
/** @type {Module} */
(moduleGraph.getModule(this));
switch (
importedModule.getExportsType(
moduleGraph,
selfModule.buildMeta.strictHarmonyModule
/** @type {BuildMeta} */
(selfModule.buildMeta).strictHarmonyModule
)
) {
case "default-only":
@ -201,7 +206,10 @@ class HarmonyImportSpecifierDependency extends HarmonyImportDependency {
_getEffectiveExportPresenceLevel(moduleGraph) {
if (this.exportPresenceMode !== ExportPresenceModes.AUTO)
return this.exportPresenceMode;
return moduleGraph.getParentModule(this).buildMeta.strictHarmonyModule
const buildMeta = /** @type {BuildMeta} */ (
moduleGraph.getParentModule(this).buildMeta
);
return buildMeta.strictHarmonyModule
? ExportPresenceModes.ERROR
: ExportPresenceModes.WARN;
}
@ -362,9 +370,10 @@ HarmonyImportSpecifierDependency.Template = class HarmonyImportSpecifierDependen
* @returns {string[]} generated code
*/
_trimIdsToThoseImported(ids, moduleGraph, dependency) {
/** @type {string[]} */
let trimmedIds = [];
const exportsInfo = moduleGraph.getExportsInfo(
moduleGraph.getModule(dependency)
/** @type {Module} */ (moduleGraph.getModule(dependency))
);
let currentExportsInfo = /** @type {ExportsInfo=} */ exportsInfo;
for (let i = 0; i < ids.length; i++) {
@ -437,7 +446,7 @@ HarmonyImportSpecifierDependency.Template = class HarmonyImportSpecifierDependen
exportExpr = runtimeTemplate.exportFromImport({
moduleGraph,
module: moduleGraph.getModule(dep),
module: /** @type {Module} */ (moduleGraph.getModule(dep)),
request: dep.request,
exportName: ids,
originModule: module,

View File

@ -13,6 +13,8 @@ const ModuleDependency = require("./ModuleDependency");
/** @typedef {import("../AsyncDependenciesBlock")} AsyncDependenciesBlock */
/** @typedef {import("../Dependency").ReferencedExport} ReferencedExport */
/** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
/** @typedef {import("../Module")} Module */
/** @typedef {import("../Module").BuildMeta} BuildMeta */
/** @typedef {import("../ModuleGraph")} ModuleGraph */
/** @typedef {import("../javascript/JavascriptParser").Range} Range */
/** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
@ -23,7 +25,7 @@ class ImportDependency extends ModuleDependency {
/**
* @param {string} request the request
* @param {Range} range expression range
* @param {string[][]=} referencedExports list of referenced exports
* @param {(string[][] | null)=} referencedExports list of referenced exports
*/
constructor(request, range, referencedExports) {
super(request);
@ -96,9 +98,9 @@ ImportDependency.Template = class ImportDependencyTemplate extends (
const content = runtimeTemplate.moduleNamespacePromise({
chunkGraph,
block: block,
module: moduleGraph.getModule(dep),
module: /** @type {Module} */ (moduleGraph.getModule(dep)),
request: dep.request,
strict: module.buildMeta.strictHarmonyModule,
strict: /** @type {BuildMeta} */ (module.buildMeta).strictHarmonyModule,
message: "import()",
runtimeRequirements
});

View File

@ -21,7 +21,7 @@ class ImportEagerDependency extends ImportDependency {
/**
* @param {string} request the request
* @param {Range} range expression range
* @param {string[][]=} referencedExports list of referenced exports
* @param {(string[][] | null)=} referencedExports list of referenced exports
*/
constructor(request, range, referencedExports) {
super(request, range, referencedExports);

View File

@ -17,7 +17,10 @@ const ImportWeakDependency = require("./ImportWeakDependency");
/** @typedef {import("../../declarations/WebpackOptions").JavascriptParserOptions} JavascriptParserOptions */
/** @typedef {import("../ChunkGroup").RawChunkGroupOptions} RawChunkGroupOptions */
/** @typedef {import("../ContextModule").ContextMode} ContextMode */
/** @typedef {import("../Dependency").DependencyLocation} DependencyLocation */
/** @typedef {import("../Module").BuildMeta} BuildMeta */
/** @typedef {import("../javascript/JavascriptParser")} JavascriptParser */
/** @typedef {import("../javascript/JavascriptParser").Range} Range */
class ImportParserPlugin {
/**
@ -32,14 +35,18 @@ class ImportParserPlugin {
* @returns {void}
*/
apply(parser) {
/**
* @template T
* @param {Iterable<T>} enumerable enumerable
* @returns {T[][]} array of array
*/
const exportsFromEnumerable = enumerable =>
Array.from(enumerable, e => [e]);
parser.hooks.importCall.tap("ImportParserPlugin", expr => {
const param = parser.evaluateExpression(expr.source);
let chunkName = null;
/** @type {ContextMode} */
let mode = this.options.dynamicImportMode;
let mode = /** @type {ContextMode} */ (this.options.dynamicImportMode);
let include = null;
let exclude = null;
/** @type {string[][] | null} */
@ -68,7 +75,7 @@ class ImportParserPlugin {
groupOptions.fetchPriority = dynamicImportFetchPriority;
const { options: importOptions, errors: commentErrors } =
parser.parseCommentOptions(expr.range);
parser.parseCommentOptions(/** @type {Range} */ (expr.range));
if (commentErrors) {
for (const e of commentErrors) {
@ -88,7 +95,7 @@ class ImportParserPlugin {
parser.state.module.addWarning(
new UnsupportedFeatureWarning(
`\`webpackIgnore\` expected a boolean, but received: ${importOptions.webpackIgnore}.`,
expr.loc
/** @type {DependencyLocation} */ (expr.loc)
)
);
} else {
@ -103,7 +110,7 @@ class ImportParserPlugin {
parser.state.module.addWarning(
new UnsupportedFeatureWarning(
`\`webpackChunkName\` expected a string, but received: ${importOptions.webpackChunkName}.`,
expr.loc
/** @type {DependencyLocation} */ (expr.loc)
)
);
} else {
@ -115,7 +122,7 @@ class ImportParserPlugin {
parser.state.module.addWarning(
new UnsupportedFeatureWarning(
`\`webpackMode\` expected a string, but received: ${importOptions.webpackMode}.`,
expr.loc
/** @type {DependencyLocation} */ (expr.loc)
)
);
} else {
@ -131,7 +138,7 @@ class ImportParserPlugin {
parser.state.module.addWarning(
new UnsupportedFeatureWarning(
`\`webpackPrefetch\` expected true or a number, but received: ${importOptions.webpackPrefetch}.`,
expr.loc
/** @type {DependencyLocation} */ (expr.loc)
)
);
}
@ -145,7 +152,7 @@ class ImportParserPlugin {
parser.state.module.addWarning(
new UnsupportedFeatureWarning(
`\`webpackPreload\` expected true or a number, but received: ${importOptions.webpackPreload}.`,
expr.loc
/** @type {DependencyLocation} */ (expr.loc)
)
);
}
@ -160,7 +167,7 @@ class ImportParserPlugin {
parser.state.module.addWarning(
new UnsupportedFeatureWarning(
`\`webpackFetchPriority\` expected true or "low", "high" or "auto", but received: ${importOptions.webpackFetchPriority}.`,
expr.loc
/** @type {DependencyLocation} */ (expr.loc)
)
);
}
@ -173,7 +180,7 @@ class ImportParserPlugin {
parser.state.module.addWarning(
new UnsupportedFeatureWarning(
`\`webpackInclude\` expected a regular expression, but received: ${importOptions.webpackInclude}.`,
expr.loc
/** @type {DependencyLocation} */ (expr.loc)
)
);
} else {
@ -188,7 +195,7 @@ class ImportParserPlugin {
parser.state.module.addWarning(
new UnsupportedFeatureWarning(
`\`webpackExclude\` expected a regular expression, but received: ${importOptions.webpackExclude}.`,
expr.loc
/** @type {DependencyLocation} */ (expr.loc)
)
);
} else {
@ -200,7 +207,7 @@ class ImportParserPlugin {
!(
typeof importOptions.webpackExports === "string" ||
(Array.isArray(importOptions.webpackExports) &&
importOptions.webpackExports.every(
/** @type {string[]} */ (importOptions.webpackExports).every(
item => typeof item === "string"
))
)
@ -208,7 +215,7 @@ class ImportParserPlugin {
parser.state.module.addWarning(
new UnsupportedFeatureWarning(
`\`webpackExports\` expected a string or an array of strings, but received: ${importOptions.webpackExports}.`,
expr.loc
/** @type {DependencyLocation} */ (expr.loc)
)
);
} else {
@ -230,7 +237,7 @@ class ImportParserPlugin {
parser.state.module.addWarning(
new UnsupportedFeatureWarning(
`\`webpackMode\` expected 'lazy', 'lazy-once', 'eager' or 'weak', but received: ${mode}.`,
expr.loc
/** @type {DependencyLocation} */ (expr.loc)
)
);
mode = "lazy";
@ -243,7 +250,7 @@ class ImportParserPlugin {
parser.state.module.addWarning(
new UnsupportedFeatureWarning(
`\`webpackExports\` could not be used with destructuring assignment.`,
expr.loc
/** @type {DependencyLocation} */ (expr.loc)
)
);
}
@ -253,15 +260,15 @@ class ImportParserPlugin {
if (param.isString()) {
if (mode === "eager") {
const dep = new ImportEagerDependency(
param.string,
expr.range,
/** @type {string} */ (param.string),
/** @type {Range} */ (expr.range),
exports
);
parser.state.current.addDependency(dep);
} else if (mode === "weak") {
const dep = new ImportWeakDependency(
param.string,
expr.range,
/** @type {string} */ (param.string),
/** @type {Range} */ (expr.range),
exports
);
parser.state.current.addDependency(dep);
@ -271,11 +278,15 @@ class ImportParserPlugin {
...groupOptions,
name: chunkName
},
expr.loc,
/** @type {DependencyLocation} */ (expr.loc),
param.string
);
const dep = new ImportDependency(param.string, expr.range, exports);
dep.loc = expr.loc;
const dep = new ImportDependency(
/** @type {string} */ (param.string),
/** @type {Range} */ (expr.range),
exports
);
dep.loc = /** @type {DependencyLocation} */ (expr.loc);
depBlock.addDependency(dep);
parser.state.current.addBlock(depBlock);
}
@ -286,7 +297,7 @@ class ImportParserPlugin {
}
const dep = ContextDependencyHelpers.create(
ImportContextDependency,
expr.range,
/** @type {Range} */ (expr.range),
param,
expr,
this.options,
@ -296,7 +307,9 @@ class ImportParserPlugin {
include,
exclude,
mode,
namespaceObject: parser.state.module.buildMeta.strictHarmonyModule
namespaceObject: /** @type {BuildMeta} */ (
parser.state.module.buildMeta
).strictHarmonyModule
? "strict"
: true,
typePrefix: "import()",
@ -306,7 +319,7 @@ class ImportParserPlugin {
parser
);
if (!dep) return;
dep.loc = expr.loc;
dep.loc = /** @type {DependencyLocation} */ (expr.loc);
dep.optional = !!parser.scope.inTry;
parser.state.current.addDependency(dep);
return true;

View File

@ -21,7 +21,7 @@ class ImportWeakDependency extends ImportDependency {
/**
* @param {string} request the request
* @param {Range} range expression range
* @param {string[][]=} referencedExports list of referenced exports
* @param {(string[][] | null)=} referencedExports list of referenced exports
*/
constructor(request, range, referencedExports) {
super(request, range, referencedExports);

2
types.d.ts vendored
View File

@ -2840,7 +2840,7 @@ declare interface ContextModuleOptions {
/**
* exports referenced from modules (won't be mangled)
*/
referencedExports?: string[][];
referencedExports?: null | string[][];
layer?: string;
resource: string | false | string[];
resourceQuery?: string;