Skip to content

Commit 25d4a27

Browse files
authored
refactor(types): use LoaderDefinition to declare loader types (#5975)
1 parent e351fc5 commit 25d4a27

File tree

3 files changed

+58
-60
lines changed

3 files changed

+58
-60
lines changed

packages/core/src/loader/ignoreCssLoader.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import type { LoaderContext } from '@rspack/core';
1+
import type { LoaderDefinition } from '@rspack/core';
22

3-
export default function (this: LoaderContext<unknown>, source: string): string {
3+
const ignoreCssLoader: LoaderDefinition = function (source) {
44
this?.cacheable(true);
55

66
// if the source code include '___CSS_LOADER_EXPORT___'
@@ -12,4 +12,6 @@ export default function (this: LoaderContext<unknown>, source: string): string {
1212

1313
// Preserve CSS Modules export for SSR.
1414
return source;
15-
}
15+
};
16+
17+
export default ignoreCssLoader;
Lines changed: 51 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,66 @@
1-
import type { LoaderContext } from '@rspack/core';
2-
import type { EnvironmentContext, Rspack } from '../types';
1+
import type { LoaderDefinition } from '@rspack/core';
2+
import type { EnvironmentContext } from '../types';
33

44
export type TransformLoaderOptions = {
55
id: string;
66
getEnvironment: () => EnvironmentContext;
77
};
88

9-
export default async function transform(
10-
this: LoaderContext<TransformLoaderOptions>,
11-
source: string,
12-
map?: string | Rspack.sources.RawSourceMap,
13-
): Promise<void> {
14-
const callback = this.async();
15-
const bypass = () => {
16-
callback(null, source, map);
17-
};
18-
19-
const { id: transformId, getEnvironment } = this.getOptions();
20-
if (!transformId) {
21-
bypass();
22-
return;
23-
}
24-
25-
const transform = this._compiler?.__rsbuildTransformer?.[transformId];
26-
if (!transform) {
27-
bypass();
28-
return;
29-
}
9+
const transformLoader: LoaderDefinition<TransformLoaderOptions> =
10+
async function transform(source, map): Promise<void> {
11+
const callback = this.async();
12+
const bypass = () => {
13+
callback(null, source, map);
14+
};
3015

31-
try {
32-
const result = await transform({
33-
code: source,
34-
context: this.context,
35-
resource: this.resource,
36-
resourcePath: this.resourcePath,
37-
resourceQuery: this.resourceQuery,
38-
environment: getEnvironment(),
39-
addDependency: this.addDependency.bind(this),
40-
addMissingDependency: this.addMissingDependency.bind(this),
41-
addContextDependency: this.addContextDependency.bind(this),
42-
emitFile: this.emitFile.bind(this),
43-
importModule: this.importModule.bind(this),
44-
resolve: this.resolve.bind(this),
45-
});
46-
47-
if (result === null || result === undefined) {
16+
const { id: transformId, getEnvironment } = this.getOptions();
17+
if (!transformId) {
4818
bypass();
4919
return;
5020
}
5121

52-
if (typeof result === 'string') {
53-
callback(null, result, map);
22+
const transform = this._compiler?.__rsbuildTransformer?.[transformId];
23+
if (!transform) {
24+
bypass();
5425
return;
5526
}
5627

57-
const useMap = map !== undefined && map !== null;
58-
const finalMap = result.map ?? map;
59-
callback(null, result.code, useMap ? finalMap : undefined);
60-
} catch (error) {
61-
if (error instanceof Error) {
62-
callback(error);
63-
} else {
64-
callback(new Error(String(error)));
28+
try {
29+
const result = await transform({
30+
code: source,
31+
context: this.context,
32+
resource: this.resource,
33+
resourcePath: this.resourcePath,
34+
resourceQuery: this.resourceQuery,
35+
environment: getEnvironment(),
36+
addDependency: this.addDependency.bind(this),
37+
addMissingDependency: this.addMissingDependency.bind(this),
38+
addContextDependency: this.addContextDependency.bind(this),
39+
emitFile: this.emitFile.bind(this),
40+
importModule: this.importModule.bind(this),
41+
resolve: this.resolve.bind(this),
42+
});
43+
44+
if (result === null || result === undefined) {
45+
bypass();
46+
return;
47+
}
48+
49+
if (typeof result === 'string') {
50+
callback(null, result, map);
51+
return;
52+
}
53+
54+
const useMap = map !== undefined && map !== null;
55+
const finalMap = result.map ?? map;
56+
callback(null, result.code, useMap ? finalMap : undefined);
57+
} catch (error) {
58+
if (error instanceof Error) {
59+
callback(error);
60+
} else {
61+
callback(new Error(String(error)));
62+
}
6563
}
66-
}
67-
}
64+
};
65+
66+
export default transformLoader;

packages/plugin-svgr/src/loader.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,7 @@ const transformSvg = callbackify(
1818
transform(contents, config, state),
1919
);
2020

21-
function svgrLoader(
22-
this: Rspack.LoaderContext<Config>,
23-
contents: string,
24-
): void {
21+
const svgrLoader: Rspack.LoaderDefinition<Config> = function (contents): void {
2522
this?.cacheable();
2623

2724
const callback = this.async();
@@ -60,6 +57,6 @@ function svgrLoader(
6057
});
6158
});
6259
}
63-
}
60+
};
6461

6562
export default svgrLoader;

0 commit comments

Comments
 (0)