webpack / enhanced-resolve

Offers an async require.resolve function. It's highly configurable.
MIT License
928 stars 186 forks source link

fix: hook.isUsed is not a function #397

Closed monako97 closed 8 months ago

linux-foundation-easycla[bot] commented 10 months ago

CLA Signed

The committers listed above are authorized under a signed CLA.

monako97 commented 9 months ago

Can you provide an example of the problem?

https://github.com/monako97/enhanced-resolve-pull-397

monako97 commented 9 months ago

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)
alexander-akait commented 9 months ago

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

alexander-akait commented 9 months ago

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)

monako97 commented 8 months ago

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