|
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'; |
3 | 3 |
|
4 | 4 | export type TransformLoaderOptions = { |
5 | 5 | id: string; |
6 | 6 | getEnvironment: () => EnvironmentContext; |
7 | 7 | }; |
8 | 8 |
|
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 | + }; |
30 | 15 |
|
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) { |
48 | 18 | bypass(); |
49 | 19 | return; |
50 | 20 | } |
51 | 21 |
|
52 | | - if (typeof result === 'string') { |
53 | | - callback(null, result, map); |
| 22 | + const transform = this._compiler?.__rsbuildTransformer?.[transformId]; |
| 23 | + if (!transform) { |
| 24 | + bypass(); |
54 | 25 | return; |
55 | 26 | } |
56 | 27 |
|
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 | + } |
65 | 63 | } |
66 | | - } |
67 | | -} |
| 64 | + }; |
| 65 | + |
| 66 | +export default transformLoader; |
0 commit comments