| | |
| | | "use strict"; |
| | | |
| | | const querystring = require("querystring"); |
| | | const parseJson = require("json-parse-even-better-errors"); |
| | | const { getContext, runLoaders } = require("loader-runner"); |
| | | const { |
| | | AsyncSeriesBailHook, |
| | |
| | | } = require("./util/identifier"); |
| | | const makeSerializable = require("./util/makeSerializable"); |
| | | const memoize = require("./util/memoize"); |
| | | const parseJson = require("./util/parseJson"); |
| | | |
| | | /** @typedef {import("enhanced-resolve").ResolveContext} ResolveContext */ |
| | | /** @typedef {import("enhanced-resolve").ResolveRequest} ResolveRequest */ |
| | |
| | | /** @typedef {import("./Module").NeedBuildCallback} NeedBuildCallback */ |
| | | /** @typedef {import("./Module").BuildCallback} BuildCallback */ |
| | | /** @typedef {import("./Module").RuntimeRequirements} RuntimeRequirements */ |
| | | /** @typedef {import("./Generator").SourceTypes} SourceTypes */ |
| | | /** @typedef {import("./Module").Sources} Sources */ |
| | | /** @typedef {import("./Module").SourceType} SourceType */ |
| | | /** @typedef {import("./Module").SourceTypes} SourceTypes */ |
| | | /** @typedef {import("./Module").UnsafeCacheData} UnsafeCacheData */ |
| | | /** @typedef {import("./ModuleGraph")} ModuleGraph */ |
| | | /** @typedef {import("./ModuleGraphConnection").ConnectionState} ConnectionState */ |
| | |
| | | * @typedef {import("../declarations/LoaderContext").NormalModuleLoaderContext<T>} NormalModuleLoaderContext |
| | | */ |
| | | |
| | | /** @typedef {(content: string) => boolean} NoParseFn */ |
| | | |
| | | const getInvalidDependenciesModuleWarning = memoize(() => |
| | | require("./InvalidDependenciesModuleWarning") |
| | | ); |
| | |
| | | |
| | | const getValidate = memoize(() => require("schema-utils").validate); |
| | | |
| | | const ABSOLUTE_PATH_REGEX = /^([a-zA-Z]:\\|\\\\|\/)/; |
| | | const ABSOLUTE_PATH_REGEX = /^(?:[a-z]:\\|\\\\|\/)/i; |
| | | |
| | | /** |
| | | * @typedef {object} LoaderItem |
| | |
| | | /** @typedef {LoaderContext<EXPECTED_ANY>} AnyLoaderContext */ |
| | | |
| | | /** |
| | | * @deprecated Use the `readResource` hook instead. |
| | | * @typedef {HookMap<FakeHook<AsyncSeriesBailHook<[string, NormalModule], string | Buffer | null>>>} DeprecatedReadResourceForScheme |
| | | */ |
| | | |
| | | /** |
| | | * @typedef {object} NormalModuleCompilationHooks |
| | | * @property {SyncHook<[AnyLoaderContext, NormalModule]>} loader |
| | | * @property {SyncHook<[LoaderItem[], NormalModule, AnyLoaderContext]>} beforeLoaders |
| | | * @property {SyncHook<[NormalModule]>} beforeParse |
| | | * @property {SyncHook<[NormalModule]>} beforeSnapshot |
| | | * @property {HookMap<FakeHook<AsyncSeriesBailHook<[string, NormalModule], string | Buffer | null>>>} readResourceForScheme |
| | | * @property {DeprecatedReadResourceForScheme} readResourceForScheme |
| | | * @property {HookMap<AsyncSeriesBailHook<[AnyLoaderContext], string | Buffer | null>>} readResource |
| | | * @property {SyncWaterfallHook<[Result, NormalModule]>} processResult |
| | | * @property {AsyncSeriesBailHook<[NormalModule, NeedBuildContext], boolean>} needBuild |
| | |
| | | /** @type {NormalModuleCreateData['rawRequest']} */ |
| | | this.rawRequest = rawRequest; |
| | | /** @type {boolean} */ |
| | | this.binary = /^(asset|webassembly)\b/.test(type); |
| | | this.binary = /^(?:asset|webassembly)\b/.test(type); |
| | | /** @type {NormalModuleCreateData['parser'] | undefined} */ |
| | | this.parser = parser; |
| | | /** @type {NormalModuleCreateData['parserOptions']} */ |
| | |
| | | this._source = null; |
| | | /** |
| | | * @private |
| | | * @type {Map<string | undefined, number> | undefined} |
| | | * @type {Map<undefined | SourceType, number> | undefined} |
| | | */ |
| | | this._sourceSizes = undefined; |
| | | /** |
| | |
| | | * @type {undefined | SourceTypes} |
| | | */ |
| | | this._sourceTypes = undefined; |
| | | |
| | | // Cache |
| | | /** |
| | | * @private |
| | | * @type {BuildMeta} |
| | | */ |
| | | this._lastSuccessfulBuildMeta = {}; |
| | | /** |
| | | * @private |
| | | * @type {boolean} |
| | | */ |
| | | this._forceBuild = true; |
| | | /** |
| | | * @private |
| | | * @type {boolean} |
| | | */ |
| | | this._isEvaluatingSideEffects = false; |
| | | /** @type {WeakSet<ModuleGraph> | undefined} */ |
| | | /** |
| | | * @private |
| | | * @type {WeakSet<ModuleGraph> | undefined} |
| | | */ |
| | | this._addedSideEffectsBailout = undefined; |
| | | /** @type {CodeGenerationResultData} */ |
| | | /** |
| | | * @private |
| | | * @type {CodeGenerationResultData} |
| | | */ |
| | | this._codeGeneratorData = new Map(); |
| | | } |
| | | |
| | | /** |
| | | * Returns the unique identifier used to reference this module. |
| | | * @returns {string} a unique identifier of the module |
| | | */ |
| | | identifier() { |
| | |
| | | } |
| | | |
| | | /** |
| | | * Returns a human-readable identifier for this module. |
| | | * @param {RequestShortener} requestShortener the request shortener |
| | | * @returns {string} a user readable identifier of the module |
| | | */ |
| | |
| | | } |
| | | |
| | | /** |
| | | * Gets the library identifier. |
| | | * @param {LibIdentOptions} options options |
| | | * @returns {LibIdent | null} an identifier for library inclusion |
| | | */ |
| | |
| | | } |
| | | |
| | | /** |
| | | * Returns the path used when matching this module against rule conditions. |
| | | * @returns {NameForCondition | null} absolute path which should be used for condition matching (usually the resource path) |
| | | */ |
| | | nameForCondition() { |
| | |
| | | if (typeof options === "string") { |
| | | if (options.startsWith("{") && options.endsWith("}")) { |
| | | try { |
| | | options = parseJson(options); |
| | | options = |
| | | /** @type {LoaderItem["options"]} */ |
| | | (parseJson(options)); |
| | | } catch (err) { |
| | | throw new Error( |
| | | `Cannot parse string options: ${/** @type {Error} */ (err).message}` |
| | | `Cannot parse string options: ${/** @type {Error} */ (err).message}`, |
| | | { cause: err } |
| | | ); |
| | | } |
| | | } else { |
| | |
| | | options = {}; |
| | | } |
| | | |
| | | if (schema) { |
| | | if (schema && compilation.options.validate) { |
| | | let name = "Loader"; |
| | | let baseDataPath = "options"; |
| | | /** @type {RegExpExecArray | null} */ |
| | | let match; |
| | | if (schema.title && (match = /^(.+) (.+)$/.exec(schema.title))) { |
| | | [, name, baseDataPath] = match; |
| | |
| | | return /** @type {AnyLoaderContext} */ (loaderContext); |
| | | } |
| | | |
| | | // TODO remove `loaderContext` in webpack@6 |
| | | /** |
| | | * @param {AnyLoaderContext} loaderContext loader context |
| | | * @param {number} index index |
| | |
| | | compilation.compiler.root |
| | | ); |
| | | if (this._sourceSizes !== undefined) this._sourceSizes.clear(); |
| | | /** @type {PreparsedAst | null} */ |
| | | this._ast = |
| | | typeof extraInfo === "object" && |
| | | extraInfo !== null && |
| | |
| | | } |
| | | |
| | | /** |
| | | * Builds the module using the provided compilation context. |
| | | * @param {WebpackOptions} options webpack options |
| | | * @param {Compilation} compilation the compilation |
| | | * @param {ResolverWithOptions} resolver the resolver |
| | |
| | | /** @type {undefined | Set<string>} */ |
| | | let nonAbsoluteDependencies; |
| | | /** |
| | | * @param {LazySet<string>} deps deps |
| | | * @param {FileSystemDependencies} deps deps |
| | | */ |
| | | const checkDependencies = (deps) => { |
| | | for (const dep of deps) { |
| | |
| | | } |
| | | |
| | | /** |
| | | * Returns the reason this module cannot be concatenated, when one exists. |
| | | * @param {ConcatenationBailoutReasonContext} context context |
| | | * @returns {string | undefined} reason why this module can't be concatenated, undefined when it can be concatenated |
| | | */ |
| | |
| | | } |
| | | |
| | | /** |
| | | * Gets side effects connection state. |
| | | * @param {ModuleGraph} moduleGraph the module graph |
| | | * @returns {ConnectionState} how this module should be connected to referencing modules when consumed for side-effects only |
| | | */ |
| | |
| | | } |
| | | |
| | | /** |
| | | * Returns the source types this module can generate. |
| | | * @returns {SourceTypes} types available (do not mutate) |
| | | */ |
| | | getSourceTypes() { |
| | |
| | | } |
| | | |
| | | /** |
| | | * Generates code and runtime requirements for this module. |
| | | * @param {CodeGenerationContext} context context for code generation |
| | | * @returns {CodeGenerationResult} result |
| | | */ |
| | |
| | | |
| | | const getData = () => this._codeGeneratorData; |
| | | |
| | | /** @type {Sources} */ |
| | | const sources = new Map(); |
| | | for (const type of sourceTypes || chunkGraph.getModuleSourceTypes(this)) { |
| | | // TODO webpack@6 make generateError required |
| | |
| | | } |
| | | |
| | | /** |
| | | * Gets the original source. |
| | | * @returns {Source | null} the original source for the module before webpack transformation |
| | | */ |
| | | originalSource() { |
| | |
| | | } |
| | | |
| | | /** |
| | | * Invalidates the cached state associated with this value. |
| | | * @returns {void} |
| | | */ |
| | | invalidateBuild() { |
| | |
| | | } |
| | | |
| | | /** |
| | | * Checks whether the module needs to be rebuilt for the current build state. |
| | | * @param {NeedBuildContext} context context info |
| | | * @param {NeedBuildCallback} callback callback function, returns true, if the module needs a rebuild |
| | | * @returns {void} |
| | |
| | | } |
| | | |
| | | /** |
| | | * Returns the estimated size for the requested source type. |
| | | * @param {string=} type the source type for which the size should be estimated |
| | | * @returns {number} the estimated size of the module (must be non-zero) |
| | | */ |
| | |
| | | } |
| | | |
| | | /** |
| | | * Adds the provided file dependencies to the module. |
| | | * @param {FileSystemDependencies} fileDependencies set where file dependencies are added to |
| | | * @param {FileSystemDependencies} contextDependencies set where context dependencies are added to |
| | | * @param {FileSystemDependencies} missingDependencies set where missing dependencies are added to |
| | |
| | | } |
| | | |
| | | /** |
| | | * Updates the hash with the data contributed by this instance. |
| | | * @param {Hash} hash the hash used to track dependencies |
| | | * @param {UpdateHashContext} context context |
| | | * @returns {void} |
| | |
| | | /** @type {string} */ |
| | | (buildInfo.hash) |
| | | ); |
| | | // Clear cached source types and re-compute so that changes in incoming |
| | | // connections (e.g. asset module newly referenced from JS via lazy |
| | | // compilation) are reflected in the hash and trigger code generation |
| | | // cache invalidation. |
| | | // https://github.com/webpack/webpack/issues/20800 |
| | | this._sourceTypes = undefined; |
| | | for (const type of this.getSourceTypes()) { |
| | | hash.update(type); |
| | | } |
| | | /** @type {Generator} */ |
| | | (this.generator).updateHash(hash, { |
| | | module: this, |
| | |
| | | } |
| | | |
| | | /** |
| | | * Serializes this instance into the provided serializer context. |
| | | * @param {ObjectSerializerContext} context context |
| | | */ |
| | | serialize(context) { |
| | |
| | | } |
| | | |
| | | /** |
| | | * Restores this instance from the provided deserializer context. |
| | | * @param {ObjectDeserializerContext} context context |
| | | */ |
| | | deserialize(context) { |