diff --git a/declarations.d.ts b/declarations.d.ts index 02f591d5419..9bd0c3c63e5 100644 --- a/declarations.d.ts +++ b/declarations.d.ts @@ -299,7 +299,8 @@ declare module "webpack-sources" { name: string, sourceMap: Object | string | Buffer, originalSource?: string | Buffer, - innerSourceMap?: Object | string | Buffer + innerSourceMap?: Object | string | Buffer, + removeOriginalSource?: boolean ); getArgsAsBuffers(): [ @@ -307,7 +308,8 @@ declare module "webpack-sources" { string, Buffer, Buffer | undefined, - Buffer | undefined + Buffer | undefined, + boolean ]; } diff --git a/lib/Compilation.js b/lib/Compilation.js index 30b6d6de0e1..13ab4627a28 100644 --- a/lib/Compilation.js +++ b/lib/Compilation.js @@ -167,7 +167,7 @@ const { isSourceEqual } = require("./util/source"); */ /** - * @typedef {Object} AssetInfo + * @typedef {Object} KnownAssetInfo * @property {boolean=} immutable true, if the asset can be long term cached forever (contains a hash) * @property {boolean=} minimized whether the asset is minimized * @property {string | string[]=} fullhash the value(s) of the full hash used for this asset @@ -182,6 +182,8 @@ const { isSourceEqual } = require("./util/source"); * @property {Record=} related object of pointers to other assets, keyed by type of relation (only points from parent to child) */ +/** @typedef {KnownAssetInfo & Record} AssetInfo */ + /** * @typedef {Object} Asset * @property {string} name the filename of the asset diff --git a/lib/index.js b/lib/index.js index dabb0abfcbe..ee716a36c40 100644 --- a/lib/index.js +++ b/lib/index.js @@ -22,6 +22,8 @@ const memorize = require("./util/memorize"); /** @typedef {import("../declarations/WebpackOptions").WebpackOptionsNormalized} WebpackOptionsNormalized */ /** @typedef {import("../declarations/WebpackOptions").WebpackPluginFunction} WebpackPluginFunction */ /** @typedef {import("../declarations/WebpackOptions").WebpackPluginInstance} WebpackPluginInstance */ +/** @typedef {import("./Compilation").Asset} Asset */ +/** @typedef {import("./Compilation").AssetInfo} AssetInfo */ /** @typedef {import("./Parser").ParserState} ParserState */ /** @@ -260,6 +262,9 @@ module.exports = mergeExports(fn, { get WatchIgnorePlugin() { return require("./WatchIgnorePlugin"); }, + get WebpackError() { + return require("./WebpackError"); + }, get WebpackOptionsApply() { return require("./WebpackOptionsApply"); }, diff --git a/lib/validateSchema.js b/lib/validateSchema.js index 8a26ce77264..49bfff147c6 100644 --- a/lib/validateSchema.js +++ b/lib/validateSchema.js @@ -69,94 +69,105 @@ const REMOVED = { }; /* cSpell:enable */ -const validateSchema = (schema, options) => { - validate(schema, options, { - name: "Webpack", - postFormatter: (formattedError, error) => { - const children = error.children; - if ( - children && - children.some( - child => - child.keyword === "absolutePath" && - child.dataPath === ".output.filename" - ) - ) { - return `${formattedError}\nPlease use output.path to specify absolute path and output.filename for the file name.`; - } - - if ( - children && - children.some( - child => child.keyword === "pattern" && child.dataPath === ".devtool" - ) - ) { - return ( - `${formattedError}\n` + - "BREAKING CHANGE since webpack 5: The devtool option is more strict.\n" + - "Please strictly follow the order of the keywords in the pattern." - ); - } - - if (error.keyword === "additionalProperties") { - const params = /** @type {import("ajv").AdditionalPropertiesParams} */ (error.params); +/** + * @param {Parameters[0]} schema a json schema + * @param {Parameters[1]} options the options that should be validated + * @param {Parameters[2]=} validationConfiguration configuration for generating errors + * @returns {void} + */ +const validateSchema = (schema, options, validationConfiguration) => { + validate( + schema, + options, + validationConfiguration || { + name: "Webpack", + postFormatter: (formattedError, error) => { + const children = error.children; if ( - Object.prototype.hasOwnProperty.call( - DID_YOU_MEAN, - params.additionalProperty + children && + children.some( + child => + child.keyword === "absolutePath" && + child.dataPath === ".output.filename" ) ) { - return `${formattedError}\nDid you mean ${ - DID_YOU_MEAN[params.additionalProperty] - }?`; + return `${formattedError}\nPlease use output.path to specify absolute path and output.filename for the file name.`; } if ( - Object.prototype.hasOwnProperty.call( - REMOVED, - params.additionalProperty + children && + children.some( + child => + child.keyword === "pattern" && child.dataPath === ".devtool" ) ) { - return `${formattedError}\n${REMOVED[params.additionalProperty]}?`; + return ( + `${formattedError}\n` + + "BREAKING CHANGE since webpack 5: The devtool option is more strict.\n" + + "Please strictly follow the order of the keywords in the pattern." + ); } - if (!error.dataPath) { - if (params.additionalProperty === "debug") { - return ( - `${formattedError}\n` + - "The 'debug' property was removed in webpack 2.0.0.\n" + - "Loaders should be updated to allow passing this option via loader options in module.rules.\n" + - "Until loaders are updated one can use the LoaderOptionsPlugin to switch loaders into debug mode:\n" + - "plugins: [\n" + - " new webpack.LoaderOptionsPlugin({\n" + - " debug: true\n" + - " })\n" + - "]" - ); + if (error.keyword === "additionalProperties") { + const params = /** @type {import("ajv").AdditionalPropertiesParams} */ (error.params); + if ( + Object.prototype.hasOwnProperty.call( + DID_YOU_MEAN, + params.additionalProperty + ) + ) { + return `${formattedError}\nDid you mean ${ + DID_YOU_MEAN[params.additionalProperty] + }?`; } - if (params.additionalProperty) { - return ( - `${formattedError}\n` + - "For typos: please correct them.\n" + - "For loader options: webpack >= v2.0.0 no longer allows custom properties in configuration.\n" + - " Loaders should be updated to allow passing options via loader options in module.rules.\n" + - " Until loaders are updated one can use the LoaderOptionsPlugin to pass these options to the loader:\n" + - " plugins: [\n" + - " new webpack.LoaderOptionsPlugin({\n" + - " // test: /\\.xxx$/, // may apply this only for some modules\n" + - " options: {\n" + - ` ${params.additionalProperty}: …\n` + - " }\n" + - " })\n" + - " ]" - ); + if ( + Object.prototype.hasOwnProperty.call( + REMOVED, + params.additionalProperty + ) + ) { + return `${formattedError}\n${REMOVED[params.additionalProperty]}?`; + } + + if (!error.dataPath) { + if (params.additionalProperty === "debug") { + return ( + `${formattedError}\n` + + "The 'debug' property was removed in webpack 2.0.0.\n" + + "Loaders should be updated to allow passing this option via loader options in module.rules.\n" + + "Until loaders are updated one can use the LoaderOptionsPlugin to switch loaders into debug mode:\n" + + "plugins: [\n" + + " new webpack.LoaderOptionsPlugin({\n" + + " debug: true\n" + + " })\n" + + "]" + ); + } + + if (params.additionalProperty) { + return ( + `${formattedError}\n` + + "For typos: please correct them.\n" + + "For loader options: webpack >= v2.0.0 no longer allows custom properties in configuration.\n" + + " Loaders should be updated to allow passing options via loader options in module.rules.\n" + + " Until loaders are updated one can use the LoaderOptionsPlugin to pass these options to the loader:\n" + + " plugins: [\n" + + " new webpack.LoaderOptionsPlugin({\n" + + " // test: /\\.xxx$/, // may apply this only for some modules\n" + + " options: {\n" + + ` ${params.additionalProperty}: …\n` + + " }\n" + + " })\n" + + " ]" + ); + } } } - } - return formattedError; + return formattedError; + } } - }); + ); }; module.exports = validateSchema; diff --git a/package.json b/package.json index 912ce6e3e38..430c446b8c4 100644 --- a/package.json +++ b/package.json @@ -92,7 +92,7 @@ "style-loader": "^2.0.0", "terser": "^5.5.0", "toml": "^3.0.0", - "tooling": "webpack/tooling#v1.10.1", + "tooling": "webpack/tooling#v1.10.2", "ts-loader": "^8.0.2", "typescript": "^4.2.0-dev.20201130", "url-loader": "^4.1.0", diff --git a/types.d.ts b/types.d.ts index 7b2884ff271..6fad487968c 100644 --- a/types.d.ts +++ b/types.d.ts @@ -78,7 +78,12 @@ import { YieldExpression } from "estree"; import { Stats as FsStats, WriteStream } from "fs"; +import { JSONSchema4, JSONSchema6, JSONSchema7 } from "json-schema"; import { default as ValidationError } from "schema-utils/declarations/ValidationError"; +import { + Extend, + ValidationErrorConfiguration +} from "schema-utils/declarations/validate"; import { AsArray, AsyncParallelHook, @@ -207,67 +212,7 @@ declare interface AssetEmittedInfo { outputPath: string; targetPath: string; } -declare interface AssetInfo { - /** - * true, if the asset can be long term cached forever (contains a hash) - */ - immutable?: boolean; - - /** - * whether the asset is minimized - */ - minimized?: boolean; - - /** - * the value(s) of the full hash used for this asset - */ - fullhash?: string | string[]; - - /** - * the value(s) of the chunk hash used for this asset - */ - chunkhash?: string | string[]; - - /** - * the value(s) of the module hash used for this asset - */ - modulehash?: string | string[]; - - /** - * the value(s) of the content hash used for this asset - */ - contenthash?: string | string[]; - - /** - * when asset was created from a source file (potentially transformed), the original filename relative to compilation context - */ - sourceFilename?: string; - - /** - * size in bytes, only set after asset has been emitted - */ - size?: number; - - /** - * true, when asset is only used for development and doesn't count towards user-facing assets - */ - development?: boolean; - - /** - * true, when asset ships data for updating an existing application (HMR) - */ - hotModuleReplacement?: boolean; - - /** - * true, when asset is javascript and an ESM - */ - javascriptModule?: boolean; - - /** - * object of pointers to other assets, keyed by type of relation (only points from parent to child) - */ - related?: Record; -} +type AssetInfo = KnownAssetInfo & Record; declare abstract class AsyncDependenciesBlock extends DependenciesBlock { groupOptions: { preloadOrder?: number; @@ -4459,6 +4404,67 @@ declare class JsonpTemplatePlugin { compilation: Compilation ): JsonpCompilationPluginHooks; } +declare interface KnownAssetInfo { + /** + * true, if the asset can be long term cached forever (contains a hash) + */ + immutable?: boolean; + + /** + * whether the asset is minimized + */ + minimized?: boolean; + + /** + * the value(s) of the full hash used for this asset + */ + fullhash?: string | string[]; + + /** + * the value(s) of the chunk hash used for this asset + */ + chunkhash?: string | string[]; + + /** + * the value(s) of the module hash used for this asset + */ + modulehash?: string | string[]; + + /** + * the value(s) of the content hash used for this asset + */ + contenthash?: string | string[]; + + /** + * when asset was created from a source file (potentially transformed), the original filename relative to compilation context + */ + sourceFilename?: string; + + /** + * size in bytes, only set after asset has been emitted + */ + size?: number; + + /** + * true, when asset is only used for development and doesn't count towards user-facing assets + */ + development?: boolean; + + /** + * true, when asset ships data for updating an existing application (HMR) + */ + hotModuleReplacement?: boolean; + + /** + * true, when asset is javascript and an ESM + */ + javascriptModule?: boolean; + + /** + * object of pointers to other assets, keyed by type of relation (only points from parent to child) + */ + related?: Record; +} declare interface KnownBuildMeta { moduleArgument?: string; exportsArgument?: string; @@ -8603,6 +8609,10 @@ declare abstract class RuntimeValue { fileDependencies: any; exec(parser?: any): any; } +type Schema = + | (JSONSchema4 & Extend) + | (JSONSchema6 & Extend) + | (JSONSchema7 & Extend); declare interface ScopeInfo { definitions: StackedMap; topLevelScope: boolean | "arrow"; @@ -8969,14 +8979,16 @@ declare class SourceMapSource extends Source { name: string, sourceMap: string | Object | Buffer, originalSource?: string | Buffer, - innerSourceMap?: string | Object | Buffer + innerSourceMap?: string | Object | Buffer, + removeOriginalSource?: boolean ); getArgsAsBuffers(): [ Buffer, string, Buffer, undefined | Buffer, - undefined | Buffer + undefined | Buffer, + boolean ]; } declare interface SourcePosition { @@ -9565,7 +9577,7 @@ declare interface UpdateHashContextGenerator { chunkGraph: ChunkGraph; runtime: RuntimeSpec; } -type UsageStateType = 0 | 1 | 2 | 3 | 4; +type UsageStateType = 0 | 2 | 3 | 1 | 4; declare interface UserResolveOptions { /** * A list of module alias configurations or an object which maps key to value @@ -9878,7 +9890,11 @@ declare class WebWorkerTemplatePlugin { */ apply(compiler: Compiler): void; } -declare interface WebpackError extends Error { +declare class WebpackError extends Error { + /** + * Creates an instance of WebpackError. + */ + constructor(message?: string); details: any; module: Module; loc: DependencyLocation; @@ -9887,6 +9903,20 @@ declare interface WebpackError extends Error { file: string; serialize(__0: { write: any }): void; deserialize(__0: { read: any }): void; + + /** + * Create .stack property on a target object + */ + static captureStackTrace(targetObject: {}, constructorOpt?: Function): void; + + /** + * Optional override for formatting stack traces + */ + static prepareStackTrace?: ( + err: Error, + stackTraces: NodeJS.CallSite[] + ) => any; + static stackTraceLimit: number; } declare abstract class WebpackLogger { getChildLogger: (arg0: string | (() => string)) => WebpackLogger; @@ -10175,7 +10205,11 @@ declare namespace exports { ): MultiCompiler; }; export const validate: (options?: any) => void; - export const validateSchema: (schema?: any, options?: any) => void; + export const validateSchema: ( + schema: Schema, + options: {} | {}[], + validationConfiguration?: ValidationErrorConfiguration + ) => void; export const version: string; export namespace cli { export let getArguments: (schema?: any) => Record; @@ -10546,6 +10580,7 @@ declare namespace exports { Stats, Template, WatchIgnorePlugin, + WebpackError, WebpackOptionsApply, WebpackOptionsDefaulter, Entry, @@ -10561,6 +10596,8 @@ declare namespace exports { Configuration, WebpackOptionsNormalized, WebpackPluginInstance, + Asset, + AssetInfo, ParserState }; } diff --git a/yarn.lock b/yarn.lock index cd23ad6db0e..ef7099546f1 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6190,9 +6190,9 @@ toml@^3.0.0: resolved "https://registry.yarnpkg.com/toml/-/toml-3.0.0.tgz#342160f1af1904ec9d204d03a5d61222d762c5ee" integrity sha512-y/mWCZinnvxjTKYhJ+pYxwD0mRLVvOtdS2Awbgxln6iEnt4rk0yBxeSBHkGJcPucRiG0e55mwWp+g/05rsrd6w== -tooling@webpack/tooling#v1.10.1: - version "1.10.1" - resolved "https://codeload.github.com/webpack/tooling/tar.gz/48b15e5b6f489be5697accff1deff5cf574ee8b2" +tooling@webpack/tooling#v1.10.2: + version "1.10.2" + resolved "https://codeload.github.com/webpack/tooling/tar.gz/b1f159c522332464f92e0eece675db144091ef12" dependencies: "@yarnpkg/lockfile" "^1.1.0" commondir "^1.0.1"