Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: update typing for webpack@^5.94.0 #110

Merged
merged 2 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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