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/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;
},

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() {
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}`;
};

View File

@ -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"
})}`
}