Skip to content

Commit bb519b6

Browse files
chenjiahanCopilot
andauthored
fix(types): allow async loader to return void (#11511)
* fix(types): allow async loader to return void * Update packages/rspack/src/config/adapterRuleUse.ts Co-authored-by: Copilot <[email protected]> --------- Co-authored-by: Copilot <[email protected]>
1 parent e8b2dc3 commit bb519b6

File tree

4 files changed

+74
-12
lines changed

4 files changed

+74
-12
lines changed

packages/rspack/etc/core.api.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4323,14 +4323,14 @@ export interface LoaderContext<OptionsType = {}> {
43234323
// @public (undocumented)
43244324
type LoaderContextCallback = (err?: Error | null, content?: string | Buffer, sourceMap?: string | SourceMap, additionalData?: AdditionalData) => void;
43254325

4326-
// @public (undocumented)
4326+
// @public
43274327
export type LoaderDefinition<OptionsType = {}, ContextAdditions = {}> = LoaderDefinitionFunction<OptionsType, ContextAdditions> & {
43284328
raw?: false;
43294329
pitch?: PitchLoaderDefinitionFunction;
43304330
};
43314331

43324332
// @public (undocumented)
4333-
export type LoaderDefinitionFunction<OptionsType = {}, ContextAdditions = {}> = (this: LoaderContext<OptionsType> & ContextAdditions, content: string, sourceMap?: string | SourceMap, additionalData?: AdditionalData) => string | void | Buffer | Promise<string | Buffer>;
4333+
export type LoaderDefinitionFunction<OptionsType = {}, ContextAdditions = {}> = (this: LoaderContext<OptionsType> & ContextAdditions, content: string, sourceMap?: string | SourceMap, additionalData?: AdditionalData) => string | void | Buffer | Promise<string | Buffer | void>;
43344334

43354335
// @public (undocumented)
43364336
interface LoaderExperiments {
@@ -5565,7 +5565,7 @@ type Performance_2 = false | {
55655565
export { Performance_2 as Performance }
55665566

55675567
// @public (undocumented)
5568-
export type PitchLoaderDefinitionFunction<OptionsType = {}, ContextAdditions = {}> = (this: LoaderContext<OptionsType> & ContextAdditions, remainingRequest: string, previousRequest: string, data: object) => string | void | Buffer | Promise<string | Buffer>;
5568+
export type PitchLoaderDefinitionFunction<OptionsType = {}, ContextAdditions = {}> = (this: LoaderContext<OptionsType> & ContextAdditions, remainingRequest: string, previousRequest: string, data: object) => string | void | Buffer | Promise<string | Buffer | void>;
55695569

55705570
// @public (undocumented)
55715571
type Plugin_2 = RspackPluginInstance | RspackPluginFunction | WebpackPluginInstance | WebpackPluginFunction | Falsy;

packages/rspack/src/config/adapterRuleUse.ts

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ export type LoaderDefinitionFunction<
394394
content: string,
395395
sourceMap?: string | SourceMap,
396396
additionalData?: AdditionalData
397-
) => string | void | Buffer | Promise<string | Buffer>;
397+
) => string | void | Buffer | Promise<string | Buffer | void>;
398398

399399
export type PitchLoaderDefinitionFunction<
400400
OptionsType = {},
@@ -404,8 +404,32 @@ export type PitchLoaderDefinitionFunction<
404404
remainingRequest: string,
405405
previousRequest: string,
406406
data: object
407-
) => string | void | Buffer | Promise<string | Buffer>;
408-
407+
) => string | void | Buffer | Promise<string | Buffer | void>;
408+
409+
/**
410+
* Defines a loader for Rspack.
411+
* A loader is a transformer that converts various types of modules into Rspack
412+
* supported types. By using different kinds of loaders, you can extend Rspack to
413+
* process additional module types, including JSX, Markdown, Sass, Less, and more.
414+
*
415+
* @template OptionsType - The type of options that the loader accepts
416+
* @template ContextAdditions - Additional properties to add to the loader context
417+
*
418+
* @example
419+
* ```ts
420+
* import type { LoaderDefinition } from '@rspack/core';
421+
*
422+
* type MyLoaderOptions = {
423+
* foo: string;
424+
* };
425+
*
426+
* const myLoader: LoaderDefinition<MyLoaderOptions> = function(source) {
427+
* return someOperation(source);
428+
* };
429+
*
430+
* export default myLoader;
431+
* ```
432+
*/
409433
export type LoaderDefinition<
410434
OptionsType = {},
411435
ContextAdditions = {}

website/docs/en/api/loader-api/types.mdx

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,23 +92,42 @@ ESM loader and CommonJS loader have the same functionality, but use different mo
9292

9393
## Write with TypeScript
9494

95-
If you write Rspack loader using TypeScript, you can import `LoaderContext` to add types to the loader:
95+
If you write Rspack loader using TypeScript, you can use `LoaderDefinition` to provide complete type definitions for your loader.
9696

9797
```ts title="my-loader.ts"
98-
import type { LoaderContext } from '@rspack/core';
98+
import type { LoaderDefinition } from '@rspack/core';
9999

100100
// Declare the type of loader options
101101
type MyLoaderOptions = {
102102
foo: string;
103103
};
104104

105+
const myLoader: LoaderDefinition<MyLoaderOptions> = function (source) {
106+
const options = this.getOptions();
107+
console.log(options); // { foo: 'bar' }
108+
// Transform the source code
109+
return `// Processed by my-loader\n${source}`;
110+
};
111+
112+
export default myLoader;
113+
```
114+
115+
Alternatively, you can import `LoaderContext` to add types to the loader context:
116+
117+
```ts title="my-loader.ts"
118+
import type { LoaderContext } from '@rspack/core';
119+
120+
type MyLoaderOptions = {
121+
foo: string;
122+
};
123+
105124
export default function myLoader(
106125
this: LoaderContext<MyLoaderOptions>,
107126
source: string,
108127
) {
109128
const options = this.getOptions();
110129
console.log(options); // { foo: 'bar' }
111-
return source;
130+
return `// Processed by my-loader\n${source}`;
112131
}
113132
```
114133

website/docs/zh/api/loader-api/types.mdx

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,23 +92,42 @@ ESM loader 和 CommonJS loader 的功能完全相同,只是使用了不同的
9292

9393
## 使用 TypeScript 编写
9494

95-
如果你使用 TypeScript 来编写 Rspack loader,可以引入 `LoaderContext` 来为 loader 添加类型:
95+
如果你使用 TypeScript 编写 Rspack loader,可以使用 `LoaderDefinition` 为你的 loader 提供完整的类型定义。
9696

9797
```ts title="my-loader.ts"
98-
import type { LoaderContext } from '@rspack/core';
98+
import type { LoaderDefinition } from '@rspack/core';
9999

100100
// 声明 loader 选项的类型
101101
type MyLoaderOptions = {
102102
foo: string;
103103
};
104104

105+
const myLoader: LoaderDefinition<MyLoaderOptions> = function (source) {
106+
const options = this.getOptions();
107+
console.log(options); // { foo: 'bar' }
108+
// Transform the source code
109+
return `// Processed by my-loader\n${source}`;
110+
};
111+
112+
export default myLoader;
113+
```
114+
115+
或者,你也可以导入 `LoaderContext` 为 loader 上下文添加类型:
116+
117+
```ts title="my-loader.ts"
118+
import type { LoaderContext } from '@rspack/core';
119+
120+
type MyLoaderOptions = {
121+
foo: string;
122+
};
123+
105124
export default function myLoader(
106125
this: LoaderContext<MyLoaderOptions>,
107126
source: string,
108127
) {
109128
const options = this.getOptions();
110129
console.log(options); // { foo: 'bar' }
111-
return source;
130+
return `// Processed by my-loader\n${source}`;
112131
}
113132
```
114133

0 commit comments

Comments
 (0)