Skip to content

Commit

Permalink
Merge pull request #12224 from webpack/typings/missing
Browse files Browse the repository at this point in the history
add some missing types and exports
  • Loading branch information
sokra committed Dec 17, 2020
2 parents 41db807 + 16d5c35 commit 5b2fb32
Show file tree
Hide file tree
Showing 7 changed files with 203 additions and 146 deletions.
6 changes: 4 additions & 2 deletions declarations.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,15 +299,17 @@ declare module "webpack-sources" {
name: string,
sourceMap: Object | string | Buffer,
originalSource?: string | Buffer,
innerSourceMap?: Object | string | Buffer
innerSourceMap?: Object | string | Buffer,
removeOriginalSource?: boolean
);

getArgsAsBuffers(): [
Buffer,
string,
Buffer,
Buffer | undefined,
Buffer | undefined
Buffer | undefined,
boolean
];
}

Expand Down
4 changes: 3 additions & 1 deletion lib/Compilation.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -182,6 +182,8 @@ const { isSourceEqual } = require("./util/source");
* @property {Record<string, string | string[]>=} related object of pointers to other assets, keyed by type of relation (only points from parent to child)
*/

/** @typedef {KnownAssetInfo & Record<string, any>} AssetInfo */

/**
* @typedef {Object} Asset
* @property {string} name the filename of the asset
Expand Down
5 changes: 5 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 */

/**
Expand Down Expand Up @@ -260,6 +262,9 @@ module.exports = mergeExports(fn, {
get WatchIgnorePlugin() {
return require("./WatchIgnorePlugin");
},
get WebpackError() {
return require("./WebpackError");
},
get WebpackOptionsApply() {
return require("./WebpackOptionsApply");
},
Expand Down
157 changes: 84 additions & 73 deletions lib/validateSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof validate>[0]} schema a json schema
* @param {Parameters<typeof validate>[1]} options the options that should be validated
* @param {Parameters<typeof validate>[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;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Loading

0 comments on commit 5b2fb32

Please sign in to comment.