mirror of https://github.com/webpack/webpack.git
refactor(types): more
This commit is contained in:
parent
a911bd9fa1
commit
e226101c55
|
@ -63,7 +63,7 @@ const makeSerializable = require("./util/makeSerializable");
|
||||||
* @property {RawChunkGroupOptions=} groupOptions
|
* @property {RawChunkGroupOptions=} groupOptions
|
||||||
* @property {string=} typePrefix
|
* @property {string=} typePrefix
|
||||||
* @property {string=} category
|
* @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
|
* @property {string=} layer
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
|
@ -83,7 +83,7 @@ class Entrypoint extends ChunkGroup {
|
||||||
* @returns {Chunk} chunk
|
* @returns {Chunk} chunk
|
||||||
*/
|
*/
|
||||||
getEntrypointChunk() {
|
getEntrypointChunk() {
|
||||||
return this._entrypointChunk;
|
return /** @type {Chunk} */ (this._entrypointChunk);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -180,6 +180,10 @@ AMDDefineDependency.Template = class AMDDefineDependencyTemplate extends (
|
||||||
this.replace(dep, source, definition, content);
|
this.replace(dep, source, definition, content);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {AMDDefineDependency} dependency dependency
|
||||||
|
* @returns {string} variable name
|
||||||
|
*/
|
||||||
localModuleVar(dependency) {
|
localModuleVar(dependency) {
|
||||||
return (
|
return (
|
||||||
dependency.localModule &&
|
dependency.localModule &&
|
||||||
|
@ -188,6 +192,10 @@ AMDDefineDependency.Template = class AMDDefineDependencyTemplate extends (
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {AMDDefineDependency} dependency dependency
|
||||||
|
* @returns {string} branch
|
||||||
|
*/
|
||||||
branch(dependency) {
|
branch(dependency) {
|
||||||
const localModuleVar = this.localModuleVar(dependency) ? "l" : "";
|
const localModuleVar = this.localModuleVar(dependency) ? "l" : "";
|
||||||
const arrayRange = dependency.arrayRange ? "a" : "";
|
const arrayRange = dependency.arrayRange ? "a" : "";
|
||||||
|
@ -196,6 +204,12 @@ AMDDefineDependency.Template = class AMDDefineDependencyTemplate extends (
|
||||||
return localModuleVar + arrayRange + objectRange + functionRange;
|
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) {
|
replace(dependency, source, definition, text) {
|
||||||
const localModuleVar = this.localModuleVar(dependency);
|
const localModuleVar = this.localModuleVar(dependency);
|
||||||
if (localModuleVar) {
|
if (localModuleVar) {
|
||||||
|
@ -216,18 +230,34 @@ AMDDefineDependency.Template = class AMDDefineDependencyTemplate extends (
|
||||||
|
|
||||||
let current = dependency.range[0];
|
let current = dependency.range[0];
|
||||||
if (dependency.arrayRange) {
|
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];
|
current = dependency.arrayRange[1];
|
||||||
}
|
}
|
||||||
|
|
||||||
if (dependency.objectRange) {
|
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];
|
current = dependency.objectRange[1];
|
||||||
} else if (dependency.functionRange) {
|
} 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];
|
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");
|
if (texts.length > 0) throw new Error("Implementation error");
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -16,8 +16,13 @@ const DynamicExports = require("./DynamicExports");
|
||||||
const LocalModuleDependency = require("./LocalModuleDependency");
|
const LocalModuleDependency = require("./LocalModuleDependency");
|
||||||
const { addLocalModule, getLocalModule } = require("./LocalModulesHelpers");
|
const { addLocalModule, getLocalModule } = require("./LocalModulesHelpers");
|
||||||
|
|
||||||
|
/** @typedef {import("estree").CallExpression} CallExpression */
|
||||||
/** @typedef {import("../javascript/JavascriptParser")} JavascriptParser */
|
/** @typedef {import("../javascript/JavascriptParser")} JavascriptParser */
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {CallExpression} expr expression
|
||||||
|
* @returns {boolean} true if it's a bound function expression
|
||||||
|
*/
|
||||||
const isBoundFunctionExpression = expr => {
|
const isBoundFunctionExpression = expr => {
|
||||||
if (expr.type !== "CallExpression") return false;
|
if (expr.type !== "CallExpression") return false;
|
||||||
if (expr.callee.type !== "MemberExpression") return false;
|
if (expr.callee.type !== "MemberExpression") return false;
|
||||||
|
|
|
@ -37,6 +37,8 @@ const CommonJsExportRequireDependency = require("./CommonJsExportRequireDependen
|
||||||
/** @typedef {import("../../declarations/WebpackOptions").JavascriptParserOptions} JavascriptParserOptions */
|
/** @typedef {import("../../declarations/WebpackOptions").JavascriptParserOptions} JavascriptParserOptions */
|
||||||
/** @typedef {import("../Compilation")} Compilation */
|
/** @typedef {import("../Compilation")} Compilation */
|
||||||
/** @typedef {import("../Compiler")} Compiler */
|
/** @typedef {import("../Compiler")} Compiler */
|
||||||
|
/** @typedef {import("../Dependency").DependencyLocation} DependencyLocation */
|
||||||
|
/** @typedef {import("../Module").BuildInfo} BuildInfo */
|
||||||
/** @typedef {import("../javascript/JavascriptParser")} Parser */
|
/** @typedef {import("../javascript/JavascriptParser")} Parser */
|
||||||
|
|
||||||
const PLUGIN_NAME = "CommonJsPlugin";
|
const PLUGIN_NAME = "CommonJsPlugin";
|
||||||
|
@ -200,12 +202,13 @@ class CommonJsPlugin {
|
||||||
parser.hooks.expression
|
parser.hooks.expression
|
||||||
.for(RuntimeGlobals.moduleLoaded)
|
.for(RuntimeGlobals.moduleLoaded)
|
||||||
.tap(PLUGIN_NAME, expr => {
|
.tap(PLUGIN_NAME, expr => {
|
||||||
parser.state.module.buildInfo.moduleConcatenationBailout =
|
/** @type {BuildInfo} */
|
||||||
|
(parser.state.module.buildInfo).moduleConcatenationBailout =
|
||||||
RuntimeGlobals.moduleLoaded;
|
RuntimeGlobals.moduleLoaded;
|
||||||
const dep = new RuntimeRequirementsDependency([
|
const dep = new RuntimeRequirementsDependency([
|
||||||
RuntimeGlobals.moduleLoaded
|
RuntimeGlobals.moduleLoaded
|
||||||
]);
|
]);
|
||||||
dep.loc = expr.loc;
|
dep.loc = /** @type {DependencyLocation} */ (expr.loc);
|
||||||
parser.state.module.addPresentationalDependency(dep);
|
parser.state.module.addPresentationalDependency(dep);
|
||||||
return true;
|
return true;
|
||||||
});
|
});
|
||||||
|
@ -213,12 +216,13 @@ class CommonJsPlugin {
|
||||||
parser.hooks.expression
|
parser.hooks.expression
|
||||||
.for(RuntimeGlobals.moduleId)
|
.for(RuntimeGlobals.moduleId)
|
||||||
.tap(PLUGIN_NAME, expr => {
|
.tap(PLUGIN_NAME, expr => {
|
||||||
parser.state.module.buildInfo.moduleConcatenationBailout =
|
/** @type {BuildInfo} */
|
||||||
|
(parser.state.module.buildInfo).moduleConcatenationBailout =
|
||||||
RuntimeGlobals.moduleId;
|
RuntimeGlobals.moduleId;
|
||||||
const dep = new RuntimeRequirementsDependency([
|
const dep = new RuntimeRequirementsDependency([
|
||||||
RuntimeGlobals.moduleId
|
RuntimeGlobals.moduleId
|
||||||
]);
|
]);
|
||||||
dep.loc = expr.loc;
|
dep.loc = /** @type {DependencyLocation} */ (expr.loc);
|
||||||
parser.state.module.addPresentationalDependency(dep);
|
parser.state.module.addPresentationalDependency(dep);
|
||||||
return true;
|
return true;
|
||||||
});
|
});
|
||||||
|
|
|
@ -18,6 +18,8 @@ const {
|
||||||
} = require("./HarmonyImportDependencyParserPlugin");
|
} = require("./HarmonyImportDependencyParserPlugin");
|
||||||
const HarmonyImportSideEffectDependency = require("./HarmonyImportSideEffectDependency");
|
const HarmonyImportSideEffectDependency = require("./HarmonyImportSideEffectDependency");
|
||||||
|
|
||||||
|
/** @typedef {import("../javascript/JavascriptParser")} JavascriptParser */
|
||||||
|
|
||||||
const { HarmonyStarExportsList } = HarmonyExportImportedSpecifierDependency;
|
const { HarmonyStarExportsList } = HarmonyExportImportedSpecifierDependency;
|
||||||
|
|
||||||
module.exports = class HarmonyExportDependencyParserPlugin {
|
module.exports = class HarmonyExportDependencyParserPlugin {
|
||||||
|
|
|
@ -330,9 +330,9 @@ class HarmonyExportImportedSpecifierDependency extends HarmonyImportDependency {
|
||||||
* @param {string[]} ids the requested export name of the imported module
|
* @param {string[]} ids the requested export name of the imported module
|
||||||
* @param {string | null} name the export name of for this module
|
* @param {string | null} name the export name of for this module
|
||||||
* @param {Set<string>} activeExports other named exports in the 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 {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
|
* @param {Assertions=} assertions import assertions
|
||||||
*/
|
*/
|
||||||
constructor(
|
constructor(
|
||||||
|
|
|
@ -11,7 +11,6 @@ const HarmonyImportDependency = require("./HarmonyImportDependency");
|
||||||
/** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
|
/** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
|
||||||
/** @typedef {import("../Dependency")} Dependency */
|
/** @typedef {import("../Dependency")} Dependency */
|
||||||
/** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
|
/** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
|
||||||
/** @typedef {import("../InitFragment")} InitFragment */
|
|
||||||
/** @typedef {import("../Module")} Module */
|
/** @typedef {import("../Module")} Module */
|
||||||
/** @typedef {import("../ModuleGraph")} ModuleGraph */
|
/** @typedef {import("../ModuleGraph")} ModuleGraph */
|
||||||
/** @typedef {import("../ModuleGraphConnection")} ModuleGraphConnection */
|
/** @typedef {import("../ModuleGraphConnection")} ModuleGraphConnection */
|
||||||
|
@ -74,7 +73,7 @@ HarmonyImportSideEffectDependency.Template = class HarmonyImportSideEffectDepend
|
||||||
apply(dependency, source, templateContext) {
|
apply(dependency, source, templateContext) {
|
||||||
const { moduleGraph, concatenationScope } = templateContext;
|
const { moduleGraph, concatenationScope } = templateContext;
|
||||||
if (concatenationScope) {
|
if (concatenationScope) {
|
||||||
const module = moduleGraph.getModule(dependency);
|
const module = /** @type {Module} */ (moduleGraph.getModule(dependency));
|
||||||
if (concatenationScope.isModuleInScope(module)) {
|
if (concatenationScope.isModuleInScope(module)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,6 +19,8 @@ const HarmonyImportDependency = require("./HarmonyImportDependency");
|
||||||
/** @typedef {import("../Dependency").ReferencedExport} ReferencedExport */
|
/** @typedef {import("../Dependency").ReferencedExport} ReferencedExport */
|
||||||
/** @typedef {import("../Dependency").UpdateHashContext} UpdateHashContext */
|
/** @typedef {import("../Dependency").UpdateHashContext} UpdateHashContext */
|
||||||
/** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
|
/** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
|
||||||
|
/** @typedef {import("../Module")} Module */
|
||||||
|
/** @typedef {import("../Module").BuildMeta} BuildMeta */
|
||||||
/** @typedef {import("../ModuleGraph")} ModuleGraph */
|
/** @typedef {import("../ModuleGraph")} ModuleGraph */
|
||||||
/** @typedef {import("../ModuleGraphConnection")} ModuleGraphConnection */
|
/** @typedef {import("../ModuleGraphConnection")} ModuleGraphConnection */
|
||||||
/** @typedef {import("../ModuleGraphConnection").ConnectionState} ConnectionState */
|
/** @typedef {import("../ModuleGraphConnection").ConnectionState} ConnectionState */
|
||||||
|
@ -66,9 +68,9 @@ class HarmonyImportSpecifierDependency extends HarmonyImportDependency {
|
||||||
this.directImport = undefined;
|
this.directImport = undefined;
|
||||||
this.shorthand = undefined;
|
this.shorthand = undefined;
|
||||||
this.asiSafe = undefined;
|
this.asiSafe = undefined;
|
||||||
/** @type {Set<string> | boolean} */
|
/** @type {Set<string> | boolean | undefined} */
|
||||||
this.usedByExports = undefined;
|
this.usedByExports = undefined;
|
||||||
/** @type {Set<string>} */
|
/** @type {Set<string> | undefined} */
|
||||||
this.referencedPropertiesInDestructuring = undefined;
|
this.referencedPropertiesInDestructuring = undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -143,11 +145,14 @@ class HarmonyImportSpecifierDependency extends HarmonyImportDependency {
|
||||||
let namespaceObjectAsContext = this.namespaceObjectAsContext;
|
let namespaceObjectAsContext = this.namespaceObjectAsContext;
|
||||||
if (ids[0] === "default") {
|
if (ids[0] === "default") {
|
||||||
const selfModule = moduleGraph.getParentModule(this);
|
const selfModule = moduleGraph.getParentModule(this);
|
||||||
const importedModule = moduleGraph.getModule(this);
|
const importedModule =
|
||||||
|
/** @type {Module} */
|
||||||
|
(moduleGraph.getModule(this));
|
||||||
switch (
|
switch (
|
||||||
importedModule.getExportsType(
|
importedModule.getExportsType(
|
||||||
moduleGraph,
|
moduleGraph,
|
||||||
selfModule.buildMeta.strictHarmonyModule
|
/** @type {BuildMeta} */
|
||||||
|
(selfModule.buildMeta).strictHarmonyModule
|
||||||
)
|
)
|
||||||
) {
|
) {
|
||||||
case "default-only":
|
case "default-only":
|
||||||
|
@ -201,7 +206,10 @@ class HarmonyImportSpecifierDependency extends HarmonyImportDependency {
|
||||||
_getEffectiveExportPresenceLevel(moduleGraph) {
|
_getEffectiveExportPresenceLevel(moduleGraph) {
|
||||||
if (this.exportPresenceMode !== ExportPresenceModes.AUTO)
|
if (this.exportPresenceMode !== ExportPresenceModes.AUTO)
|
||||||
return this.exportPresenceMode;
|
return this.exportPresenceMode;
|
||||||
return moduleGraph.getParentModule(this).buildMeta.strictHarmonyModule
|
const buildMeta = /** @type {BuildMeta} */ (
|
||||||
|
moduleGraph.getParentModule(this).buildMeta
|
||||||
|
);
|
||||||
|
return buildMeta.strictHarmonyModule
|
||||||
? ExportPresenceModes.ERROR
|
? ExportPresenceModes.ERROR
|
||||||
: ExportPresenceModes.WARN;
|
: ExportPresenceModes.WARN;
|
||||||
}
|
}
|
||||||
|
@ -362,9 +370,10 @@ HarmonyImportSpecifierDependency.Template = class HarmonyImportSpecifierDependen
|
||||||
* @returns {string[]} generated code
|
* @returns {string[]} generated code
|
||||||
*/
|
*/
|
||||||
_trimIdsToThoseImported(ids, moduleGraph, dependency) {
|
_trimIdsToThoseImported(ids, moduleGraph, dependency) {
|
||||||
|
/** @type {string[]} */
|
||||||
let trimmedIds = [];
|
let trimmedIds = [];
|
||||||
const exportsInfo = moduleGraph.getExportsInfo(
|
const exportsInfo = moduleGraph.getExportsInfo(
|
||||||
moduleGraph.getModule(dependency)
|
/** @type {Module} */ (moduleGraph.getModule(dependency))
|
||||||
);
|
);
|
||||||
let currentExportsInfo = /** @type {ExportsInfo=} */ exportsInfo;
|
let currentExportsInfo = /** @type {ExportsInfo=} */ exportsInfo;
|
||||||
for (let i = 0; i < ids.length; i++) {
|
for (let i = 0; i < ids.length; i++) {
|
||||||
|
@ -437,7 +446,7 @@ HarmonyImportSpecifierDependency.Template = class HarmonyImportSpecifierDependen
|
||||||
|
|
||||||
exportExpr = runtimeTemplate.exportFromImport({
|
exportExpr = runtimeTemplate.exportFromImport({
|
||||||
moduleGraph,
|
moduleGraph,
|
||||||
module: moduleGraph.getModule(dep),
|
module: /** @type {Module} */ (moduleGraph.getModule(dep)),
|
||||||
request: dep.request,
|
request: dep.request,
|
||||||
exportName: ids,
|
exportName: ids,
|
||||||
originModule: module,
|
originModule: module,
|
||||||
|
|
|
@ -13,6 +13,8 @@ const ModuleDependency = require("./ModuleDependency");
|
||||||
/** @typedef {import("../AsyncDependenciesBlock")} AsyncDependenciesBlock */
|
/** @typedef {import("../AsyncDependenciesBlock")} AsyncDependenciesBlock */
|
||||||
/** @typedef {import("../Dependency").ReferencedExport} ReferencedExport */
|
/** @typedef {import("../Dependency").ReferencedExport} ReferencedExport */
|
||||||
/** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
|
/** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
|
||||||
|
/** @typedef {import("../Module")} Module */
|
||||||
|
/** @typedef {import("../Module").BuildMeta} BuildMeta */
|
||||||
/** @typedef {import("../ModuleGraph")} ModuleGraph */
|
/** @typedef {import("../ModuleGraph")} ModuleGraph */
|
||||||
/** @typedef {import("../javascript/JavascriptParser").Range} Range */
|
/** @typedef {import("../javascript/JavascriptParser").Range} Range */
|
||||||
/** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
|
/** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
|
||||||
|
@ -23,7 +25,7 @@ class ImportDependency extends ModuleDependency {
|
||||||
/**
|
/**
|
||||||
* @param {string} request the request
|
* @param {string} request the request
|
||||||
* @param {Range} range expression range
|
* @param {Range} range expression range
|
||||||
* @param {string[][]=} referencedExports list of referenced exports
|
* @param {(string[][] | null)=} referencedExports list of referenced exports
|
||||||
*/
|
*/
|
||||||
constructor(request, range, referencedExports) {
|
constructor(request, range, referencedExports) {
|
||||||
super(request);
|
super(request);
|
||||||
|
@ -96,9 +98,9 @@ ImportDependency.Template = class ImportDependencyTemplate extends (
|
||||||
const content = runtimeTemplate.moduleNamespacePromise({
|
const content = runtimeTemplate.moduleNamespacePromise({
|
||||||
chunkGraph,
|
chunkGraph,
|
||||||
block: block,
|
block: block,
|
||||||
module: moduleGraph.getModule(dep),
|
module: /** @type {Module} */ (moduleGraph.getModule(dep)),
|
||||||
request: dep.request,
|
request: dep.request,
|
||||||
strict: module.buildMeta.strictHarmonyModule,
|
strict: /** @type {BuildMeta} */ (module.buildMeta).strictHarmonyModule,
|
||||||
message: "import()",
|
message: "import()",
|
||||||
runtimeRequirements
|
runtimeRequirements
|
||||||
});
|
});
|
||||||
|
|
|
@ -21,7 +21,7 @@ class ImportEagerDependency extends ImportDependency {
|
||||||
/**
|
/**
|
||||||
* @param {string} request the request
|
* @param {string} request the request
|
||||||
* @param {Range} range expression range
|
* @param {Range} range expression range
|
||||||
* @param {string[][]=} referencedExports list of referenced exports
|
* @param {(string[][] | null)=} referencedExports list of referenced exports
|
||||||
*/
|
*/
|
||||||
constructor(request, range, referencedExports) {
|
constructor(request, range, referencedExports) {
|
||||||
super(request, range, referencedExports);
|
super(request, range, referencedExports);
|
||||||
|
|
|
@ -17,7 +17,10 @@ const ImportWeakDependency = require("./ImportWeakDependency");
|
||||||
/** @typedef {import("../../declarations/WebpackOptions").JavascriptParserOptions} JavascriptParserOptions */
|
/** @typedef {import("../../declarations/WebpackOptions").JavascriptParserOptions} JavascriptParserOptions */
|
||||||
/** @typedef {import("../ChunkGroup").RawChunkGroupOptions} RawChunkGroupOptions */
|
/** @typedef {import("../ChunkGroup").RawChunkGroupOptions} RawChunkGroupOptions */
|
||||||
/** @typedef {import("../ContextModule").ContextMode} ContextMode */
|
/** @typedef {import("../ContextModule").ContextMode} ContextMode */
|
||||||
|
/** @typedef {import("../Dependency").DependencyLocation} DependencyLocation */
|
||||||
|
/** @typedef {import("../Module").BuildMeta} BuildMeta */
|
||||||
/** @typedef {import("../javascript/JavascriptParser")} JavascriptParser */
|
/** @typedef {import("../javascript/JavascriptParser")} JavascriptParser */
|
||||||
|
/** @typedef {import("../javascript/JavascriptParser").Range} Range */
|
||||||
|
|
||||||
class ImportParserPlugin {
|
class ImportParserPlugin {
|
||||||
/**
|
/**
|
||||||
|
@ -32,14 +35,18 @@ class ImportParserPlugin {
|
||||||
* @returns {void}
|
* @returns {void}
|
||||||
*/
|
*/
|
||||||
apply(parser) {
|
apply(parser) {
|
||||||
|
/**
|
||||||
|
* @template T
|
||||||
|
* @param {Iterable<T>} enumerable enumerable
|
||||||
|
* @returns {T[][]} array of array
|
||||||
|
*/
|
||||||
const exportsFromEnumerable = enumerable =>
|
const exportsFromEnumerable = enumerable =>
|
||||||
Array.from(enumerable, e => [e]);
|
Array.from(enumerable, e => [e]);
|
||||||
parser.hooks.importCall.tap("ImportParserPlugin", expr => {
|
parser.hooks.importCall.tap("ImportParserPlugin", expr => {
|
||||||
const param = parser.evaluateExpression(expr.source);
|
const param = parser.evaluateExpression(expr.source);
|
||||||
|
|
||||||
let chunkName = null;
|
let chunkName = null;
|
||||||
/** @type {ContextMode} */
|
let mode = /** @type {ContextMode} */ (this.options.dynamicImportMode);
|
||||||
let mode = this.options.dynamicImportMode;
|
|
||||||
let include = null;
|
let include = null;
|
||||||
let exclude = null;
|
let exclude = null;
|
||||||
/** @type {string[][] | null} */
|
/** @type {string[][] | null} */
|
||||||
|
@ -68,7 +75,7 @@ class ImportParserPlugin {
|
||||||
groupOptions.fetchPriority = dynamicImportFetchPriority;
|
groupOptions.fetchPriority = dynamicImportFetchPriority;
|
||||||
|
|
||||||
const { options: importOptions, errors: commentErrors } =
|
const { options: importOptions, errors: commentErrors } =
|
||||||
parser.parseCommentOptions(expr.range);
|
parser.parseCommentOptions(/** @type {Range} */ (expr.range));
|
||||||
|
|
||||||
if (commentErrors) {
|
if (commentErrors) {
|
||||||
for (const e of commentErrors) {
|
for (const e of commentErrors) {
|
||||||
|
@ -88,7 +95,7 @@ class ImportParserPlugin {
|
||||||
parser.state.module.addWarning(
|
parser.state.module.addWarning(
|
||||||
new UnsupportedFeatureWarning(
|
new UnsupportedFeatureWarning(
|
||||||
`\`webpackIgnore\` expected a boolean, but received: ${importOptions.webpackIgnore}.`,
|
`\`webpackIgnore\` expected a boolean, but received: ${importOptions.webpackIgnore}.`,
|
||||||
expr.loc
|
/** @type {DependencyLocation} */ (expr.loc)
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
|
@ -103,7 +110,7 @@ class ImportParserPlugin {
|
||||||
parser.state.module.addWarning(
|
parser.state.module.addWarning(
|
||||||
new UnsupportedFeatureWarning(
|
new UnsupportedFeatureWarning(
|
||||||
`\`webpackChunkName\` expected a string, but received: ${importOptions.webpackChunkName}.`,
|
`\`webpackChunkName\` expected a string, but received: ${importOptions.webpackChunkName}.`,
|
||||||
expr.loc
|
/** @type {DependencyLocation} */ (expr.loc)
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
|
@ -115,7 +122,7 @@ class ImportParserPlugin {
|
||||||
parser.state.module.addWarning(
|
parser.state.module.addWarning(
|
||||||
new UnsupportedFeatureWarning(
|
new UnsupportedFeatureWarning(
|
||||||
`\`webpackMode\` expected a string, but received: ${importOptions.webpackMode}.`,
|
`\`webpackMode\` expected a string, but received: ${importOptions.webpackMode}.`,
|
||||||
expr.loc
|
/** @type {DependencyLocation} */ (expr.loc)
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
|
@ -131,7 +138,7 @@ class ImportParserPlugin {
|
||||||
parser.state.module.addWarning(
|
parser.state.module.addWarning(
|
||||||
new UnsupportedFeatureWarning(
|
new UnsupportedFeatureWarning(
|
||||||
`\`webpackPrefetch\` expected true or a number, but received: ${importOptions.webpackPrefetch}.`,
|
`\`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(
|
parser.state.module.addWarning(
|
||||||
new UnsupportedFeatureWarning(
|
new UnsupportedFeatureWarning(
|
||||||
`\`webpackPreload\` expected true or a number, but received: ${importOptions.webpackPreload}.`,
|
`\`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(
|
parser.state.module.addWarning(
|
||||||
new UnsupportedFeatureWarning(
|
new UnsupportedFeatureWarning(
|
||||||
`\`webpackFetchPriority\` expected true or "low", "high" or "auto", but received: ${importOptions.webpackFetchPriority}.`,
|
`\`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(
|
parser.state.module.addWarning(
|
||||||
new UnsupportedFeatureWarning(
|
new UnsupportedFeatureWarning(
|
||||||
`\`webpackInclude\` expected a regular expression, but received: ${importOptions.webpackInclude}.`,
|
`\`webpackInclude\` expected a regular expression, but received: ${importOptions.webpackInclude}.`,
|
||||||
expr.loc
|
/** @type {DependencyLocation} */ (expr.loc)
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
|
@ -188,7 +195,7 @@ class ImportParserPlugin {
|
||||||
parser.state.module.addWarning(
|
parser.state.module.addWarning(
|
||||||
new UnsupportedFeatureWarning(
|
new UnsupportedFeatureWarning(
|
||||||
`\`webpackExclude\` expected a regular expression, but received: ${importOptions.webpackExclude}.`,
|
`\`webpackExclude\` expected a regular expression, but received: ${importOptions.webpackExclude}.`,
|
||||||
expr.loc
|
/** @type {DependencyLocation} */ (expr.loc)
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
|
@ -200,7 +207,7 @@ class ImportParserPlugin {
|
||||||
!(
|
!(
|
||||||
typeof importOptions.webpackExports === "string" ||
|
typeof importOptions.webpackExports === "string" ||
|
||||||
(Array.isArray(importOptions.webpackExports) &&
|
(Array.isArray(importOptions.webpackExports) &&
|
||||||
importOptions.webpackExports.every(
|
/** @type {string[]} */ (importOptions.webpackExports).every(
|
||||||
item => typeof item === "string"
|
item => typeof item === "string"
|
||||||
))
|
))
|
||||||
)
|
)
|
||||||
|
@ -208,7 +215,7 @@ class ImportParserPlugin {
|
||||||
parser.state.module.addWarning(
|
parser.state.module.addWarning(
|
||||||
new UnsupportedFeatureWarning(
|
new UnsupportedFeatureWarning(
|
||||||
`\`webpackExports\` expected a string or an array of strings, but received: ${importOptions.webpackExports}.`,
|
`\`webpackExports\` expected a string or an array of strings, but received: ${importOptions.webpackExports}.`,
|
||||||
expr.loc
|
/** @type {DependencyLocation} */ (expr.loc)
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
|
@ -230,7 +237,7 @@ class ImportParserPlugin {
|
||||||
parser.state.module.addWarning(
|
parser.state.module.addWarning(
|
||||||
new UnsupportedFeatureWarning(
|
new UnsupportedFeatureWarning(
|
||||||
`\`webpackMode\` expected 'lazy', 'lazy-once', 'eager' or 'weak', but received: ${mode}.`,
|
`\`webpackMode\` expected 'lazy', 'lazy-once', 'eager' or 'weak', but received: ${mode}.`,
|
||||||
expr.loc
|
/** @type {DependencyLocation} */ (expr.loc)
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
mode = "lazy";
|
mode = "lazy";
|
||||||
|
@ -243,7 +250,7 @@ class ImportParserPlugin {
|
||||||
parser.state.module.addWarning(
|
parser.state.module.addWarning(
|
||||||
new UnsupportedFeatureWarning(
|
new UnsupportedFeatureWarning(
|
||||||
`\`webpackExports\` could not be used with destructuring assignment.`,
|
`\`webpackExports\` could not be used with destructuring assignment.`,
|
||||||
expr.loc
|
/** @type {DependencyLocation} */ (expr.loc)
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -253,15 +260,15 @@ class ImportParserPlugin {
|
||||||
if (param.isString()) {
|
if (param.isString()) {
|
||||||
if (mode === "eager") {
|
if (mode === "eager") {
|
||||||
const dep = new ImportEagerDependency(
|
const dep = new ImportEagerDependency(
|
||||||
param.string,
|
/** @type {string} */ (param.string),
|
||||||
expr.range,
|
/** @type {Range} */ (expr.range),
|
||||||
exports
|
exports
|
||||||
);
|
);
|
||||||
parser.state.current.addDependency(dep);
|
parser.state.current.addDependency(dep);
|
||||||
} else if (mode === "weak") {
|
} else if (mode === "weak") {
|
||||||
const dep = new ImportWeakDependency(
|
const dep = new ImportWeakDependency(
|
||||||
param.string,
|
/** @type {string} */ (param.string),
|
||||||
expr.range,
|
/** @type {Range} */ (expr.range),
|
||||||
exports
|
exports
|
||||||
);
|
);
|
||||||
parser.state.current.addDependency(dep);
|
parser.state.current.addDependency(dep);
|
||||||
|
@ -271,11 +278,15 @@ class ImportParserPlugin {
|
||||||
...groupOptions,
|
...groupOptions,
|
||||||
name: chunkName
|
name: chunkName
|
||||||
},
|
},
|
||||||
expr.loc,
|
/** @type {DependencyLocation} */ (expr.loc),
|
||||||
param.string
|
param.string
|
||||||
);
|
);
|
||||||
const dep = new ImportDependency(param.string, expr.range, exports);
|
const dep = new ImportDependency(
|
||||||
dep.loc = expr.loc;
|
/** @type {string} */ (param.string),
|
||||||
|
/** @type {Range} */ (expr.range),
|
||||||
|
exports
|
||||||
|
);
|
||||||
|
dep.loc = /** @type {DependencyLocation} */ (expr.loc);
|
||||||
depBlock.addDependency(dep);
|
depBlock.addDependency(dep);
|
||||||
parser.state.current.addBlock(depBlock);
|
parser.state.current.addBlock(depBlock);
|
||||||
}
|
}
|
||||||
|
@ -286,7 +297,7 @@ class ImportParserPlugin {
|
||||||
}
|
}
|
||||||
const dep = ContextDependencyHelpers.create(
|
const dep = ContextDependencyHelpers.create(
|
||||||
ImportContextDependency,
|
ImportContextDependency,
|
||||||
expr.range,
|
/** @type {Range} */ (expr.range),
|
||||||
param,
|
param,
|
||||||
expr,
|
expr,
|
||||||
this.options,
|
this.options,
|
||||||
|
@ -296,7 +307,9 @@ class ImportParserPlugin {
|
||||||
include,
|
include,
|
||||||
exclude,
|
exclude,
|
||||||
mode,
|
mode,
|
||||||
namespaceObject: parser.state.module.buildMeta.strictHarmonyModule
|
namespaceObject: /** @type {BuildMeta} */ (
|
||||||
|
parser.state.module.buildMeta
|
||||||
|
).strictHarmonyModule
|
||||||
? "strict"
|
? "strict"
|
||||||
: true,
|
: true,
|
||||||
typePrefix: "import()",
|
typePrefix: "import()",
|
||||||
|
@ -306,7 +319,7 @@ class ImportParserPlugin {
|
||||||
parser
|
parser
|
||||||
);
|
);
|
||||||
if (!dep) return;
|
if (!dep) return;
|
||||||
dep.loc = expr.loc;
|
dep.loc = /** @type {DependencyLocation} */ (expr.loc);
|
||||||
dep.optional = !!parser.scope.inTry;
|
dep.optional = !!parser.scope.inTry;
|
||||||
parser.state.current.addDependency(dep);
|
parser.state.current.addDependency(dep);
|
||||||
return true;
|
return true;
|
||||||
|
|
|
@ -21,7 +21,7 @@ class ImportWeakDependency extends ImportDependency {
|
||||||
/**
|
/**
|
||||||
* @param {string} request the request
|
* @param {string} request the request
|
||||||
* @param {Range} range expression range
|
* @param {Range} range expression range
|
||||||
* @param {string[][]=} referencedExports list of referenced exports
|
* @param {(string[][] | null)=} referencedExports list of referenced exports
|
||||||
*/
|
*/
|
||||||
constructor(request, range, referencedExports) {
|
constructor(request, range, referencedExports) {
|
||||||
super(request, range, referencedExports);
|
super(request, range, referencedExports);
|
||||||
|
|
|
@ -2840,7 +2840,7 @@ declare interface ContextModuleOptions {
|
||||||
/**
|
/**
|
||||||
* exports referenced from modules (won't be mangled)
|
* exports referenced from modules (won't be mangled)
|
||||||
*/
|
*/
|
||||||
referencedExports?: string[][];
|
referencedExports?: null | string[][];
|
||||||
layer?: string;
|
layer?: string;
|
||||||
resource: string | false | string[];
|
resource: string | false | string[];
|
||||||
resourceQuery?: string;
|
resourceQuery?: string;
|
||||||
|
|
Loading…
Reference in New Issue