improve code, remove unneeded old stuff

This commit is contained in:
Tobias Koppers 2020-01-16 14:48:52 +01:00
parent 4e808cb613
commit f9846f1f91
4 changed files with 43 additions and 40 deletions

View File

@ -52,6 +52,13 @@ const makeSerializable = require("./util/makeSerializable");
/** @typedef {import("./util/Hash")} Hash */ /** @typedef {import("./util/Hash")} Hash */
/** @typedef {import("./util/fs").InputFileSystem} InputFileSystem */ /** @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} context absolute context path
* @param {string} source a source path * @param {string} source a source path
@ -169,7 +176,7 @@ class NormalModule extends Module {
* @param {string} options.request request string * @param {string} options.request request string
* @param {string} options.userRequest request intented by user (without loaders from config) * @param {string} options.userRequest request intented by user (without loaders from config)
* @param {string} options.rawRequest request without resolving * @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} options.resource path + query of the real resource
* @param {string | undefined} options.matchResource path + query of the matched resource (virtuel * @param {string | undefined} options.matchResource path + query of the matched resource (virtuel
* @param {Parser} options.parser the parser used * @param {Parser} options.parser the parser used
@ -207,7 +214,7 @@ class NormalModule extends Module {
this.resource = resource; this.resource = resource;
/** @type {string | undefined} */ /** @type {string | undefined} */
this.matchResource = matchResource; this.matchResource = matchResource;
/** @type {TODO[]} */ /** @type {LoaderItem[]} */
this.loaders = loaders; this.loaders = loaders;
if (resolveOptions !== undefined) { if (resolveOptions !== undefined) {
// already declared in super class // already declared in super class
@ -353,47 +360,35 @@ class NormalModule extends Module {
}; };
const loaderContext = { const loaderContext = {
version: 2, version: 2,
getOptions: (loaderName, schema) => { getOptions: schema => {
const loader = loaderContext.loaders[loaderContext.loaderIndex]; const loader = this.getCurrentLoader(loaderContext);
let options = {};
if (loader.options && typeof loader.options === "object") { let { options } = loader;
({ options } = loader);
} else if (loader.query) {
let { query } = loader;
if (query.substr(0, 1) !== "?") { if (typeof options === "string") {
throw new WebpackError( if (options.substr(0, 1) === "{" && options.substr(-1) === "}") {
"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) === "}") {
try { try {
options = parseJson(query); options = parseJson(options);
} catch (e) { } catch (e) {
throw new WebpackError(`Cannot parse query string: ${e.message}`); throw new Error(`Cannot parse string options: ${e.message}`);
} }
} else { } else {
options = querystring.parse(query); options = querystring.parse(options, "&", "=", {
maxKeys: 0
});
} }
} }
if (!schema) { if (options === null || options === undefined) {
return options; options = {};
} }
validateOptions(schema, options, { if (schema) {
name: loaderName || "Unknown Loader", validateOptions(schema, options, {
baseDataPath: "options" name: getCurrentLoaderName(),
}); baseDataPath: "options"
});
}
return options; return options;
}, },

View File

@ -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/ }
];

View File

@ -1,12 +1,11 @@
const schema = require('./loader-1.options'); const schema = require("./loader-1.options");
module.exports = function() { module.exports = function() {
const options = this.getOptions('Loader Name', schema); const options = this.getOptions(schema);
const json = JSON.stringify(options) const json = JSON.stringify(options)
.replace(/\u2028/g, '\\u2028') .replace(/\u2028/g, "\\u2028")
.replace(/\u2029/g, '\\u2029'); .replace(/\u2029/g, "\\u2029");
return `module.exports = ${json}`; return `module.exports = ${json}`;
}; };

View File

@ -49,7 +49,7 @@ module.exports = {
{ {
test: /d\.js$/, test: /d\.js$/,
loader: "./loader", loader: "./loader",
options: "?" options: ""
}, },
{ {
test: /f\.js$/, test: /f\.js$/,
@ -64,12 +64,12 @@ module.exports = {
{ {
test: /h\.js$/, test: /h\.js$/,
loader: "./loader", loader: "./loader",
options: "?foo=bar" options: "foo=bar"
}, },
{ {
test: /i\.js$/, test: /i\.js$/,
loader: "./loader", loader: "./loader",
options: `?${JSON.stringify({ options: `${JSON.stringify({
foo: "bar" foo: "bar"
})}` })}`
} }