Skip to content

Commit

Permalink
feat: add typings about LoaderDefinition
Browse files Browse the repository at this point in the history
  • Loading branch information
Asuka109 committed Apr 24, 2023
1 parent e2647bb commit 10df027
Show file tree
Hide file tree
Showing 99 changed files with 149 additions and 113 deletions.
47 changes: 40 additions & 7 deletions packages/rspack/src/config/adapter-rule-use.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,21 @@ export interface LoaderObject {
normalExecuted: boolean;
}

export interface LoaderContext {
export interface LoaderContext<OptionsType = {}> {
version: 2;
resource: string;
resourcePath: string;
resourceQuery: string;
resourceFragment: string;
async(): (
err: Error | null,
content: string | Buffer,
err?: Error | null,
content?: string | Buffer,
sourceMap?: string | SourceMap,
additionalData?: AdditionalData
) => void;
callback(
err: Error | null,
content: string | Buffer,
err?: Error | null,
content?: string | Buffer,
sourceMap?: string | SourceMap,
additionalData?: AdditionalData
): void;
Expand Down Expand Up @@ -99,7 +99,7 @@ export interface LoaderContext {
loaders: LoaderObject[];
mode?: Mode;
hot?: boolean;
getOptions(schema?: any): unknown;
getOptions(schema?: any): OptionsType;
resolve(
context: string,
request: string,
Expand Down Expand Up @@ -136,7 +136,7 @@ export interface LoaderContext {
contextify: (context: string, request: string) => string;
createHash: (algorithm?: string) => Hash;
};
query: unknown;
query: string | OptionsType;
data: unknown;
_compiler: Compiler;
_compilation: Compiler["compilation"];
Expand All @@ -147,6 +147,7 @@ export interface LoaderContext {
* @internal
*/
__internal__isPitching: boolean;
// TODO: LoaderPluginLoaderContext
}

export interface LoaderResult {
Expand All @@ -160,6 +161,38 @@ export interface LoaderResult {
buildDependencies: string[];
}

export interface LoaderDefinitionFunction<
OptionsType = {},
ContextAdditions = {}
> {
(
this: LoaderContext<OptionsType> & ContextAdditions,
content: string,
sourceMap?: string | SourceMap,
additionalData?: AdditionalData
): string | void | Buffer | Promise<string | Buffer>;
}

export interface PitchLoaderDefinitionFunction<
OptionsType = {},
ContextAdditions = {}
> {
(
this: LoaderContext<OptionsType> & ContextAdditions,
remainingRequest: string,
previousRequest: string,
data: object
): string | void | Buffer | Promise<string | Buffer>;
}

export type LoaderDefinition<
OptionsType = {},
ContextAdditions = {}
> = LoaderDefinitionFunction<OptionsType, ContextAdditions> & {
raw?: false;
pitch?: PitchLoaderDefinitionFunction;
};

export function createRawModuleRuleUses(
uses: RuleSetUse,
options: ComposeJsUseOptions
Expand Down
6 changes: 5 additions & 1 deletion packages/rspack/src/config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ import * as oldBuiltins from "./builtins";

export type { BannerConditions, BannerCondition } from "./builtins";

export type { LoaderContext } from "./adapter-rule-use";
export type {
LoaderContext,
LoaderDefinitionFunction,
LoaderDefinition
} from "./adapter-rule-use";

export type Configuration = RspackOptions;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/** @type {import("../../../../").LoaderDefinition} */
/** @type {import("@rspack/core").LoaderDefinition} */
module.exports = function (source) {
return source + 'module.exports += " loader1";\n';
};
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/** @type {import("../../../../").LoaderDefinition} */
/** @type {import("@rspack/core").LoaderDefinition} */
module.exports = function (source) {
return source + 'module.exports += " loader2";\n';
};
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/** @type {import("../../../../").LoaderDefinition} */
/** @type {import("@rspack/core").LoaderDefinition} */
module.exports = function (source) {
return source + 'module.exports += " loader3";\n';
};
2 changes: 1 addition & 1 deletion packages/rspack/tests/fixtures/count-loader.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
let counter = 0;

/** @type {import("../../../../").LoaderDefinition} */
/** @type {import("@rspack/core").LoaderDefinition} */
module.exports = function () {
return `module.exports = ${counter++};`;
};
2 changes: 1 addition & 1 deletion packages/rspack/tests/fixtures/delay-loader.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/** @type {import("../../../../").LoaderDefinition} */
/** @type {import("@rspack/core").LoaderDefinition} */
module.exports = function (source) {
var cb = this.async();
setTimeout(function () {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/** @type {import("../../../../").LoaderDefinition} */
/** @type {import("@rspack/core").LoaderDefinition} */
module.exports = function (source) {
return source + "// some comment";
};
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/** @type {import("../../../../").LoaderDefinition} */
/** @type {import("@rspack/core").LoaderDefinition} */
module.exports = function (source) {
const callback = this.async();
const error = new Error("this is a callback error");
Expand Down
2 changes: 1 addition & 1 deletion packages/rspack/tests/fixtures/errors/emit-error-loader.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/** @type {import("../../../../").LoaderDefinition} */
/** @type {import("@rspack/core").LoaderDefinition} */
module.exports = function (source) {
this.emitWarning(new Error("this is a warning"));
this.emitError(new Error("this is an error"));
Expand Down
2 changes: 1 addition & 1 deletion packages/rspack/tests/fixtures/errors/identity-loader.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/** @type {import("../../../../").LoaderDefinition} */
/** @type {import("@rspack/core").LoaderDefinition} */
module.exports = function (source) {
return source;
};
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/** @type {import("../../../../").LoaderDefinition} */
/** @type {import("@rspack/core").LoaderDefinition} */
module.exports = function (source) {
const empty = null;
const emptyError = new Error();
Expand Down
4 changes: 2 additions & 2 deletions packages/rspack/tests/fixtures/errors/no-return-loader.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
/** @type {import("../../../../").LoaderDefinition} */
module.exports = function () {};
/** @type {import("@rspack/core").LoaderDefinition} */
module.exports = function () { };
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/** @type {import("../../../../").LoaderDefinition} */
/** @type {import("@rspack/core").LoaderDefinition} */
module.exports = function (source) {
throw new Error("this is a thrown error");
};
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/** @type {import("../../../../").LoaderDefinition<{ i: string }>} */
/** @type {import("@rspack/core").LoaderDefinition<{ i: string }>} */
module.exports = function () {
const options = this.getOptions();
const i = +options.i;
Expand Down
2 changes: 1 addition & 1 deletion webpack-test/cases/compile/error-hide-stack/loader.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/** @type {import("../../../../").LoaderDefinition} */
/** @type {import("@rspack/core").LoaderDefinition} */
module.exports = function () {
var err = new Error("Message");
err.stack = "Stack";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { promisify } = require("util");

/** @type {import("../../../../").LoaderDefinitionFunction} */
/** @type {import("@rspack/core").LoaderDefinitionFunction} */
exports.default = function (source) {
const content = JSON.parse(source);
// content is one reference or an array of references
Expand All @@ -13,11 +13,11 @@ exports.default = function (source) {
// bug from https://github.com/webpack/webpack/issues/14379 doesn't occur if
// they are loaded in parallel.
const loadedRefs = []
for(const ref of refs) {
for (const ref of refs) {
try {
const source = await loadModulePromise("../loader!" + ref);
loadedRefs.push([ref, JSON.parse(source)]);
} catch(err) {
} catch (err) {
loadedRefs.push([ref, `err: ${err && err.message}`]);
}
}
Expand Down
2 changes: 1 addition & 1 deletion webpack-test/cases/errors/load-module-cycle/loader.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/** @type {import("../../../../").LoaderDefinitionFunction} */
/** @type {import("@rspack/core").LoaderDefinitionFunction} */
exports.default = function (source) {
const ref = JSON.parse(source);
const callback = this.async();
Expand Down
4 changes: 2 additions & 2 deletions webpack-test/cases/errors/load-module-error/error-loader.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/** @type {import("../../../../types").LoaderDefinition} */
module.exports = function(source) {
/** @type {import("@rspack/core").LoaderDefinition} */
module.exports = function (source) {
const callback = this.async();
callback(new Error("err: abc"));
}
2 changes: 1 addition & 1 deletion webpack-test/cases/errors/load-module-error/loader.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/** @type {import("../../../../").LoaderDefinitionFunction} */
/** @type {import("@rspack/core").LoaderDefinitionFunction} */
exports.default = function (source) {
const callback = this.async();
const ref = JSON.parse(source);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/** @type {import("../../../../").LoaderDefinition<string>} */
/** @type {import("@rspack/core").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.slice(1));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/** @type {import("../../../../").LoaderDefinition<string>} */
/** @type {import("@rspack/core").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.slice(1));
Expand Down
2 changes: 1 addition & 1 deletion webpack-test/cases/loaders/async/loaders/asyncloader.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/** @type {import("../../../../../").LoaderDefinition} */
/** @type {import("@rspack/core").LoaderDefinition} */
module.exports = function (content) {
var cb = this.async();
if (!cb) throw new Error("Loader should allow async mode");
Expand Down
2 changes: 1 addition & 1 deletion webpack-test/cases/loaders/async/loaders/syncloader.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/** @type {import("../../../../../").LoaderDefinition} */
/** @type {import("@rspack/core").LoaderDefinition} */
module.exports = function (content) {
return content;
};
2 changes: 1 addition & 1 deletion webpack-test/cases/loaders/emit-file/loader.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/** @type {import("../../../../").LoaderDefinition} */
/** @type {import("@rspack/core").LoaderDefinition} */
module.exports = function (content) {
this.emitFile("extra-file.js", content);
return "";
Expand Down
5 changes: 2 additions & 3 deletions webpack-test/cases/loaders/import-module/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const path = require("path");

/** @type {import("../../../../").LoaderDefinition} */
/** @type {import("@rspack/core").LoaderDefinition} */
module.exports = function () {
const callback = this.async();
this.importModule(
Expand All @@ -16,8 +16,7 @@ module.exports = function () {

callback(
null,
`module.exports = ${
exports.asset ? JSON.stringify(exports.asset) : undefined
`module.exports = ${exports.asset ? JSON.stringify(exports.asset) : undefined
}`
);
}
Expand Down
2 changes: 1 addition & 1 deletion webpack-test/cases/loaders/issue-10725/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const { getRemainingRequest, stringifyRequest } = require("loader-utils");

const loaderPath = require.resolve("./loader");

/** @type {import("../../../../").LoaderDefinition} */
/** @type {import("@rspack/core").LoaderDefinition} */
module.exports = function () {
if (this.query === "?load") {
return `
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/** @type {import("../../../../").LoaderDefinition} */
/** @type {import("@rspack/core").LoaderDefinition} */
module.exports = function (content) {
return content.split("").reverse().join("");
};
2 changes: 1 addition & 1 deletion webpack-test/cases/loaders/query/loaders/queryloader.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/** @type {import("../../../../../").LoaderDefinition} */
/** @type {import("@rspack/core").LoaderDefinition} */
module.exports = function (content) {
return (
"module.exports = " +
Expand Down
2 changes: 1 addition & 1 deletion webpack-test/cases/loaders/resolve/loader.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const path = require("path");
/** @type {import("../../../../").LoaderDefinition} */
/** @type {import("@rspack/core").LoaderDefinition} */
module.exports = function () {
const resolve1 = this.getResolve();
const resolve2 = this.getResolve({
Expand Down
2 changes: 1 addition & 1 deletion webpack-test/cases/loaders/utils/loader.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/** @type {import("../../../../").LoaderDefinition} */
/** @type {import("@rspack/core").LoaderDefinition} */
module.exports = function () {
return `module.exports = {
request1: ${JSON.stringify(
Expand Down
2 changes: 1 addition & 1 deletion webpack-test/cases/parsing/context/loaders/queryloader.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/** @type {import("../../../../../").LoaderDefinition} */
/** @type {import("@rspack/core").LoaderDefinition} */
module.exports = function (content) {
return (
"module.exports = " +
Expand Down
4 changes: 2 additions & 2 deletions webpack-test/cases/parsing/precreated-ast/ast-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const acorn = require("acorn");
const acornParser = acorn.Parser;

/** @type {import("../../../../").LoaderDefinition} */
/** @type {import("@rspack/core").LoaderDefinition} */
module.exports = function (source) {
const comments = [];

Expand All @@ -23,7 +23,7 @@ module.exports = function (source) {

//@ts-ignore
ast.comments = comments;
this.callback(null, source, null, {
this.callback(null, source, undefined, {
webpackAST: ast
});
};
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/** @type {import("../../../../../").LoaderDefinition} */
/** @type {import("@rspack/core").LoaderDefinition} */
module.exports = function (content) {
return (
"module.exports = " +
Expand Down
2 changes: 1 addition & 1 deletion webpack-test/configCases/asset-modules/data-url/loader.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/** @type {import("../../../../").LoaderDefinition<{ f(): any }>} */
/** @type {import("@rspack/core").LoaderDefinition<{ f(): any }>} */
module.exports = function (source) {
return `${source}, World!`;
};
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const path = require("path");

/** @type {import("../../../../").LoaderDefinition} */
/** @type {import("@rspack/core").LoaderDefinition} */
module.exports = function (source) {
this.addDependency(path.resolve(__dirname, "node_modules/package/extra.js"));
this.addDependency(path.resolve(__dirname, "extra.js"));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const path = require("path");

/** @type {import("../../../../").LoaderDefinition} */
/** @type {import("@rspack/core").LoaderDefinition} */
module.exports = function (source) {
this.addDependency(path.resolve(__dirname, "node_modules/package/extra.js"));
this.addDependency(path.resolve(__dirname, "extra.js"));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/** @type {import("../../../../").LoaderDefinitionFunction} */
/** @type {import("@rspack/core").LoaderDefinitionFunction} */
module.exports = function () {
const callback = this.async();
this.importModule("./module1", { baseUri: "webpack://" }, (err, exports) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/** @type {import("../../../../").LoaderDefinition} */
/** @type {import("@rspack/core").LoaderDefinition} */
module.exports = function (content) {
return (
"module.exports = " +
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/** @type {import("../../../../").LoaderDefinition} */
/** @type {import("@rspack/core").LoaderDefinition} */
module.exports = function (source) {
this.addDependency("loader.js");
this.addDependency("../**/dir/*.js");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/** @type {import("../../../../").LoaderDefinition} */
/** @type {import("@rspack/core").LoaderDefinition} */
module.exports = function (source) {
return source;
};
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/** @type {import("../../../../").LoaderDefinition<string>} */
/** @type {import("@rspack/core").LoaderDefinition<string>} */
module.exports = function () {
const { name, expect, usedExports } = JSON.parse(this.query.slice(1));
return [
Expand Down
Loading

0 comments on commit 10df027

Please sign in to comment.