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 @@
module.exports = function() { /** @type {import("../../../../").LoaderDefinition} */
module.exports = function () {
var err = new Error("Message"); var err = new Error("Message");
err.stack = "Stack"; err.stack = "Stack";
//@ts-expect-error hideStack is not a property on normal errors
err.hideStack = true; err.hideStack = true;
throw err; throw err;
}; };

View File

@ -1,4 +1,5 @@
exports.default = function(source) { /** @type {import("../../../../").LoaderDefinitionFunction} */
exports.default = function (source) {
const ref = JSON.parse(source); const ref = JSON.parse(source);
const callback = this.async(); const callback = this.async();
this.loadModule("../loader!" + ref, (err, source, sourceMap, module) => { this.loadModule("../loader!" + ref, (err, source, sourceMap, module) => {

View File

@ -1,4 +1,5 @@
exports.default = function(source) { /** @type {import("../../../../").LoaderDefinitionFunction} */
exports.default = function (source) {
const callback = this.async(); const callback = this.async();
const ref = JSON.parse(source); const ref = JSON.parse(source);
this.loadModule("./error-loader!" + ref, (err, source, sourceMap, module) => { this.loadModule("./error-loader!" + ref, (err, source, sourceMap, module) => {

View File

@ -1,4 +1,6 @@
module.exports = function(source) { /** @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)); this.emitError(this.query.substr(1));
return source; return source;
} };

View File

@ -1,4 +1,6 @@
module.exports = function(source) { /** @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)); this.emitWarning(this.query.substr(1));
return source; return source;
} };

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,5 +1,6 @@
const path = require("path"); const path = require("path");
module.exports = function() { /** @type {import("../../../../").LoaderDefinition} */
module.exports = function () {
const resolve1 = this.getResolve(); const resolve1 = this.getResolve();
const resolve2 = this.getResolve({ const resolve2 = this.getResolve({
extensions: [".xyz", ".js"] extensions: [".xyz", ".js"]

View File

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

View File

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

View File

@ -3,7 +3,8 @@
const acorn = require("acorn"); const acorn = require("acorn");
const acornParser = acorn.Parser; const acornParser = acorn.Parser;
module.exports = function(source) { /** @type {import("../../../../").LoaderDefinition} */
module.exports = function (source) {
const comments = []; const comments = [];
const ast = acornParser.parse(source, { const ast = acornParser.parse(source, {
@ -15,9 +16,12 @@ module.exports = function(source) {
}); });
// change something to test if it's really used // 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].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; ast.comments = comments;
this.callback(null, source, null, { this.callback(null, source, null, {
webpackAST: ast webpackAST: ast

View File

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

View File

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

View File

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

View File

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

View File

@ -1,7 +1,11 @@
module.exports = function(content) { /** @type {import("../../../../").LoaderDefinition} */
return "module.exports = " + JSON.stringify({ module.exports = function (content) {
resourceQuery: this.resourceQuery, return (
query: this.query, "module.exports = " +
prev: content.replace(/\r\n?/g, "\n") 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) { module.exports = function (source) {
this.addDependency("loader.js"); this.addDependency("loader.js");
this.addDependency("../**/dir/*.js"); this.addDependency("../**/dir/*.js");

View File

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

View File

@ -1,4 +1,5 @@
module.exports = function() { /** @type {import("../../../../").LoaderDefinition<string>} */
module.exports = function () {
const { name, expect, usedExports } = JSON.parse(this.query.slice(1)); const { name, expect, usedExports } = JSON.parse(this.query.slice(1));
return [ return [
`if (Math.random() < 0) require(${JSON.stringify( `if (Math.random() < 0) require(${JSON.stringify(

View File

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

View File

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

View File

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

View File

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

View File

@ -1,3 +1,6 @@
module.exports = function(source) { /** @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()); return "module.exports = " + JSON.stringify(this.query.f());
}; };

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

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

View File

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

View File

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

View File

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

View File

@ -1,3 +1,6 @@
module.exports = function(source) { /** @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()); return "module.exports = " + JSON.stringify(this.query.f());
}; };

View File

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

View File

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

View File

@ -1,7 +1,8 @@
module.exports = function() { /** @type {import("../../../../").LoaderDefinition} */
module.exports = function () {
var str = "export default Promise.all([\n"; var str = "export default Promise.all([\n";
for(var i = 0; i < 6; i++) { for (var i = 0; i < 6; i++) {
for(var j = 0; j < 2; j++) { for (var j = 0; j < 2; j++) {
str += `import("./reexport.loader.js!?${i}"),\n`; str += `import("./reexport.loader.js!?${i}"),\n`;
} }
} }

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -4,30 +4,38 @@ var cacheMap = new WeakMap();
const getCache = (associate, path) => { const getCache = (associate, path) => {
let o = cacheMap.get(associate); let o = cacheMap.get(associate);
if(o === undefined) { if (o === undefined) {
o = new Map(); o = new Map();
cacheMap.set(associate, o); cacheMap.set(associate, o);
} }
let c = o.get(path); let c = o.get(path);
if(c === undefined) { if (c === undefined) {
c = { counter: 0 }; c = { counter: 0 };
o.set(path, c); o.set(path, c);
} }
return c; return c;
}; };
module.exports = function(source) { /** @type {import("../../../../../").LoaderDefinition} */
if(map.has(currentWatchStepModule.step)) return map.get(currentWatchStepModule.step); module.exports = function (source) {
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++; compilationCache.counter++;
var childCompiler = this._compilation.createChildCompiler("my-compiler " + source.trim(), { var childCompiler = this._compilation.createChildCompiler(
filename: "test" "my-compiler " + source.trim(),
}); {
filename: "test"
}
);
var callback = this.async(); var callback = this.async();
childCompiler.runAsChild((err, entries, compilation) => { childCompiler.runAsChild((err, entries, compilation) => {
if(err) return callback(err); if (err) return callback(err);
const childCache = getCache(this._compiler.root, compilation.compilerPath); const childCache = getCache(this._compiler.root, compilation.compilerPath);
childCache.counter++; childCache.counter++;

View File

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

View File

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

View File

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

View File

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

View File

@ -12,5 +12,12 @@
"types": ["node", "jest"], "types": ["node", "jest"],
"esModuleInterop": true "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"
]
} }