Skip to content

Commit acbc71f

Browse files
committed
enrich WebpackLoaderContext type
1 parent f3089d2 commit acbc71f

File tree

2 files changed

+151
-63
lines changed

2 files changed

+151
-63
lines changed

src/interfaces.ts

Lines changed: 147 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import type * as typescript from 'typescript';
22

33
import { Chalk } from 'chalk';
44
import * as logger from './logger';
5-
import * as webpack from 'webpack';
5+
import type * as webpack from 'webpack';
6+
import type * as enhancedResolve from 'enhanced-resolve';
67

78
export interface ErrorInfo {
89
code: number;
@@ -283,63 +284,134 @@ export type WebpackLoaderCallback = (
283284
sourceMap?: string | any
284285
) => void;
285286

286-
/** cutdown version of https://github.com/DefinitelyTyped/DefinitelyTyped/blob/9a1a04bc85f4137fbd053e780899526881bdd1ff/types/webpack/index.d.ts#L2222 */
287+
/** based on https://github.com/webpack/webpack/pull/13164#issuecomment-821357676 */
287288
export interface WebpackLoaderContext {
289+
version: number;
290+
288291
/**
289-
* The directory of the module. Can be used as context for resolving other stuff.
290-
* In the example: /abc because resource.js is in this directory
292+
* Hacky access to the Compilation object of webpack.
291293
*/
292-
context: string;
294+
_compilation: webpack.Compilation;
293295

294296
/**
295-
* Starting with webpack 4, the formerly `this.options.context` is provided as `this.rootContext`.
297+
* Hacky access to the Compiler object of webpack.
296298
*/
297-
rootContext: string;
299+
_compiler: webpack.Compiler;
300+
301+
/**
302+
* Hacky access to the Module object being loaded.
303+
*/
304+
_module: webpack.NormalModule;
305+
306+
addBuildDependency(dep: string): void;
307+
308+
/**
309+
* Add a directory as dependency of the loader result.
310+
* https://github.com/webpack/loader-runner/blob/6221befd031563e130f59d171e732950ee4402c6/lib/LoaderRunner.js#L305
311+
*/
312+
addContextDependency(context: string): void;
313+
314+
/**
315+
* Adds a file as dependency of the loader result in order to make them watchable.
316+
* For example, html-loader uses this technique as it finds src and src-set attributes.
317+
* Then, it sets the url's for those attributes as dependencies of the html file that is parsed.
318+
* https://github.com/webpack/loader-runner/blob/6221befd031563e130f59d171e732950ee4402c6/lib/LoaderRunner.js#L302
319+
*/
320+
addDependency(file: string): void;
321+
322+
/** https://github.com/webpack/loader-runner/blob/6221befd031563e130f59d171e732950ee4402c6/lib/LoaderRunner.js#L308 */
323+
addMissingDependency(context: string): void;
298324

299325
/**
300326
* Make this loader async.
327+
* https://github.com/webpack/loader-runner/blob/6221befd031563e130f59d171e732950ee4402c6/lib/LoaderRunner.js#L108
301328
*/
302329
async(): WebpackLoaderCallback | undefined;
303330

304331
/**
305-
* Make this loader result cacheable. By default it's not cacheable.
306-
* A cacheable loader must have a deterministic result, when inputs and dependencies haven't changed.
307-
* This means the loader shouldn't have other dependencies than specified with this.addDependency.
308-
* Most loaders are deterministic and cacheable.
332+
* Make this loader result cacheable. By default it's not cacheable.
333+
* A cacheable loader must have a deterministic result, when inputs and dependencies haven't changed.
334+
* This means the loader shouldn't have other dependencies than specified with this.addDependency.
335+
* Most loaders are deterministic and cacheable.
336+
* https://github.com/webpack/loader-runner/blob/6221befd031563e130f59d171e732950ee4402c6/lib/LoaderRunner.js#L297
309337
*/
310338
cacheable(flag?: boolean): void;
311339

340+
/** https://github.com/webpack/loader-runner/blob/6221befd031563e130f59d171e732950ee4402c6/lib/LoaderRunner.js#L116 */
341+
callback(): void;
342+
312343
/**
313-
* An array of all the loaders. It is writeable in the pitch phase.
314-
* loaders = [{request: string, path: string, query: string, module: function}]
315-
*
316-
* In the example:
317-
* [
318-
* { request: "/abc/loader1.js?xyz",
319-
* path: "/abc/loader1.js",
320-
* query: "?xyz",
321-
* module: [Function]
322-
* },
323-
* { request: "/abc/node_modules/loader2/index.js",
324-
* path: "/abc/node_modules/loader2/index.js",
325-
* query: "",
326-
* module: [Function]
327-
* }
328-
* ]
344+
* Remove all dependencies of the loader result. Even initial dependencies and these of other loaders. Consider using pitch.
345+
* https://github.com/webpack/loader-runner/blob/6221befd031563e130f59d171e732950ee4402c6/lib/LoaderRunner.js#L320
329346
*/
330-
loaders: any[];
347+
clearDependencies(): void;
331348

332349
/**
333-
* The index in the loaders array of the current loader.
334-
* In the example: in loader1: 0, in loader2: 1
350+
* The directory of the module. Can be used as context for resolving other stuff.
351+
* eg '/workspaces/ts-loader/examples/vanilla/src'
352+
* https://github.com/webpack/loader-runner/blob/6221befd031563e130f59d171e732950ee4402c6/lib/LoaderRunner.js#L289
335353
*/
336-
loaderIndex: number;
354+
context: string;
355+
356+
/** https://github.com/webpack/loader-runner/blob/6221befd031563e130f59d171e732950ee4402c6/lib/LoaderRunner.js#L358 */
357+
readonly currentRequest: string;
337358

359+
/** https://github.com/webpack/loader-runner/blob/6221befd031563e130f59d171e732950ee4402c6/lib/LoaderRunner.js#L381 */
360+
readonly data: any;
338361
/**
339-
* The resource file.
340-
* In the example: "/abc/resource.js"
362+
* alias of addDependency
363+
* Adds a file as dependency of the loader result in order to make them watchable.
364+
* For example, html-loader uses this technique as it finds src and src-set attributes.
365+
* Then, it sets the url's for those attributes as dependencies of the html file that is parsed.
366+
* https://github.com/webpack/loader-runner/blob/6221befd031563e130f59d171e732950ee4402c6/lib/LoaderRunner.js#L302
341367
*/
342-
resourcePath: string;
368+
dependency(file: string): void;
369+
370+
/**
371+
* https://github.com/webpack/webpack/blob/49890b77aae455b3204c17fdbed78eeb47bc1d98/lib/NormalModule.js#L520
372+
*/
373+
emitError(error: Error | string): void;
374+
375+
/** https://github.com/webpack/webpack/blob/49890b77aae455b3204c17fdbed78eeb47bc1d98/lib/NormalModule.js#L562 */
376+
emitFile(
377+
name: string,
378+
content: string,
379+
sourceMap: string,
380+
assetInfo: webpack.AssetInfo
381+
): void;
382+
383+
/**
384+
* https://github.com/webpack/webpack/blob/49890b77aae455b3204c17fdbed78eeb47bc1d98/lib/NormalModule.js#L510
385+
*/
386+
emitWarning(warning: Error | string): void;
387+
388+
/** https://github.com/webpack/webpack/blob/49890b77aae455b3204c17fdbed78eeb47bc1d98/lib/NormalModule.js#L590 */
389+
fs: enhancedResolve.CachedInputFileSystem;
390+
391+
/** https://github.com/webpack/loader-runner/blob/6221befd031563e130f59d171e732950ee4402c6/lib/LoaderRunner.js#L314 */
392+
getContextDependencies(): string[];
393+
394+
/** https://github.com/webpack/loader-runner/blob/6221befd031563e130f59d171e732950ee4402c6/lib/LoaderRunner.js#L311 */
395+
getDependencies(): string[];
396+
397+
/** https://github.com/webpack/webpack/blob/49890b77aae455b3204c17fdbed78eeb47bc1d98/lib/NormalModule.js#L530 */
398+
getLogger(name: string): webpack.Compilation['logger'];
399+
400+
/** https://github.com/webpack/loader-runner/blob/6221befd031563e130f59d171e732950ee4402c6/lib/LoaderRunner.js#L317 */
401+
getMissingDependencies(): string[];
402+
403+
/** https://github.com/webpack/webpack/blob/49890b77aae455b3204c17fdbed78eeb47bc1d98/lib/NormalModule.js#L472 */
404+
getOptions(schema: any): any;
405+
406+
/** https://github.com/webpack/webpack/blob/49890b77aae455b3204c17fdbed78eeb47bc1d98/lib/NormalModule.js#L541 */
407+
getResolve(options: webpack.Configuration): any;
408+
409+
/**
410+
* The index in the loaders array of the current loader.
411+
* In the example: in loader1: 0, in loader2: 1
412+
* https://github.com/webpack/loader-runner/blob/6221befd031563e130f59d171e732950ee4402c6/lib/LoaderRunner.js#L290
413+
*/
414+
loaderIndex: number;
343415

344416
/**
345417
* Resolves the given request to a module, applies all configured loaders and calls
@@ -355,44 +427,58 @@ export interface WebpackLoaderContext {
355427
sourceMap: any,
356428
module: webpack.Module
357429
) => void
358-
): any;
430+
): void;
359431

360-
/**
361-
* Adds a file as dependency of the loader result in order to make them watchable.
362-
* For example, html-loader uses this technique as it finds src and src-set attributes.
363-
* Then, it sets the url's for those attributes as dependencies of the html file that is parsed.
364-
*/
365-
addDependency(file: string): void;
432+
/** https://github.com/webpack/webpack/blob/49890b77aae455b3204c17fdbed78eeb47bc1d98/lib/NormalModule.js#L586 */
433+
mode: 'development' | 'production' | 'none';
366434

367-
/**
368-
* Adds a file as dependency of the loader result in order to make them watchable.
369-
* For example, html-loader uses this technique as it finds src and src-set attributes.
370-
* Then, it sets the url's for those attributes as dependencies of the html file that is parsed.
371-
*/
372-
dependency(file: string): void;
435+
/** https://github.com/webpack/loader-runner/blob/6221befd031563e130f59d171e732950ee4402c6/lib/LoaderRunner.js#L366 */
436+
readonly previousRequest: any;
373437

374-
/**
375-
* Add a directory as dependency of the loader result.
376-
*/
377-
addContextDependency(directory: string): void;
438+
/** https://github.com/webpack/loader-runner/blob/6221befd031563e130f59d171e732950ee4402c6/lib/LoaderRunner.js#L374 */
439+
readonly query: any;
378440

379-
/**
380-
* Remove all dependencies of the loader result. Even initial dependencies and these of other loaders. Consider using pitch.
381-
*/
382-
clearDependencies(): void;
441+
/** https://github.com/webpack/loader-runner/blob/6221befd031563e130f59d171e732950ee4402c6/lib/LoaderRunner.js#L348 */
442+
readonly remainingRequest: any;
443+
444+
/** https://github.com/webpack/loader-runner/blob/6221befd031563e130f59d171e732950ee4402c6/lib/LoaderRunner.js#L340 */
445+
readonly request: any;
446+
447+
/** https://github.com/webpack/webpack/blob/49890b77aae455b3204c17fdbed78eeb47bc1d98/lib/NormalModule.js#L538 */
448+
resolve(context: string, request: any, callback: any): any;
383449

384450
/**
385-
* Hacky access to the Compilation object of webpack.
451+
* Starting with webpack 4, the formerly `this.options.context` is provided as `this.rootContext`.
452+
* https://github.com/webpack/webpack/blob/49890b77aae455b3204c17fdbed78eeb47bc1d98/lib/NormalModule.js#L583
386453
*/
387-
_compilation: any;
454+
rootContext: string;
388455

389456
/**
390-
* Hacky access to the Compiler object of webpack.
457+
* An array of all the loaders. It is writeable in the pitch phase.
458+
* loaders = [{request: string, path: string, query: string, module: function}]
459+
*
460+
* In the example:
461+
* [
462+
* { request: "/abc/loader1.js?xyz",
463+
* path: "/abc/loader1.js",
464+
* query: "?xyz",
465+
* module: [Function]
466+
* },
467+
* { request: "/abc/node_modules/loader2/index.js",
468+
* path: "/abc/node_modules/loader2/index.js",
469+
* query: "",
470+
* module: [Function]
471+
* }
472+
* ]
473+
*
474+
* https://github.com/webpack/loader-runner/blob/6221befd031563e130f59d171e732950ee4402c6/lib/LoaderRunner.js#L291
475+
* https://github.com/webpack/loader-runner/blob/6221befd031563e130f59d171e732950ee4402c6/lib/LoaderRunner.js#L46
391476
*/
392-
_compiler: any;
477+
loaders: { request: string }[];
393478

394479
/**
395-
* Hacky access to the Module object being loaded.
480+
* The resource file.
481+
* In the example: "/abc/resource.js"
396482
*/
397-
_module: any;
483+
resourcePath: string;
398484
}

src/resolver.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
import * as webpack from 'webpack';
1+
import type * as webpack from 'webpack';
22

33
import { create } from 'enhanced-resolve';
44

5-
export function makeResolver(options: webpack.Configuration): ResolveSync {
5+
export function makeResolver(
6+
options: webpack.WebpackOptionsNormalized
7+
): ResolveSync {
68
return create.sync(options.resolve);
79
}
810

0 commit comments

Comments
 (0)