Skip to content

Commit

Permalink
fix: update typing for webpack@^5.94.0 (#110)
Browse files Browse the repository at this point in the history
* fix: update typing for webpack@^5.94.0

* chore: remove explicit declaration of tapable types
  • Loading branch information
rostislav-simonik authored Nov 1, 2024
1 parent a6629fc commit 5a0b8d8
Show file tree
Hide file tree
Showing 3 changed files with 1,736 additions and 1,778 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"dependencies": {
"chalk": "^4.1.0",
"enhanced-resolve": "^5.7.0",
"tapable": "^2.2.1",
"tsconfig-paths": "^4.1.2"
},
"devDependencies": {
Expand All @@ -30,7 +31,7 @@
"ts-jest": "^27.0.7",
"ts-loader": "^8.0.18",
"typescript": "^4.2.3",
"webpack": "^5.65.0",
"webpack": "^5.94.0",
"webpack-cli": "^4.5.0"
},
"scripts": {
Expand Down
68 changes: 37 additions & 31 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@ import * as Options from "./options";
import * as Logger from "./logger";
import * as fs from "fs";
import { ResolvePluginInstance, Resolver } from "webpack";
import { ResolveRequest, ResolveContext } from "enhanced-resolve";
import { AsyncSeriesBailHook } from "tapable";
import { ResolveContext, ResolveRequest } from "enhanced-resolve";

type TapAsyncCallback = Parameters<
AsyncSeriesBailHook<
[ResolveRequest, ResolveContext],
null | ResolveRequest
>["tapAsync"]
>[1];

type TapAsyncInnerCallback = Parameters<TapAsyncCallback>[2];

type FileSystem = Resolver["fileSystem"];
type TapAsyncCallback = (
request: ResolveRequest,
context: ResolveContext,
callback: TapAsyncInnerCallback
) => void;
type TapAsyncInnerCallback = (
error?: Error | null | false,
result?: null | ResolveRequest
) => void;

export interface LegacyResolverPlugin {
readonly apply: (resolver: LegacyResolver) => void;
Expand All @@ -28,7 +29,12 @@ export interface LegacyResolver {
readonly doResolve: doResolveLegacy | doResolve;
readonly join: (relativePath: string, innerRequest: Request) => Request;
readonly fileSystem: LegacyResolverFileSystem;
readonly getHook: (hook: string) => Tapable;
readonly getHook: (
hook: string
) => AsyncSeriesBailHook<
[ResolveRequest, ResolveContext],
null | ResolveRequest
>;
}

export type doResolveLegacy = (
Expand All @@ -39,14 +45,20 @@ export type doResolveLegacy = (
) => void;

export type doResolve = (
hook: Tapable,
hook: AsyncSeriesBailHook<
[ResolveRequest, ResolveContext],
null | ResolveRequest
>,
req: Request,
message: string,
resolveContext: LegacyResolveContext,
callback: Callback
) => void;

export type ReadJsonCallback = (error: Error | undefined, result?: {}) => void;
export type ReadJsonCallback = (
error: Error | undefined | null,
result?: {}
) => void;

export type ReadJson = (path2: string, callback: ReadJsonCallback) => void;

Expand All @@ -58,17 +70,6 @@ export interface LegacyResolveContext {
missing?: string;
}

export interface Tapable {
readonly tapAsync: (
options: TapableOptions,
callback: TapAsyncCallback
) => void;
}

export interface TapableOptions {
readonly name: string;
}

export type ResolverCallbackLegacy = (
request: Request,
callback: Callback
Expand Down Expand Up @@ -120,7 +121,9 @@ export interface Callback {
// eslint-disable-next-line no-redeclare
const getInnerRequest: getInnerRequest = require("enhanced-resolve/lib/getInnerRequest");

export class TsconfigPathsPlugin implements ResolvePluginInstance {
export class TsconfigPathsPlugin
implements Exclude<ResolvePluginInstance, Function>
{
source: string = "described-resolve";
target: string = "resolve";

Expand Down Expand Up @@ -216,12 +219,12 @@ export class TsconfigPathsPlugin implements ResolvePluginInstance {
);
} else if ("plugin" in resolver) {
// This is the legacy (Webpack < 4.0.0) way of using the plugin system.
const legacyResolver = (resolver as unknown) as LegacyResolver;
const legacyResolver = resolver as unknown as LegacyResolver;
legacyResolver.plugin(
this.source,
createPluginLegacy(
this.matchPath,
(resolver as unknown) as LegacyResolver,
resolver as unknown as LegacyResolver,
this.absoluteBaseUrl,
this.target,
this.extensions
Expand Down Expand Up @@ -251,7 +254,10 @@ function createPluginCallback(
baseMatchPath: TsconfigPaths.MatchPathAsync,
resolver: Resolver,
baseAbsoluteBaseUrl: string,
hook: Tapable,
hook: AsyncSeriesBailHook<
[ResolveRequest, ResolveContext],
null | ResolveRequest
>,
extensions: ReadonlyArray<string>
): TapAsyncCallback {
const fileExistAsync = createFileExistAsync(resolver.fileSystem);
Expand Down Expand Up @@ -420,7 +426,7 @@ function createPluginLegacy(
}

function readJson(
fileSystem: FileSystem,
fileSystem: FileSystem | LegacyResolverFileSystem,
path2: string,
callback: ReadJsonCallback
): void {
Expand All @@ -447,7 +453,7 @@ function readJson(
}

function createReadJsonAsync(
filesystem: FileSystem
filesystem: FileSystem | LegacyResolverFileSystem
): TsconfigPaths.ReadJsonAsync {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return (path2: string, callback2: (err?: Error, content?: any) => void) => {
Expand All @@ -463,7 +469,7 @@ function createReadJsonAsync(
}

function createFileExistAsync(
filesystem: FileSystem
filesystem: FileSystem | LegacyResolverFileSystem
): TsconfigPaths.FileExistsAsync {
return (
path2: string,
Expand Down
Loading

0 comments on commit 5a0b8d8

Please sign in to comment.