From f9846f1f91f1827fa351febad788f0d38b8e6115 Mon Sep 17 00:00:00 2001 From: Tobias Koppers Date: Thu, 16 Jan 2020 14:48:52 +0100 Subject: [PATCH] improve code, remove unneeded old stuff --- lib/NormalModule.js | 59 +++++++++---------- .../loaders/options/deprecations.js | 9 +++ test/configCases/loaders/options/loader-1.js | 9 ++- .../loaders/options/webpack.config.js | 6 +- 4 files changed, 43 insertions(+), 40 deletions(-) create mode 100644 test/configCases/loaders/options/deprecations.js diff --git a/lib/NormalModule.js b/lib/NormalModule.js index 1f8ea1818..3152d76d2 100644 --- a/lib/NormalModule.js +++ b/lib/NormalModule.js @@ -52,6 +52,13 @@ const makeSerializable = require("./util/makeSerializable"); /** @typedef {import("./util/Hash")} Hash */ /** @typedef {import("./util/fs").InputFileSystem} InputFileSystem */ +/** + * @typedef {Object} LoaderItem + * @property {string} loader + * @property {any} options + * @property {string?} ident + */ + /** * @param {string} context absolute context path * @param {string} source a source path @@ -169,7 +176,7 @@ class NormalModule extends Module { * @param {string} options.request request string * @param {string} options.userRequest request intented by user (without loaders from config) * @param {string} options.rawRequest request without resolving - * @param {TODO[]} options.loaders list of loaders + * @param {LoaderItem[]} options.loaders list of loaders * @param {string} options.resource path + query of the real resource * @param {string | undefined} options.matchResource path + query of the matched resource (virtuel * @param {Parser} options.parser the parser used @@ -207,7 +214,7 @@ class NormalModule extends Module { this.resource = resource; /** @type {string | undefined} */ this.matchResource = matchResource; - /** @type {TODO[]} */ + /** @type {LoaderItem[]} */ this.loaders = loaders; if (resolveOptions !== undefined) { // already declared in super class @@ -353,47 +360,35 @@ class NormalModule extends Module { }; const loaderContext = { version: 2, - getOptions: (loaderName, schema) => { - const loader = loaderContext.loaders[loaderContext.loaderIndex]; - let options = {}; + getOptions: schema => { + const loader = this.getCurrentLoader(loaderContext); - if (loader.options && typeof loader.options === "object") { - ({ options } = loader); - } else if (loader.query) { - let { query } = loader; + let { options } = loader; - if (query.substr(0, 1) !== "?") { - throw new WebpackError( - "A valid query string should begin with '?'" - ); - } - - query = query.substr(1); - - // Allow to use `?foo=bar` in `options` - if (query.substr(0, 1) === "?") { - query = query.substr(1); - } - - if (query.substr(0, 1) === "{" && query.substr(-1) === "}") { + if (typeof options === "string") { + if (options.substr(0, 1) === "{" && options.substr(-1) === "}") { try { - options = parseJson(query); + options = parseJson(options); } catch (e) { - throw new WebpackError(`Cannot parse query string: ${e.message}`); + throw new Error(`Cannot parse string options: ${e.message}`); } } else { - options = querystring.parse(query); + options = querystring.parse(options, "&", "=", { + maxKeys: 0 + }); } } - if (!schema) { - return options; + if (options === null || options === undefined) { + options = {}; } - validateOptions(schema, options, { - name: loaderName || "Unknown Loader", - baseDataPath: "options" - }); + if (schema) { + validateOptions(schema, options, { + name: getCurrentLoaderName(), + baseDataPath: "options" + }); + } return options; }, diff --git a/test/configCases/loaders/options/deprecations.js b/test/configCases/loaders/options/deprecations.js new file mode 100644 index 000000000..6c3c0c2f1 --- /dev/null +++ b/test/configCases/loaders/options/deprecations.js @@ -0,0 +1,9 @@ +module.exports = [ + { code: /DEP_WEBPACK_RULE_LOADER_OPTIONS_STRING/ }, + { code: /DEP_WEBPACK_RULE_LOADER_OPTIONS_STRING/ }, + { code: /DEP_WEBPACK_RULE_LOADER_OPTIONS_STRING/ }, + { code: /DEP_WEBPACK_RULE_LOADER_OPTIONS_STRING/ }, + { code: /DEP_WEBPACK_RULE_LOADER_OPTIONS_STRING/ }, + { code: /DEP_WEBPACK_RULE_LOADER_OPTIONS_STRING/ }, + { code: /DEP_WEBPACK_RULE_LOADER_OPTIONS_STRING/ } +]; diff --git a/test/configCases/loaders/options/loader-1.js b/test/configCases/loaders/options/loader-1.js index 5c8b11efa..f27763418 100644 --- a/test/configCases/loaders/options/loader-1.js +++ b/test/configCases/loaders/options/loader-1.js @@ -1,12 +1,11 @@ -const schema = require('./loader-1.options'); +const schema = require("./loader-1.options"); module.exports = function() { - const options = this.getOptions('Loader Name', schema); + const options = this.getOptions(schema); const json = JSON.stringify(options) - .replace(/\u2028/g, '\\u2028') - .replace(/\u2029/g, '\\u2029'); - + .replace(/\u2028/g, "\\u2028") + .replace(/\u2029/g, "\\u2029"); return `module.exports = ${json}`; }; diff --git a/test/configCases/loaders/options/webpack.config.js b/test/configCases/loaders/options/webpack.config.js index 0f343bd7b..4b4540148 100644 --- a/test/configCases/loaders/options/webpack.config.js +++ b/test/configCases/loaders/options/webpack.config.js @@ -49,7 +49,7 @@ module.exports = { { test: /d\.js$/, loader: "./loader", - options: "?" + options: "" }, { test: /f\.js$/, @@ -64,12 +64,12 @@ module.exports = { { test: /h\.js$/, loader: "./loader", - options: "?foo=bar" + options: "foo=bar" }, { test: /i\.js$/, loader: "./loader", - options: `?${JSON.stringify({ + options: `${JSON.stringify({ foo: "bar" })}` }