Closed monako97 closed 8 months ago
Can you provide an example of the problem?
Can you provide an example of the problem?
webpack 5.89.0 compiled successfully in 91 ms
❯ npm run build
> enhanced-resolve-pull-397@1.0.0 build
> webpack
/Users/moneko/Downloads/Github/enhanced-resolve-pull-397/node_modules/enhanced-resolve/lib/Resolver.js:504
if (hook.isUsed()) {
^
TypeError: hook.isUsed is not a function
at Resolver.doResolve (/Users/moneko/Downloads/Github/enhanced-resolve-pull-397/node_modules/enhanced-resolve/lib/Resolver.js:504:12)
at file:///Users/moneko/Downloads/Github/enhanced-resolve-pull-397/webpack.config.js:28:33
at Hook.eval [as callAsync] (eval at create (/Users/moneko/Downloads/Github/enhanced-resolve-pull-397/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:37:1)
at Hook.CALL_ASYNC_DELEGATE [as _callAsync] (/Users/moneko/Downloads/Github/enhanced-resolve-pull-397/node_modules/tapable/lib/Hook.js:18:14)
at Resolver.doResolve (/Users/moneko/Downloads/Github/enhanced-resolve-pull-397/node_modules/enhanced-resolve/lib/Resolver.js:516:16)
at Array.<anonymous> (/Users/moneko/Downloads/Github/enhanced-resolve-pull-397/node_modules/enhanced-resolve/lib/FileExistsPlugin.js:48:15)
at runCallbacks (/Users/moneko/Downloads/Github/enhanced-resolve-pull-397/node_modules/enhanced-resolve/lib/CachedInputFileSystem.js:43:15)
at /Users/moneko/Downloads/Github/enhanced-resolve-pull-397/node_modules/enhanced-resolve/lib/CachedInputFileSystem.js:249:5
at callback (/Users/moneko/Downloads/Github/enhanced-resolve-pull-397/node_modules/graceful-fs/polyfills.js:306:20)
at FSReqCallback.oncomplete (node:fs:211:5)
Please use this code:
class ResolverPlugin {
constructor(){}
apply(resolver) {
const target = resolver.ensureHook('resolved');
const rPrev = `./${relative(programPath, 'src')}`;
const rNext = `./${relative(programPath, 'patch')}`;
resolver
.getHook('existing-file')
.tapAsync('ResolverPlugin', (request, resolveContext, callback) => {
const inner = request.request || request.path;
if (inner && inner.startsWith(join(programPath, 'src')) && request.relativePath) {
const overridePath = inner.replace(join(programPath, 'src'), join(programPath, 'patch'));
if (existsSync(overridePath)) {
const relativePath = request.relativePath.replace(rPrev, rNext);
Object.assign(request, {
path: overridePath,
relativePath: relativePath,
__innerRequest_relativePath: relativePath,
__innerRequest: relativePath,
});
return resolver.doResolve(
target,
request,
`ResolverPlugin use patch`,
resolveContext,
callback,
);
}
}
return callback();
});
}
}
you need to have const target = resolver.ensureHook('resolved');
, before call a hook
In theory we can improve our logic and throw a better error here, but I am afraid it will be not good for perf reasons, maybe we can improve our docs (I think it will be better)
Please use this code:
class ResolverPlugin { constructor(){} apply(resolver) { const target = resolver.ensureHook('resolved'); const rPrev = `./${relative(programPath, 'src')}`; const rNext = `./${relative(programPath, 'patch')}`; resolver .getHook('existing-file') .tapAsync('ResolverPlugin', (request, resolveContext, callback) => { const inner = request.request || request.path; if (inner && inner.startsWith(join(programPath, 'src')) && request.relativePath) { const overridePath = inner.replace(join(programPath, 'src'), join(programPath, 'patch')); if (existsSync(overridePath)) { const relativePath = request.relativePath.replace(rPrev, rNext); Object.assign(request, { path: overridePath, relativePath: relativePath, __innerRequest_relativePath: relativePath, __innerRequest: relativePath, }); return resolver.doResolve( target, request, `ResolverPlugin use patch`, resolveContext, callback, ); } } return callback(); }); } }
you need to have
const target = resolver.ensureHook('resolved');
, before call a hook
Thank you very much
The committers listed above are authorized under a signed CLA.