test typings and fix type problems in loaders in the test suite

This commit is contained in:
Tobias Koppers 2021-04-22 21:45:45 +02:00
parent ea53a23827
commit 4e70b37579
85 changed files with 283 additions and 152 deletions

View File

@ -1,6 +1,8 @@
/** @type {import("../../../../").LoaderDefinition} */
module.exports = function () {
var err = new Error("Message");
err.stack = "Stack";
//@ts-expect-error hideStack is not a property on normal errors
err.hideStack = true;
throw err;
};

View File

@ -1,3 +1,4 @@
/** @type {import("../../../../").LoaderDefinitionFunction} */
exports.default = function (source) {
const ref = JSON.parse(source);
const callback = this.async();

View File

@ -1,3 +1,4 @@
/** @type {import("../../../../").LoaderDefinitionFunction} */
exports.default = function (source) {
const callback = this.async();
const ref = JSON.parse(source);

View File

@ -1,4 +1,6 @@
/** @type {import("../../../../").LoaderDefinition<string>} */
module.exports = function (source) {
//@ts-expect-error errors must be Errors, string is not recommended and should lead to type error
this.emitError(this.query.substr(1));
return source;
}
};

View File

@ -1,4 +1,6 @@
/** @type {import("../../../../").LoaderDefinition<string>} */
module.exports = function (source) {
//@ts-expect-error warnings must be Errors, string is not recommended and should lead to type error
this.emitWarning(this.query.substr(1));
return source;
}
};

View File

@ -1,7 +1,9 @@
/** @type {import("../../../../../").LoaderDefinition} */
module.exports = function (content) {
var cb = this.async();
if (!cb) throw new Error("Loader should allow async mode");
if(cb !== this.callback) throw new Error("result of this.async() should be equal to this.callback");
if (cb !== this.callback)
throw new Error("result of this.async() should be equal to this.callback");
process.nextTick(function () {
cb(null, content);
});

View File

@ -1,3 +1,4 @@
/** @type {import("../../../../../").LoaderDefinition} */
module.exports = function (content) {
return content;
};

View File

@ -1,4 +1,5 @@
/** @type {import("../../../../").LoaderDefinition} */
module.exports = function (content) {
this.emitFile("extra-file.js", content);
return "";
}
};

View File

@ -2,22 +2,25 @@ const { getRemainingRequest, stringifyRequest } = require("loader-utils");
const loaderPath = require.resolve("./loader");
/** @type {import("../../../../").LoaderDefinition} */
module.exports = function () {
if (this.query === "?load") {
return `
import { answer } from "./lib";
export default answer;
`
`;
}
const matchResource = `${this.resourcePath}.js`;
const loader = `${loaderPath}?load`;
const remaining = getRemainingRequest(this);
const request = JSON.parse(stringifyRequest(this, `${matchResource}!=!${loader}!${remaining}`));
const request = JSON.parse(
stringifyRequest(this, `${matchResource}!=!${loader}!${remaining}`)
);
this.async();
this.loadModule(request, (err, source) => {
this.callback(err, source)
this.callback(err, source);
});
};

View File

@ -1,3 +1,4 @@
/** @type {import("../../../../").LoaderDefinition} */
module.exports = function (content) {
return content.split("").reverse().join("");
}
};

View File

@ -1,7 +1,11 @@
/** @type {import("../../../../../").LoaderDefinition} */
module.exports = function (content) {
return "module.exports = " + JSON.stringify({
return (
"module.exports = " +
JSON.stringify({
resourceQuery: this.resourceQuery,
query: this.query,
prev: content
});
}
})
);
};

View File

@ -1,4 +1,5 @@
const path = require("path");
/** @type {import("../../../../").LoaderDefinition} */
module.exports = function () {
const resolve1 = this.getResolve();
const resolve2 = this.getResolve({

View File

@ -1,3 +1,4 @@
/** @type {import("../../../../").LoaderDefinition} */
module.exports = function () {
return `module.exports = {
request1: ${JSON.stringify(

View File

@ -1,7 +1,11 @@
/** @type {import("../../../../../").LoaderDefinition} */
module.exports = function (content) {
return "module.exports = " + JSON.stringify({
return (
"module.exports = " +
JSON.stringify({
resourceQuery: this.resourceQuery,
query: this.query,
prev: content
});
})
);
};

View File

@ -3,6 +3,7 @@
const acorn = require("acorn");
const acornParser = acorn.Parser;
/** @type {import("../../../../").LoaderDefinition} */
module.exports = function (source) {
const comments = [];
@ -15,9 +16,12 @@ module.exports = function(source) {
});
// change something to test if it's really used
//@ts-ignore
ast.body[0].expression.right.arguments[0].value = "./ok";
ast.body[0].expression.right.arguments[0].raw = "\"./ok\"";
//@ts-ignore
ast.body[0].expression.right.arguments[0].raw = '"./ok"';
//@ts-ignore
ast.comments = comments;
this.callback(null, source, null, {
webpackAST: ast

View File

@ -1,7 +1,11 @@
/** @type {import("../../../../../").LoaderDefinition} */
module.exports = function (content) {
return "module.exports = " + JSON.stringify({
return (
"module.exports = " +
JSON.stringify({
resourceQuery: this.resourceQuery,
query: this.query,
prev: content
});
}
})
);
};

View File

@ -1,5 +1,6 @@
const stringifyRequest = require("loader-utils").stringifyRequest;
/** @type {import("../../../../").PitchLoaderDefinitionFunction} */
module.exports.pitch = function (remainingRequest) {
return `
import { getString as _getString, memory } from ${stringifyRequest(

View File

@ -1,5 +1,6 @@
const stringifyRequest = require("loader-utils").stringifyRequest;
/** @type {import("../../../../").PitchLoaderDefinitionFunction} */
module.exports.pitch = function (remainingRequest) {
return `
import { getString as _getString, memory } from ${stringifyRequest(

View File

@ -1,4 +1,6 @@
const path = require("path");
/** @type {import("../../../../").LoaderDefinition} */
module.exports = function (source) {
this.addDependency(path.resolve(__dirname, "node_modules/package/extra.js"));
this.addDependency(path.resolve(__dirname, "extra.js"));

View File

@ -1,7 +1,11 @@
/** @type {import("../../../../").LoaderDefinition} */
module.exports = function (content) {
return "module.exports = " + JSON.stringify({
return (
"module.exports = " +
JSON.stringify({
resourceQuery: this.resourceQuery,
query: this.query,
prev: content.replace(/\r\n?/g, "\n")
});
})
);
};

View File

@ -1,3 +1,4 @@
/** @type {import("../../../../").LoaderDefinition} */
module.exports = function (source) {
this.addDependency("loader.js");
this.addDependency("../**/dir/*.js");

View File

@ -1,3 +1,4 @@
/** @type {import("../../../../").LoaderDefinition} */
module.exports = function (source) {
return source;
};

View File

@ -1,3 +1,4 @@
/** @type {import("../../../../").LoaderDefinition<string>} */
module.exports = function () {
const { name, expect, usedExports } = JSON.parse(this.query.slice(1));
return [

View File

@ -1,3 +1,4 @@
/** @type {import("../../../../").LoaderDefinition<string>} */
module.exports = function () {
const usedExports = JSON.parse(this.query.slice(1));
return [

View File

@ -1,3 +1,4 @@
/** @type {import("../../../../").LoaderDefinition<{ value: any }>} */
module.exports = function (source) {
const options = this.getOptions();
return `${source}

View File

@ -1,3 +1,4 @@
/** @type {import("../../../../").PitchLoaderDefinitionFunction} */
exports.pitch = async function (remaining) {
const result = await this.importModule(
this.resourcePath + ".webpack[javascript/auto]" + "!=!" + remaining,

View File

@ -1,3 +1,6 @@
/** @type {import("../../../../").PitchLoaderDefinitionFunction} */
module.exports.pitch = function (remainingRequest) {
return "module.exports = require(" + JSON.stringify("!!" + remainingRequest) + ");";
return (
"module.exports = require(" + JSON.stringify("!!" + remainingRequest) + ");"
);
};

View File

@ -1,3 +1,6 @@
/** @type {import("../../../../").LoaderDefinition<{ f(): any }>} */
module.exports = function (source) {
if (typeof this.query === "string")
throw new Error("query must be an object");
return "module.exports = " + JSON.stringify(this.query.f());
};

View File

@ -1,3 +1,4 @@
/** @type {import("../../../../").LoaderDefinition}} */
module.exports = function () {
return `module.exports = ${JSON.stringify(!!this.hot)};`;
}
};

View File

@ -1,3 +1,4 @@
/** @type {import("../../../../").LoaderDefinition} */
module.exports = function (source) {
return `module.exports = "${this.mode}";`;
};

View File

@ -1,3 +1,4 @@
/** @type {import("../../../../").LoaderDefinition} */
module.exports = function (source) {
return `module.exports = "${this.mode}";`;
};

View File

@ -1,3 +1,4 @@
/** @type {import("../../../../").LoaderDefinition} */
module.exports = function (source) {
return `module.exports = "${this.mode}";`;
};

View File

@ -1,3 +1,4 @@
/** @type {import("../../../../").LoaderDefinition} */
module.exports = function (source) {
return `module.exports = "${this.mode}";`;
};

View File

@ -1,5 +1,6 @@
const schema = require("./loader-1.options");
const schema = require("./loader-1.options.json");
/** @type {import("../../../../").LoaderDefinition} */
module.exports = function () {
const options = this.getOptions(schema);

View File

@ -1,5 +1,6 @@
const schema = require("./loader-2.options");
const schema = require("./loader-2.options.json");
/** @type {import("../../../../").LoaderDefinition} */
module.exports = function () {
const options = this.getOptions(schema);

View File

@ -1,9 +1,10 @@
/** @type {import("../../../../").LoaderDefinition} */
module.exports = function () {
const options = this.getOptions();
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

@ -1,3 +1,4 @@
/** @type {import("../../../../").LoaderDefinition} */
module.exports = function (source) {
return source + "module.exports += \" loader1\";\n";
return source + 'module.exports += " loader1";\n';
};

View File

@ -1,3 +1,4 @@
/** @type {import("../../../../").LoaderDefinition} */
module.exports = function (source) {
return source + "module.exports += \" loader2\";\n";
return source + 'module.exports += " loader2";\n';
};

View File

@ -1,3 +1,4 @@
/** @type {import("../../../../").LoaderDefinition} */
module.exports = function (source) {
return source + "module.exports += \" loader3\";\n";
return source + 'module.exports += " loader3";\n';
};

View File

@ -1,3 +1,6 @@
/** @type {import("../../../../").PitchLoaderDefinitionFunction} */
module.exports.pitch = function (remainingRequest) {
return "module.exports = require(" + JSON.stringify("!!" + remainingRequest) + ");";
return (
"module.exports = require(" + JSON.stringify("!!" + remainingRequest) + ");"
);
};

View File

@ -1,3 +1,6 @@
/** @type {import("../../../../").LoaderDefinition<{ f(): any }>} */
module.exports = function (source) {
if (typeof this.query === "string")
throw new Error("query must be an object");
return "module.exports = " + JSON.stringify(this.query.f());
};

View File

@ -1,3 +1,4 @@
/** @type {import("../../../../").LoaderDefinition} */
module.exports = function (src) {
return `module.exports = "loader-a" + module.id`;
};

View File

@ -1,3 +1,4 @@
/** @type {import("../../../../").LoaderDefinition} */
module.exports = function (src) {
return `module.exports = "loader-b" + module.id`;
};

View File

@ -1,3 +1,4 @@
/** @type {import("../../../../").LoaderDefinition} */
module.exports = function () {
var str = "export default Promise.all([\n";
for (var i = 0; i < 6; i++) {

View File

@ -1,7 +1,8 @@
/** @type {import("../../../../").LoaderDefinition} */
module.exports = function () {
var str = "";
for (var i = 0; i < 1000; i++) {
str += `export var a${i} = ${i};\n`;
}
return str;
}
};

View File

@ -1,9 +1,10 @@
/** @type {import("../../../../").LoaderDefinition} */
module.exports = function () {
var str = "import * as i from \"./file.loader.js!\";\n";
var str = 'import * as i from "./file.loader.js!";\n';
str += "var sum = 0;\n";
for (var i = 0; i < 1000; i++) {
str += `sum += i.a${i};\n`;
}
str += "export default sum;\n";
return str;
}
};

View File

@ -1,6 +1,10 @@
/** @type {import("../../../../").LoaderDefinition<{}, { minimize: boolean, jsfile: boolean }>} */
module.exports = function () {
return "module.exports = " + JSON.stringify({
return (
"module.exports = " +
JSON.stringify({
minimize: this.minimize,
jsfile: this.jsfile
});
})
);
};

View File

@ -1,3 +1,4 @@
/** @type {import("../../../../").LoaderDefinition} */
module.exports = function () {
const callback = this.async();
let finished = false;

View File

@ -1,3 +1,4 @@
/** @type {import("../../../../").LoaderDefinition<{}, { shouldReplace: boolean }>} */
module.exports = function (source) {
if (this.shouldReplace) {
this._module.buildInfo._isReplaced = true;

View File

@ -1,3 +1,4 @@
/** @type {import("../../../../").LoaderDefinition<{}, { shouldReplace: boolean }>} */
module.exports = function (source) {
if (this.shouldReplace) {
this._module.buildInfo._isReplaced = true;

View File

@ -1,3 +1,4 @@
/** @type {import("../../../../").LoaderDefinition} */
module.exports = function (source) {
return source;
};

View File

@ -1,3 +1,4 @@
/** @type {import("../../../../").LoaderDefinition} */
module.exports = async function () {
const defaultResolve = this.getResolve({});
const overrideResolve = this.getResolve({
@ -20,6 +21,7 @@ module.exports = async function () {
expect(await defaultResolve(undefined, "package2").catch(e => "ok")).toBe(
"ok"
);
// @ts-expect-error undefined should not be a valid type
expect(await defaultResolve(undefined).catch(e => "ok")).toBe("ok");
return `
export { default as a } from ${JSON.stringify(resolved1)};

View File

@ -1,3 +1,4 @@
/** @type {import("../../../../").LoaderDefinition<{ get(): string }>} */
module.exports = function (source) {
var query = this.query;
if (typeof query === "object" && typeof query.get === "function") {
@ -5,4 +6,3 @@ module.exports = function(source) {
}
return source + "\nmodule.exports.push(" + JSON.stringify(query) + ");";
};

View File

@ -1,4 +1,4 @@
/** @type {import("../../../../").LoaderDefinition} */
module.exports = function (source) {
return "module.exports = " + JSON.stringify("loader matched");
};

View File

@ -1,3 +1,4 @@
/** @type {import("../../../../").LoaderDefinition<{ get(): string }>} */
module.exports = function (source) {
var query = this.query;
if (typeof query === "object" && typeof query.get === "function") {
@ -5,4 +6,3 @@ module.exports = function(source) {
}
return source + "\nmodule.exports.push(" + JSON.stringify(query) + ");";
};

View File

@ -1,3 +1,4 @@
/** @type {import("../../../../").LoaderDefinition<{ get(): string }>} */
module.exports = function (source) {
var query = this.query;
if (typeof query === "object" && typeof query.get === "function") {
@ -5,4 +6,3 @@ module.exports = function(source) {
}
return source + "\nmodule.exports.push(" + JSON.stringify(query) + ");";
};

View File

@ -1,3 +1,4 @@
/** @type {import("../../../../").LoaderDefinition<{ get(): string }>} */
module.exports = function (source) {
var query = this.query;
if (typeof query === "object" && typeof query.get === "function") {
@ -5,4 +6,3 @@ module.exports = function(source) {
}
return source + "\nmodule.exports.push(" + JSON.stringify(query) + ");";
};

View File

@ -1,3 +1,4 @@
/** @type {import("../../../../").LoaderDefinition<{ get(): string }>} */
module.exports = function (source) {
var query = this.query;
if (typeof query === "object" && typeof query.get === "function") {
@ -5,4 +6,3 @@ module.exports = function(source) {
}
return source + "\nmodule.exports.push(" + JSON.stringify(query) + ");";
};

View File

@ -1,3 +1,4 @@
/** @type {import("../../../../").LoaderDefinition<{ get(): string }>} */
module.exports = function (source) {
var query = this.query;
if (typeof query === "object" && typeof query.get === "function") {
@ -5,4 +6,3 @@ module.exports = function(source) {
}
return source + "\nmodule.exports.push(" + JSON.stringify(query) + ");";
};

View File

@ -1,4 +1,5 @@
const path = require("path");
/** @type {import("../../../../").LoaderDefinition} */
module.exports = function () {
this.callback(null, "module.exports = 'ok';", {
version: 3,
@ -6,6 +7,7 @@ module.exports = function () {
sourceRoot: path.join(__dirname, "folder"),
sources: ["test1.txt"],
sourcesContent: ["Test"],
names: [],
mappings: "AAAA"
});
};

View File

@ -1,10 +1,12 @@
const path = require("path");
/** @type {import("../../../../").LoaderDefinition} */
module.exports = function () {
this.callback(null, "module.exports = 'ok';", {
version: 3,
file: "/should/be/removed",
sources: [path.join(__dirname, "folder", "test5.txt")],
sourcesContent: ["Test"],
names: [],
mappings: "AAAA"
});
};

View File

@ -1,9 +1,11 @@
/** @type {import("../../../../").LoaderDefinition} */
module.exports = function () {
this.callback(null, "module.exports = 'ok';", {
version: 3,
file: "/should/be/removed",
sources: ["webpack://./folder/test6.txt"],
sourcesContent: ["Test"],
names: [],
mappings: "AAAA"
});
};

View File

@ -1,4 +1,5 @@
const path = require("path");
/** @type {import("../../../../").LoaderDefinition} */
module.exports = function () {
this.callback(null, "module.exports = 'ok';", {
version: 3,
@ -6,6 +7,7 @@ module.exports = function() {
sourceRoot: path.join(__dirname, "folder") + "/",
sources: ["/test4.txt"],
sourcesContent: ["Test"],
names: [],
mappings: "AAAA"
});
};

View File

@ -1,4 +1,5 @@
const path = require("path");
/** @type {import("../../../../").LoaderDefinition} */
module.exports = function () {
this.callback(null, "module.exports = 'ok';", {
version: 3,
@ -6,6 +7,7 @@ module.exports = function() {
sourceRoot: path.join(__dirname, "folder") + "/",
sources: ["test3.txt"],
sourcesContent: ["Test"],
names: [],
mappings: "AAAA"
});
};

View File

@ -1,4 +1,5 @@
const path = require("path");
/** @type {import("../../../../").LoaderDefinition} */
module.exports = function () {
this.callback(null, "module.exports = 'ok';", {
version: 3,
@ -6,6 +7,7 @@ module.exports = function() {
sourceRoot: path.join(__dirname, "folder"),
sources: ["/test2.txt"],
sourcesContent: ["Test"],
names: [],
mappings: "AAAA"
});
};

View File

@ -1,4 +1,5 @@
const path = require("path");
/** @type {import("../../../../").LoaderDefinition} */
module.exports = function () {
this.callback(null, "module.exports = 'ok';", {
version: 3,
@ -6,6 +7,7 @@ module.exports = function() {
sourceRoot: path.join(__dirname, "folder"),
sources: ["test1.txt"],
sourcesContent: ["Test"],
names: [],
mappings: "AAAA"
});
};

View File

@ -1,5 +1,6 @@
let counter = 0;
/** @type {import("../../../../").LoaderDefinition} */
module.exports = function () {
return `module.exports = ${counter++};`;
};

View File

@ -1,3 +1,4 @@
/** @type {import("../../../../").LoaderDefinition} */
module.exports = function (source) {
var cb = this.async();
setTimeout(function () {

View File

@ -1,3 +1,4 @@
/** @type {import("../../../../").LoaderDefinition} */
module.exports = function (source) {
return source + "// some comment";
};

View File

@ -1,3 +1,4 @@
/** @type {import("../../../../").LoaderDefinition} */
module.exports = function (source) {
const callback = this.async();
const error = new Error("this is a callback error");

View File

@ -1,3 +1,4 @@
/** @type {import("../../../../").LoaderDefinition} */
module.exports = function (source) {
this.emitWarning(new Error("this is a warning"));
this.emitError(new Error("this is an error"));

View File

@ -1,3 +1,4 @@
/** @type {import("../../../../").LoaderDefinition} */
module.exports = function (source) {
return source;
};

View File

@ -1,3 +1,4 @@
/** @type {import("../../../../").LoaderDefinition} */
module.exports = function (source) {
const empty = null;
const emptyError = new Error();

View File

@ -1,2 +1,2 @@
module.exports = function(){
}
/** @type {import("../../../../").LoaderDefinition} */
module.exports = function () {};

View File

@ -1,3 +1,4 @@
/** @type {import("../../../../").LoaderDefinition} */
module.exports = function (source) {
throw new Error("this is a thrown error");
};

View File

@ -5,6 +5,7 @@ const {
const compilerCache = new WeakMap();
/** @type {import("../../../../").LoaderDefinition} */
module.exports = function (source) {
let childCompiler = compilerCache.get(this._compiler);
if (childCompiler === undefined) {

View File

@ -1,3 +1,4 @@
/** @type {import("../../").LoaderDefinition<{}, { updateIndex: number }>} */
module.exports = function (source) {
var idx = this.updateIndex;
var items = source.split(/---+\r?\n/g);

View File

@ -1,3 +1,4 @@
/** @type {import("../../../../").PitchLoaderDefinitionFunction} */
exports.pitch = async function (remaining) {
const result = await this.importModule(
this.resourcePath + ".webpack[javascript/auto]" + "!=!" + remaining

View File

@ -1,5 +1,5 @@
/** @type {import("../../../../").LoaderDefinition} */
module.exports = function (source) {
if(source.indexOf("error") >= 0)
throw new Error(source.trim());
if (source.indexOf("error") >= 0) throw new Error(source.trim());
return source;
};

View File

@ -16,15 +16,23 @@ const getCache = (associate, path) => {
return c;
};
/** @type {import("../../../../../").LoaderDefinition} */
module.exports = function (source) {
if(map.has(currentWatchStepModule.step)) return map.get(currentWatchStepModule.step);
if (map.has(currentWatchStepModule.step))
return map.get(currentWatchStepModule.step);
const compilationCache = getCache(this._compiler.root, this._compilation.compilerPath);
const compilationCache = getCache(
this._compiler.root,
this._compilation.compilerPath
);
compilationCache.counter++;
var childCompiler = this._compilation.createChildCompiler("my-compiler " + source.trim(), {
var childCompiler = this._compilation.createChildCompiler(
"my-compiler " + source.trim(),
{
filename: "test"
});
}
);
var callback = this.async();
childCompiler.runAsChild((err, entries, compilation) => {
if (err) return callback(err);

View File

@ -1,3 +1,4 @@
/** @type {import("../../../../../").PitchLoaderDefinitionFunction} */
exports.pitch = async function (remaining) {
const result = await this.importModule(
`${this.resourcePath}.webpack[javascript/auto]!=!${remaining}`

View File

@ -1,6 +1,7 @@
const path = require("path");
const directory = path.resolve(__dirname, "directory");
/** @type {import("../../../../../").LoaderDefinition} */
module.exports = function () {
this.addContextDependency(directory);
const callback = this.async();

View File

@ -1,7 +1,9 @@
/** @type {import("../../../../../").LoaderDefinition} */
module.exports = function () {
const callback = this.async();
this.resolve(this.context, "./file", (err, file) => {
if (err) return callback(err);
if (!file) return callback(new Error("Resolving failed"));
this.fs.readFile(file, (err, result) => {
if (err) return callback(err);
callback(

View File

@ -1,3 +1,4 @@
/** @type {import("../../../../../").LoaderDefinition} */
module.exports = function (source) {
this.emitWarning(new Error(source.trim()));
return "";

View File

@ -12,5 +12,12 @@
"types": ["node", "jest"],
"esModuleInterop": true
},
"include": ["test/**/webpack.config.js", "declarations.test.d.ts"]
"include": [
"test/**/webpack.config.js",
"test/cases/**/*loader*.js",
"test/watchCases/**/*loader*.js",
"test/configCases/**/*loader*.js",
"test/hotCases/**/*loader*.js",
"declarations.test.d.ts"
]
}