@rspack/core 1.2.5 → 1.2.7-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,4 @@
1
- import type { JsAssetInfo, RawModuleRuleUse, RawOptions } from "@rspack/binding";
1
+ import type { AssetInfo, RawModuleRuleUse, RawOptions } from "@rspack/binding";
2
2
  import type { ResolveRequest } from "../../compiled/enhanced-resolve";
3
3
  import type { Compilation } from "../Compilation";
4
4
  import type { Compiler } from "../Compiler";
@@ -73,21 +73,83 @@ export interface ImportModuleOptions {
73
73
  baseUri?: string;
74
74
  }
75
75
  export interface LoaderContext<OptionsType = {}> {
76
+ /**
77
+ * The version number of the loader API. Currently 2.
78
+ * This is useful for providing backwards compatibility. Using the version you can specify
79
+ * custom logic or fallbacks for breaking changes.
80
+ */
76
81
  version: 2;
82
+ /**
83
+ * The path string of the current module.
84
+ * @example `'/abc/resource.js?query#hash'`.
85
+ */
77
86
  resource: string;
87
+ /**
88
+ * The path string of the current module, excluding the query and fragment parameters.
89
+ * @example `'/abc/resource.js?query#hash'` in `'/abc/resource.js'`.
90
+ */
78
91
  resourcePath: string;
92
+ /**
93
+ * The query parameter for the path string of the current module.
94
+ * @example `'?query'` in `'/abc/resource.js?query#hash'`.
95
+ */
79
96
  resourceQuery: string;
97
+ /**
98
+ * The fragment parameter of the current module's path string.
99
+ * @example `'#hash'` in `'/abc/resource.js?query#hash'`.
100
+ */
80
101
  resourceFragment: string;
102
+ /**
103
+ * Tells Rspack that this loader will be called asynchronously. Returns `this.callback`.
104
+ */
81
105
  async(): LoaderContextCallback;
106
+ /**
107
+ * A function that can be called synchronously or asynchronously in order to return multiple
108
+ * results. The expected arguments are:
109
+ *
110
+ * 1. The first parameter must be `Error` or `null`, which marks the current module as a
111
+ * compilation failure.
112
+ * 2. The second argument is a `string` or `Buffer`, which indicates the contents of the file
113
+ * after the module has been processed by the loader.
114
+ * 3. The third parameter is a source map that can be processed by the loader.
115
+ * 4. The fourth parameter is ignored by Rspack and can be anything (e.g. some metadata).
116
+ */
82
117
  callback: LoaderContextCallback;
118
+ /**
119
+ * A function that sets the cacheable flag.
120
+ * By default, the processing results of the loader are marked as cacheable.
121
+ * Calling this method and passing `false` turns off the loader's ability to
122
+ * cache processing results.
123
+ */
83
124
  cacheable(cacheable?: boolean): void;
125
+ /**
126
+ * Tells if source map should be generated. Since generating source maps can be an expensive task,
127
+ * you should check if source maps are actually requested.
128
+ */
84
129
  sourceMap: boolean;
130
+ /**
131
+ * The base path configured in Rspack config via `context`.
132
+ */
85
133
  rootContext: string;
134
+ /**
135
+ * The directory path of the currently processed module, which changes with the
136
+ * location of each processed module.
137
+ * For example, if the loader is processing `/project/src/components/Button.js`,
138
+ * then the value of `this.context` would be `/project/src/components`.
139
+ */
86
140
  context: string | null;
141
+ /**
142
+ * The index in the loaders array of the current loader.
143
+ */
87
144
  loaderIndex: number;
88
145
  remainingRequest: string;
89
146
  currentRequest: string;
90
147
  previousRequest: string;
148
+ /**
149
+ * The module specifier string after being resolved.
150
+ * For example, if a `resource.js` is processed by `loader1.js` and `loader2.js`, the value of
151
+ * `this.request` will be `/path/to/loader1.js!/path/to/loader2.js!/path/to/resource.js`.
152
+ */
91
153
  request: string;
92
154
  /**
93
155
  * An array of all the loaders. It is writeable in the pitch phase.
@@ -108,23 +170,80 @@ export interface LoaderContext<OptionsType = {}> {
108
170
  * ]
109
171
  */
110
172
  loaders: LoaderObject[];
173
+ /**
174
+ * The value of `mode` is read when Rspack is run.
175
+ * The possible values are: `'production'`, `'development'`, `'none'`
176
+ */
111
177
  mode?: Mode;
178
+ /**
179
+ * The current compilation target. Passed from `target` configuration options.
180
+ */
112
181
  target?: Target;
182
+ /**
183
+ * Whether HMR is enabled.
184
+ */
113
185
  hot?: boolean;
114
186
  /**
115
- * @param schema To provide the best performance, Rspack does not perform the schema validation. If your loader requires schema validation, please call scheme-utils or zod on your own.
187
+ * Get the options passed in by the loader's user.
188
+ * @param schema To provide the best performance, Rspack does not perform the schema
189
+ * validation. If your loader requires schema validation, please call scheme-utils or
190
+ * zod on your own.
116
191
  */
117
192
  getOptions(schema?: any): OptionsType;
193
+ /**
194
+ * Resolve a module specifier.
195
+ * @param context The absolute path to a directory. This directory is used as the starting
196
+ * location for resolving.
197
+ * @param request The module specifier to be resolved.
198
+ * @param callback A callback function that gives the resolved path.
199
+ */
118
200
  resolve(context: string, request: string, callback: (arg0: null | Error, arg1?: string | false, arg2?: ResolveRequest) => void): void;
201
+ /**
202
+ * Create a resolver like `this.resolve`.
203
+ */
119
204
  getResolve(options: Resolve): ((context: string, request: string, callback: ResolveCallback) => void) | ((context: string, request: string) => Promise<string | false | undefined>);
205
+ /**
206
+ * Get the logger of this compilation, through which messages can be logged.
207
+ */
120
208
  getLogger(name: string): Logger;
209
+ /**
210
+ * Emit an error. Unlike `throw` and `this.callback(err)` in the loader, it does not
211
+ * mark the current module as a compilation failure, it just adds an error to Rspack's
212
+ * Compilation and displays it on the command line at the end of this compilation.
213
+ */
121
214
  emitError(error: Error): void;
215
+ /**
216
+ * Emit a warning.
217
+ */
122
218
  emitWarning(warning: Error): void;
123
- emitFile(name: string, content: string | Buffer, sourceMap?: string, assetInfo?: JsAssetInfo): void;
219
+ /**
220
+ * Emit a new file. This method allows you to create new files during the loader execution.
221
+ */
222
+ emitFile(name: string, content: string | Buffer, sourceMap?: string, assetInfo?: AssetInfo): void;
223
+ /**
224
+ * Add a file as a dependency on the loader results so that any changes to them can be listened to.
225
+ * For example, `sass-loader`, `less-loader` use this trick to recompile when the imported style
226
+ * files change.
227
+ */
124
228
  addDependency(file: string): void;
229
+ /**
230
+ * Alias of `this.addDependency()`.
231
+ */
125
232
  dependency(file: string): void;
233
+ /**
234
+ * Add the directory as a dependency for the loader results so that any changes to the
235
+ * files in the directory can be listened to.
236
+ */
126
237
  addContextDependency(context: string): void;
238
+ /**
239
+ * Add a currently non-existent file as a dependency of the loader result, so that its
240
+ * creation and any changes can be listened. For example, when a new file is created at
241
+ * that path, it will trigger a rebuild.
242
+ */
127
243
  addMissingDependency(missing: string): void;
244
+ /**
245
+ * Removes all dependencies of the loader result.
246
+ */
128
247
  clearDependencies(): void;
129
248
  getDependencies(): string[];
130
249
  getContextDependencies(): string[];
@@ -145,24 +264,58 @@ export interface LoaderContext<OptionsType = {}> {
145
264
  */
146
265
  importModule<T = any>(request: string, options: ImportModuleOptions | undefined, callback: (err?: null | Error, exports?: T) => any): void;
147
266
  importModule<T = any>(request: string, options?: ImportModuleOptions): Promise<T>;
267
+ /**
268
+ * Access to the `compilation` object's `inputFileSystem` property.
269
+ */
148
270
  fs: any;
149
271
  /**
150
272
  * This is an experimental API and maybe subject to change.
151
273
  * @experimental
152
274
  */
153
275
  experiments: LoaderExperiments;
276
+ /**
277
+ * Access to some utilities.
278
+ */
154
279
  utils: {
280
+ /**
281
+ * Return a new request string using absolute paths when possible.
282
+ */
155
283
  absolutify: (context: string, request: string) => string;
284
+ /**
285
+ * Return a new request string avoiding absolute paths when possible.
286
+ */
156
287
  contextify: (context: string, request: string) => string;
288
+ /**
289
+ * Return a new Hash object from provided hash function.
290
+ */
157
291
  createHash: (algorithm?: string) => Hash;
158
292
  };
293
+ /**
294
+ * The value depends on the loader configuration:
295
+ * - If the current loader was configured with an options object, `this.query` will
296
+ * point to that object.
297
+ * - If the current loader has no options, but was invoked with a query string, this
298
+ * will be a string starting with `?`.
299
+ */
159
300
  query: string | OptionsType;
301
+ /**
302
+ * A data object shared between the pitch and the normal phase.
303
+ */
160
304
  data: unknown;
305
+ /**
306
+ * Access to the current Compiler object of Rspack.
307
+ */
161
308
  _compiler: Compiler;
309
+ /**
310
+ * Access to the current Compilation object of Rspack.
311
+ */
162
312
  _compilation: Compilation;
313
+ /**
314
+ * @deprecated Hacky access to the Module object being loaded.
315
+ */
163
316
  _module: Module;
164
317
  /**
165
- * Note: This is not a webpack public API, maybe removed in future.
318
+ * Note: This is not a Rspack public API, maybe removed in future.
166
319
  * Store some data from loader, and consume it from parser, it may be removed in the future
167
320
  *
168
321
  * @internal
@@ -1,4 +1,4 @@
1
- import type { JsAssetInfo, RawFuncUseCtx } from "@rspack/binding";
1
+ import type { AssetInfo, RawFuncUseCtx } from "@rspack/binding";
2
2
  import type * as webpackDevServer from "webpack-dev-server";
3
3
  import type { ChunkGraph } from "../ChunkGraph";
4
4
  import type { Compilation, PathData } from "../Compilation";
@@ -8,7 +8,7 @@ import type ModuleGraph from "../ModuleGraph";
8
8
  import type { LazyCompilationDefaultBackendOptions } from "../builtin-plugin/lazy-compilation/backend";
9
9
  import type { Chunk } from "../exports";
10
10
  export type FilenameTemplate = string;
11
- export type Filename = FilenameTemplate | ((pathData: PathData, assetInfo?: JsAssetInfo) => string);
11
+ export type Filename = FilenameTemplate | ((pathData: PathData, assetInfo?: AssetInfo) => string);
12
12
  /** Name of the configuration. Used when loading multiple configurations. */
13
13
  export type Name = string;
14
14
  /** A list of name defining all sibling configurations it depends on. Dependent configurations need to be compiled first. */
@@ -1814,7 +1814,7 @@ export type LazyCompilationOptions = {
1814
1814
  /**
1815
1815
  * Test function or regex to determine which modules to include.
1816
1816
  */
1817
- test?: RegExp | ((module: any) => boolean);
1817
+ test?: RegExp | ((module: Module) => boolean);
1818
1818
  };
1819
1819
  /**
1820
1820
  * Options for incremental builds.